为什么这个可以运行更新我的onDraw?

时间:2016-12-24 08:23:38

标签: java android

我有以下代码:

 @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

    }
};

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

1 个答案:

答案 0 :(得分:1)

尝试使用这样的代码:

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