Android:UI更新方法二:View.postInvalidate+Thread+Runnable

  1. package com.example.updateui;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.util.AttributeSet;  
  8. import android.util.Log;  
  9. import android.view.View;  
  10.   
  11. public class MyView extends View  
  12. {  
  13.     private static final String TAG = MyView.class.getSimpleName();  
  14.     private int mCount = 0;  
  15.   
  16.     public MyView(Context context)  
  17.     {  
  18.         super(context);  
  19.         // TODO Auto-generated constructor stub  
  20.     }  
  21.     public MyView(Context context,AttributeSet attrSet)  
  22.     {  
  23.         super(context,attrSet);  
  24.         // TODO Auto-generated constructor stub  
  25.     }  
  26.     @Override  
  27.     public void onDraw(Canvas canvas)  
  28.     {  
  29.         Log.i(TAG,"onDraw");  
  30.         if (mCount < 100)  
  31.         {  
  32.             mCount++;  
  33.         }  
  34.         else  
  35.         {  
  36.             mCount = 0;  
  37.         }  
  38.         // 绘图  
  39.         Paint paint = new Paint();  
  40.         switch (mCount % 4)  
  41.         {  
  42.         case 0:  
  43.             paint.setColor(Color.BLACK);  
  44.             break;  
  45.         case 1:  
  46.             paint.setColor(Color.RED);  
  47.             break;  
  48.         case 2:  
  49.             paint.setColor(Color.BLUE);  
  50.             break;  
  51.         case 3:  
  52.             paint.setColor(Color.YELLOW);  
  53.             break;  
  54.         }  
  55.         // 绘图  
  56.         canvas.drawRect(this.getLeft(), this.getTop(), this.getRight(),  
  57.                 this.getBottom(), paint);  
  58.         Log.i(TAG,"drawRect count:"+mCount);  
  59.     }  
  60. }  


    1. package com.example.updateui;  
    2.   
    3. import android.os.Bundle;  
    4. import android.os.Handler;  
    5. import android.os.Message;  
    6. import android.app.Activity;  
    7. import android.view.Menu;  
    8. import android.view.Window;  
    9.   
    10. public class MainActivity extends Activity  
    11. {  
    12.     private MyView mView;  
    13.   
    14.     @Override  
    15.     protected void onCreate(Bundle savedInstanceState)  
    16.     {  
    17.         super.onCreate(savedInstanceState);  
    18.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
    19.         mView = new MyView(this);  
    20.         setContentView(mView);//需要绑定到window,invalidate有效  
    21.   
    22.         new Thread(new Runnable()  
    23.         {  
    24.   
    25.             @Override  
    26.             public void run()  
    27.             {  
    28.                 // TODO Auto-generated method stub  
    29.                 while (!Thread.currentThread().isInterrupted())  
    30.                 {  
    31.                     try  
    32.                     {  
    33.                         Thread.sleep(1000);  
    34.                     }  
    35.                     catch (InterruptedException e)  
    36.                     {  
    37.                         Thread.currentThread().interrupt();  
    38.                     }  
    39.                     mView.postInvalidate();//postInvalidate可以在工作线程直接刷新UI  
    40.                 }  
    41.             }  
    42.   
    43.         }).start();  
    44.         // setContentView(R.layout.activity_main);  
    45.     }  
    46.   
    47.       
    48.   
    49.     @Override  
    50.     public boolean onCreateOptionsMenu(Menu menu)  
    51.     {  
    52.         // Inflate the menu; this adds items to the action bar if it is present.  
    53.         getMenuInflater().inflate(R.menu.activity_main, menu);  
    54.         return true;  
    55.     }  
    56.   
posted @ 2016-11-19 15:14  天涯海角路  阅读(106)  评论(0)    收藏  举报