---
title: "Android开发：Invalidate和postInvalidate刷新View的区别及应用-----ImageView绘制旋转圆环（二）"
author: "rys5851968"
date: "2013-02-15T16:32:00Z"
tags: []
slug: "android-invalidate-postinvalidate-view-imageview"
draft: false
lastmod: "2013-03-04T08:35:58Z"
id: 1820745
url: "https://rys1314520.iteye.com/blog/1820745"
---

[上文](http://blog.csdn.net/yanzi1225627/article/details/8581897)已经实现了圆环旋转，但是很多合格。发现之前对handler Thread Runnable理解的不深，包括如何刷新UI。这次总算清了一下帐，基本搞清除了。Android中实现view的更新有两组方法，一组是invalidate，另一组是postInvalidate，其中前者是在UI线程自身中使用，而后者在非UI线程中使用。参考[这里](http://kalogen.iteye.com/blog/1566111)，刷新UI有三种方式：

1.不使用多线程和双缓冲

这种情况最简单了，一般只是希望在View发生改变时对UI进行重绘。你只需在Activity中显式地调用View对象中的invalidate()方法即可。系统会自动调用 View的onDraw()方法。

2.使用多线程和不使用双缓冲

这种情况需要开启新的线程，新开的线程就不好访问View对象了。强行访问的话会报：android.view.ViewRoot$CalledFromWrongThreadException：Only the original thread that created a view hierarchy can touch its views.

这时候你需要创建一个继承了android.os.Handler的子类，并重写handleMessage(Message msg)方法。android.os.Handler是能发送和处理消息的，你需要在Activity中发出更新UI的消息，然后再你的Handler（可以使用匿名内部类）中处理消息（因为匿名内部类可以访问父类变量， 你可以直接调用View对象中的invalidate()方法 ）。也就是说：在新线程创建并发送一个Message，然后再主线程中捕获、处理该消息。

3.使用多线程和双缓冲

Android中SurfaceView是View的子类，她同时也实现了双缓冲。你可以定义一个她的子类并实现SurfaceHolder.Callback接口。由于实现SurfaceHolder.Callback接口，新线程就不需要android.os.Handler帮忙了。SurfaceHolder中lockCanvas()方法可以锁定画布，绘制玩新的图像后调用unlockCanvasAndPost(canvas)解锁（显示），还是比较方便得。

上面清晰也可以看到SurfaceView绘图和一般ImageView绘图的区别。一般采用第二种，个人觉的第二种里还是采用postInvalidate的方法好。因为invalidate是在主UI线程里，容易卡。尤其在退出程序的时候，这个很明显。所以刷新ImageView，让圆环转起来，我采用的是postInvalidate方法。

代码如下：  
**class UpdateThread implements Runnable {**  
**public void run() {**  
**while (!Thread.currentThread().isInterrupted()) {**  
**try {**  
**Thread.sleep(100);**  
**} catch (InterruptedException e) {**  
**Thread.currentThread().interrupt();**  
**}**

**// 使用postInvalidate可以直接在线程中更新界面**  
**postInvalidate();**  
**}**  
**}**  
**}**

注意这个postInvalidate(); 当执行这句话时，ImageView会自动调用onDraw()一次。在protected void onDraw(Canvas canvas) { }里进行画图就Ok了。正基于此，postInvalidate()这句话放在Thread.sleep(100);后面也是可以的，感觉这样反而更容易理解！在DrawImageView的构造函数里调用new Thread（new UpdateThread（））.start()开启这个刷新线程。

另外，还有一点这种方式是定义一个UpdateThread，还可以直接在申请类的时候就实现这个接口如public class DrawImageView extends ImageView implements Runnable{}，这样做的好处就是变量的申明和定义上。

举个例子，像http://blog.csdn.net/hui\_ttoo/article/details/5390387这里提供的第二段示例程序：

```
class CustomizeView extends TextView {

	public CustomizeView(Context context) {
		super(context);
		new Thread(new UIUpdateThread()).start();
	}

	class UIUpdateThread implements Runnable {
		final Handler mHandler = new Handler();

		final Runnable mUpdateResults = new Runnable() {
			public void run() {
				concreteUpdateUI();
				invalidate();
			}
		};

		public void run() {
			// delay some minutes you desire. 
			/*try {
				Thread.sleep(1000 * 5);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}*/
			mHandler.post(mUpdateResults);
		}

	}

	protected void concreteUpdateUI() {
		// Add concrete movement for UI updates.  
		// ...  
	}

}
```

里面没有重载onDraw（）函数，而是自己新建一个刷新Ui的函数，这个函数可以获取TextView然后更新时间阿。如果既有自己定义的updateUi又重载了onDraw()函数。由于updateUI是类UIUpdateThread 里面的子函数，如果不作处理，是无否调用全局的View成员。

但我不推荐上面这种写法，上面这种写法在线程中止时很可能会有问题。如果是用invalidate方法更新UI，我推荐这种写法：

```
// 在onCreate()中开启线程

new Thread(new GameThread()).start();、

// 实例化一个handler

Handler myHandler = new Handler() {
// 接收到消息后处理
public void handleMessage(Message msg) {
switch (msg.what) {
case Activity01.REFRESH:
mGameView.invalidate(); // 刷新界面
break;
}

super.handleMessage(msg);
}
};

class GameThread implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
Message message = new Message();
message.what = Activity01.REFRESH;
// 发送消息
Activity01.this.myHandler.sendMessage(message);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}

}
```

参考：

http://www.cnblogs.com/tt\_mc/archive/2012/01/30/2332023.html

http://www.blogjava.net/gooogle/archive/2008/03/05/184030.html

http://pwp5566.iteye.com/blog/1473159

http://kalogen.iteye.com/blog/1566111

http://blog.csdn.net/hui\_ttoo/article/details/5390387 （前两个比较好）