有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在安卓 studio上更新Java的onDraw()

所以我刚开始学习如何用安卓 studio创建一个应用程序,我正在尝试制作一个简单的动画。例如,我想到了一个弹跳球,但目前我无法使我的球移动。我不完全理解一切是如何工作的,但我设法在屏幕上画了一个球,现在我想让我的球移动,但问题是我无法在屏幕打开时更新屏幕(由onDraw函数绘制),手机在所有移动完成后只显示最后一个屏幕。我听说我必须使用invalidate函数,但我不知道如何使用它,我尝试使用move函数来使用它。我想知道,为了看到球在屏幕上移动,我必须对代码做的最简单的更改是什么

package com.example.myfirstapp;

import 安卓x.appcompat.app.AppCompatActivity;

import 安卓.content.Context;
import 安卓.graphics.Canvas;
import 安卓.graphics.Paint;
import 安卓.os.Bundle;
import 安卓.os.SystemClock;
import 安卓.view.View;
import 安卓.view.Window;
import 安卓.view.WindowManager;

import java.util.Random;

public class AnimationActivity extends AppCompatActivity {
    public int posX= 300;
    public int posY= 300;
    public int radius= 50;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        RenderView renderView = new RenderView(this);
        setContentView(renderView);
        while(posX<2000) {
            renderView.move();
            setContentView(renderView);
        }
    }


class RenderView extends View {
    public RenderView(Context context){
        super(context);
    }

    public void move(){
        posX=posX+1;
        //SystemClock.sleep(2);
        invalidate();
    }

    protected void onDraw(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();


        canvas.drawRGB(255, 255, 255);
        Paint ball = new Paint();
        ball.setAntiAlias(true);
        ball.setARGB(255,255,0,0);
        ball.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawCircle(posX,posY,radius,ball);

    }
}

}


共 (1) 个答案

  1. # 1 楼答案

    试试这个代码

        public class MainActivity extends AppCompatActivity {
        public int posX = 300;
        public int posY = 300;
        public int radius = 50;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            RenderView renderView = new RenderView(this);
            setContentView(renderView);
          /*  while (posX < 2000) {
    
                renderView.move();
                renderView.invalidate();
            }*/
        }
    
    
        class RenderView extends View {
            public RenderView(Context context) {
                super(context);
            }
    
            public void move() {
                posX++;
                //SystemClock.sleep(2);
            }
    
            protected void onDraw(Canvas canvas) {
                int width = getWidth();
                int height = getHeight();
    
    
                canvas.drawRGB(255, 255, 255);
                Paint ball = new Paint();
                ball.setAntiAlias(true);
                ball.setColor(getResources().getColor(R.color.colorAccent));
                ball.setStyle(Paint.Style.FILL_AND_STROKE);
                canvas.drawCircle(posX, posY, radius, ball);
                if (posX > 350) {
                    posX = 300;
                    posY+=10;
                }
                posX++;
                invalidate();
    
            }
        }
    }