Invalidate和postInvalidate,android界面刷新

来源:互联网 发布:查看mac地址 编辑:程序博客网 时间:2024/05/17 21:45

       由于最近需要用到很多的UI处理、动画处理、图片处理等问题,牵涉到的动态刷新和局部刷新的问题挺多的,现将在网上的一篇博文加上自己的理解在这里做个记号,以便方便的时候查询使用。


原文地址:http://wuhua.javaeye.com/blog/168358


根据Android SDK api文档说明
invalidate 方法是用来更新视图(View)的方法,不过这东西的用法比较古怪
invalidate 方法如果你直接在主线程中调用,是看不到任何更新的。
如果跟线程结合使用的话
比如在下面的代码中就会抛出异常

UIThread implements Runnable{
public void run(){
invalidate();
}
}
上面的代码会抛出Only the original thread that created a view hierarchy can touch its views。
怎么样解决上面的问题呢,如果你有两个View,你需要一个View用来显示当前的状态,一个Thread去下载网络数据
或者是读取文件等,这些数据读取完毕后你要更新View到当前屏幕上怎么办呢。看看下面的代码,也许可以帮助你

第一种解决方案是:
class UIUpdateThread implements Runnable{

            public void run() {
                try {
                    Thread.sleep(1000*5);
                    mHandler.post(mUpdateResults);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
            }
            
            final Handler mHandler = new Handler();
                final Runnable mUpdateResults = new Runnable() {
                    public void run() {
                        invalidate(); //更新视图
                    }
                };
            
}
你必须实现一个Handler.然后再你下载数据的线程中放上一个mHandler.post(mUpdateResults);这样就可以了。

第2中方案比较简单

LoadDataThread implements Runnable{
public void run(){
   doLoadData();
   mHandler.sendMessage(mHandler.obtainMessage()); //这里系统会自动调用handleMessage;这样就可以更新视图了
   }
}

Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // 这里处理视图需要更新的代码。
                    
        }
   };
   


    ============

总结:
       总的来说,invalidate()得在UI线程中被调动,在工作者线程中可以通过Handler来通知UI线程进行界面更新。而postInvalidate()在工作者线程中被调用。


   
   
对于postInvalidate用法来说就相对简单点了直接调用就OK了,这里就不详细说了。


下面示例来自:http://blog.csdn.net/vincent_czz/article/details/7018725

package com.czz.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.os.Message;import android.view.MotionEvent;import android.view.View;import android.widget.Toast;public class KeyDownActivity extends Activity {private static final int REFRESH = 0x00001;GameView mGameView;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);mGameView = new GameView(this);this.setContentView(mGameView);new Thread(new GameThread()).start();}@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubtoast("touch position: "+event.getX()+","+event.getY());this.finish();return super.onTouchEvent(event);}void toast(String text){Toast.makeText(KeyDownActivity.this, text, Toast.LENGTH_SHORT).show();}private class GameThread implements Runnable {@Overridepublic void run() {// TODO Auto-generated method stubwhile(!Thread.currentThread().isInterrupted()){try{Thread.sleep(400);} catch(Exception e){toast("GameThread error");Thread.currentThread().interrupt();}//使用postInvalidate可以直接在线程中刷新mGameView.postInvalidate();}}}private class GameThreadOld implements Runnable{@Overridepublic void run() {// TODO Auto-generated method stubwhile(!Thread.currentThread().isInterrupted()){try{Thread.sleep(200);} catch(Exception e){toast("GameThread error");Thread.currentThread().interrupt();}Message m = new Message();m.what = KeyDownActivity.REFRESH;KeyDownActivity.this.mHandler.sendMessage(m);}}}private Handler mHandler = new Handler(){@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubswitch(msg.what){case KeyDownActivity.REFRESH:/*invalidate不能直接在线程中刷新,因为它违反了单线程模型:Android的UI操作不是线程安全的,并且这些操作必须在UI线程中执行,因此Android最常用的方法就是利用Handler来实现UI线程的刷新。*/mGameView.invalidate();break;}super.handleMessage(msg);}};private class GameView extends View{int mCount = 0;int y = 100;public GameView(Context context) {super(context);// TODO Auto-generated constructor stub}@Overridepublic void draw(Canvas canvas) {// TODO Auto-generated method stubsuper.draw(canvas);if(mCount < 10)mCount++;elsemCount = 0;Paint mPaint = new Paint();switch(mCount % 4){case 0:mPaint.setColor(Color.RED);break;case 1:mPaint.setColor(Color.GREEN);break;case 2:mPaint.setColor(Color.BLUE);break;case 3:mPaint.setColor(Color.YELLOW);break;default:mPaint.setColor(Color.WHITE);break;}canvas.drawRect((480-80)/2, y, (480-80)/2+80, y+40, mPaint);}}}


原创粉丝点击