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. // ... } }
但我不推荐上面这种写法,上面这种写法在线程中止时很可能会有问题。如果是用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 (前两个比较好)