Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> 利用Handler來更新android的UI

利用Handler來更新android的UI

編輯:初級開發

利用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);

}

}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved