android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用。 
以下是我在android文档中找到的说明, 
引用
public void invalidate() 
Invalidate the whole view. If the view is visible, onDraw(Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate(). 

public void postInvalidate () 
Cause an invalidate to happen on a subsequent cycle through the event loop. Use this to invalidate the View from a non-UI thread. 

google的文档的说明实在是简单,往往看了开发中都会遇到这两个问题: 
1. 没有任何异常,view没能刷新。 
2. android应用异常终止,打开logcat会看到这样的异常信息, Only the original thread that created a view hierarchy can touch its views。 

最后,通过查文档,上网查询才知道,invalidate和postInvalidate方法需要使用android提供的handler,才能实现重绘,而在文档的说明中却只字不提,真是简单啊。具体是在需要重绘的地方调用handler的sendMessage方法发送消息,紧接着会os会触发handler中的handlerMessage方法,在handlerMessage方法中再调用view的invalidate或者postInvalidate方法就能实现重绘。 

下面是我分别针对invalidate方法,给出view重绘代码,仅供参考: 
Java代码  收藏代码
  1. class CustomizeView extends WhichView {  
  2.   
  3.     public CustomizeView(Context context) {  
  4.         super(context);  
  5.         final Handler handler = new Handler();  
  6.   
  7.         new Thread(new Runnable() {  
  8.             @Override  
  9.             public void run() {  
  10.                 // delay some minutes you desire.  
  11.                 /*try { 
  12.                     Thread.sleep(3000); 
  13.                 } catch (InterruptedException e) { 
  14.                 }*/  
  15.                 handler.post(new Runnable() {  
  16.                     public void run() {  
  17.                         concreteUpdateUI();  
  18.                         invalidate();  
  19.                     }  
  20.                 });  
  21.             }  
  22.         }).start();  
  23.     }  
  24.   
  25.     protected void concreteUpdateUI() {  
  26.         // Add concrete movement for UI updates.  
  27.         // ...  
  28.     }  
  29. }  

或者这样实现也可以。 
Java代码  收藏代码
  1. class CustomizeView extends TextView {  
  2.   
  3.     public CustomizeView(Context context) {  
  4.         super(context);  
  5.         new Thread(new UIUpdateThread()).start();  
  6.     }  
  7.   
  8.     class UIUpdateThread implements Runnable {  
  9.         final Handler mHandler = new Handler();  
  10.   
  11.         final Runnable mUpdateResults = new Runnable() {  
  12.             public void run() {  
  13.                 concreteUpdateUI();  
  14.                 invalidate();  
  15.             }  
  16.         };  
  17.   
  18.         public void run() {  
  19.             // delay some minutes you desire.   
  20.             /*try { 
  21.                 Thread.sleep(1000 * 5); 
  22.             } catch (InterruptedException e) { 
  23.                 e.printStackTrace(); 
  24.             }*/  
  25.             mHandler.post(mUpdateResults);  
  26.         }  
  27.   
  28.     }  
  29.   
  30.     protected void concreteUpdateUI() {  
  31.         // Add concrete movement for UI updates.    
  32.         // ...    
  33.     }  
  34.   
  35. }  

更多相关文章

  1. Android 解决setRequestedOrientation之后手机屏幕的旋转不触发o
  2. 面试例题6:两种方法将图像显示在View上
  3. Android NDK报错(Eclipse)及解决方法
  4. Android 更新UI的两个方法
  5. Android 日志系统logcat内核代码分析
  6. 编写android jni代码时遇到的问题
  7. 收藏Android下bitmap内存限制OUT OF MEMORY的方法
  8. Android 代码自动提示功能
  9. 【Android】Android SDK下载和更新失败的解决方法!!!

随机推荐

  1. Android RecyclerView 的瀑布流式布局
  2. Android Android Studio添加MPChart
  3. Android(安卓)绑定远程服务出现 Not Allo
  4. 如何查看Android 中native的Service
  5. Android(安卓)Theme
  6. [AndroidTips]Android软件测试的日志文件
  7. Ubuntu下批处理转换jpg 2 png格式
  8. AIDL实例分析和讲解
  9. 翻译引用 android的按键响应
  10. 一招破解Android SDk 更新问题