论坛首页 移动开发技术论坛

利用Handler更新Android UI的另一种方法

浏览 18308 次
精华帖 (0) :: 良好帖 (5) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-07-29  

文字程序很简单,一个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);
		}
    	
    }
}

 之前还写过另外一篇关于Handler更新UI的文章:

http://rayleung.iteye.com/blog/411860

   发表时间:2009-08-04  
你的两篇文章对于invalidate()和postInvalidate()认识有些问题
http://www.iteye.com/topic/435147
http://rayleung.iteye.com/blog/411860

调用Handler.post(Runnable r)方法,Runnable运行在UI所在线程,所以可以直接调用View.invalidate()
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(new Runnable(){
			@Override
			public void run() {
				myView.invalidate();
				mHandler.postDelayed(this, 5);
			}
         });
        setContentView(myView);
    }
    
    class MyView extends View{
    	private float x = 0f;
		public MyView(Context context) {
			super(context);
			
	}
		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);
		}
    	
    }
}


在新线程里更新UI,可以直接postInvalidate()
	public void onCreate(Bundle savedInstanceState) {    
                super.onCreate(savedInstanceState);    
                this.requestWindowFeature(Window.FEATURE_NO_TITLE);    
	   
                myView = new MyView(this);
	       this.setContentView(this.myView);    
	       new Thread(new myThread()).start();   
	}    
   
     class myThread implements Runnable {    
	          public void run() {   
	              while (!Thread.currentThread().isInterrupted()) {    
	                   try {
	                	      myView.postInvalidate(); 
	                        Thread.sleep(100);     
	                   } catch (InterruptedException e) {    
	                        Thread.currentThread().interrupt();    
	                   }    
	               }    
	          }    
     } 
16 请登录后投票
   发表时间:2009-08-04  
非常感谢您指出我理解上的错误,谢谢!我会继续努力的!
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics