WEB开发网
开发学院手机开发Android 开发 利用Handler来更新android的UI 阅读

利用Handler来更新android的UI

 2010-08-24 05:43:00 来源:WEB开发网   
核心提示:Thread myRefreshThread = null;BounceView myBounceView = null;Handler myHandler = new Handler() {public void handleMessage(Message msg) {switch (msg.what) {case

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

Tags:利用 Handler 更新

编辑录入:coldstar [复制链接] [打 印]
赞助商链接