通过实例化Handler对象并重写handkeMessage方法实现了一个消息接收器。然后再线程中通过sendMessage方法发送更新界面的消息,接收器收到更新界面的消息时便执行invalidate方法更新屏幕显示。
package com.yarin.Android.TestOnView;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class GameView extends View
{
int miCount = 0;
int y = 0;
public GameView(Context context)
{
super(context);
}
public void onDraw(Canvas canvas)
{
if (miCount < 100)
{
miCount++;
}
else
{
miCount = 0;
}
//绘图
Paint mPaint = new Paint();
switch (miCount%4)
{
case 0:
mPaint.setColor(Color.BLUE);
break;
case 1:
mPaint.setColor(Color.GREEN);
break;
case 2:
mPaint.setColor(Color.RED);
break;
case 3:
mPaint.setColor(Color.YELLOW);
break;
default:
mPaint.setColor(Color.WHITE);
break;
}
//绘制矩形
canvas.drawRect((320-80)/2, y, (320-80)/2+80, y+40, mPaint);
}
}
上面是用来绘制界面的,我们还需要一个类来控制应用的操作