刚刚开始接触android线程编程的时候,习惯好像java一样,试图用下面的代码解决问题
- new Thread( new Runnable() {
- public void run() {
- myView.invalidate();
- }
- }).start();
new Thread( new Runnable() {
public void run() {
myView.invalidate();
}
}).start();
然而发现这样是不行的,因为它违背了单线程模型:Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行。查阅了文档和apidemo后,发觉常用的方法是利用Handler来实现UI线程的更新的。
下面代码的功能很简单:画一个圆出来,每隔0.1秒,圆向10移动10个像素。但可以清楚展示利用Handler更新UI的流程。
首先创建简单的View,代码如下:
- package com.ray.handler;
-
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Point;
- import android.graphics.drawable.Drawable;
- import android.view.View;
-
- public class BounceView extends View {
- float x = 40;
-
- public BounceView(Context context) {
- super(context);
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
- x+=10;
- Paint mPaint = new Paint();
- mPaint.setAntiAlias(true);
- mPaint.setColor(Color.GREEN);
- canvas.drawCircle(x, 40, 40, mPaint);
- }
- }
package com.ray.handler;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.View;
public class BounceView extends View {
float x = 40;
public BounceView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
x+=10;
Paint mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.GREEN);
canvas.drawCircle(x, 40, 40, mPaint);
}
}
创建Activity,代码如下:
- package com.ray.handler;
-
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.View;
- import android.view.Window;
-
- public class TestHandler extends Activity {
- protected static final int GUIUPDATEIDENTIFIER = 0x101;
-
- Thread myRefreshThread = null;
- BounceView myBounceView = null;
-
- Handler myHandler = new Handler() {
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case TestHandler.GUIUPDATEIDENTIFIER:
- myBounceView.invalidate();
- break;
- }
- super.handleMessage(msg);
- }
- };
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
-
- this.myBounceView = new BounceView(this);
- this.setContentView(this.myBounceView);
- new Thread(new myThread()).start();
- }
-
- class myThread implements Runnable {
- public void run() {
- while (!Thread.currentThread().isInterrupted()) {
-
- Message message = new Message();
- message.what = TestHandler.GUIUPDATEIDENTIFIER;
-
- TestHandler.this.myHandler.sendMessage(message);
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- }
- }
- }
- }
- }
阅读(782) | 评论(0) | 转发(0) |