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

How to update the onDraw() of Java on android studio

所以我刚开始学习如何使用 android-studio 创建应用程序,我正在尝试制作一个简单的动画。例如,我想到了一个弹跳球,但目前我无法让我的球移动。我不太明白一切是如何工作的,但我设法在屏幕上画了一个球,现在我想让我的球移动,但问题是我无法更新我的屏幕(由 onDraw 函数绘制)打开时,phone 仅在所有移动完成后显示最终屏幕。我听说我必须使用无效函数,但我不知道如何使用它,我试图制作一个移动函数来使用它。我想知道我必须对代码做哪些最简单的更改才能看到球在屏幕上移动。

package com.example.myfirstapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.Window;
import android.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);

    }
}

}

试试这个代码

    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();

        }
    }
}