編輯:初級開發
利用Handler來更新android的UI(1)
剛剛開始接觸android線程編程的時候,習慣好像Java一樣,試圖用下面的代碼解決問題
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);
}
}
創建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();
}
}
}
}
}
文字程序很簡單,一個VIEw,一個Activity,利用handler和postInvalidate()更新UI。
程序效果是一個藍色的正方形向右移出屏幕。
package com.ray.test;
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.view.VIEw;
public class TestHandler extends Activity {
private MyView myVIEw;
private Handler mHandler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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);
}
}
本文收集10款對開發者有用的Android應用,希望能對你的開發有所幫助。如果你還知道有其他對開發者很有用的android應用?也分享出來吧。列表如下:1.Remote
這段時間都在看Java,android放了好久,現在慢慢再看先上結果圖:這次首先要實現上面的效果,使用的是ListActivity和SimpleAdapter適配器首先
以Windows平台的SDK為例,這裡Android開發網的模擬器配置路徑為 C:\Users\Administrator\.android\avd\2.3.3.avd
1. 先定義XML布局文件,<SeekBar android:id=@+id/mySeek