调用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();    
	                   }    
	               }    
	          }    
     }