gpt4 book ai didi

java - 为什么这个可运行程序不更新我的 onDraw?

转载 作者:行者123 更新时间:2023-12-01 09:05:37 30 4
gpt4 key购买 nike

我有以下代码:

 @Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(3);
canvas.drawRect(200, hotCOOL, hotCOOL, 400, mPaint);
}
Handler handler = new Handler(Looper.getMainLooper());
Runnable movePlayer0Runnable = new Runnable(){
public void run(){
hotCOOL= hotCOOL +20;
invalidate(); //will trigger the onDraw
handler.postDelayed(this,5000); //in 5 sec player0 will move again

}
};

我制作的矩形看起来应该会移动,但事实并非如此。有谁知道这是为什么吗?

最佳答案

尝试使用这样的代码:

@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(3);
canvas.drawRect(200, hotCOOL, hotCOOL, 400, mPaint);
}
Handler handler = new Handler(Looper.getMainLooper());
Runnable movePlayer0Runnable = new Runnable(){
public void run(){
hotCOOL= hotCOOL +20;
invalidate(); //will trigger the onDraw
handler.postDelayed(this,5000); //in 5 sec player0 will move again

}
};

handler.post(movePlayer0Runnable);

关于java - 为什么这个可运行程序不更新我的 onDraw?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41311611/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com