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

问题描述 投票:0回答:1

所以我刚刚开始学习如何使用android-studio创建应用,而我正在尝试制作一个简单的动画。例如,我想到了一个弹跳的球,但此刻我无法使球移动。我不能完全理解所有的工作原理,但是我设法在屏幕上画了一个球,现在我想让球移动,但问题是我无法更新屏幕(由onDraw函数绘制)当手机打开时,手机仅显示所有动作完成后的最终屏幕。我听说我必须使用无效函数,但是我不知道如何使用它,因此我尝试制作一个移动函数来使用它。我想知道我要对代码进行最简单的更改才能看到球在屏幕上移动。

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

    }
}

}

java android android-studio animation android-animation
1个回答
0
投票

尝试此代码

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

        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.