利用Handler来更新android的UI
2010-08-24 05:43:00 来源:WEB开发网核心提示:myView = new MyView(this);mHandler = new Handler();mHandler.post(update);setContentView(myView);}private Runnable update = new Runnable() {public void run() {my
myView = new MyView(this);
mHandler = new Handler();
mHandler.post(update);
setContentView(myView);
}
private Runnable update = new Runnable() {
public void run() {
myView.update();
mHandler.postDelayed(update, 5);
}
};
class MyView extends View{
private float x = 0f;
public MyView(Context context) {
super(context);
}
public void update(){
postInvalidate();
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
x+=1;
Paint mPaint = new Paint();
mPaint.setColor(Color.BLUE);
canvas.drawRect(x, 40, x+40, 80, mPaint);
}
}
更多精彩
赞助商链接