handler的使用(主要更新UI)

来源:互联网 发布:手机刷基带软件 编辑:程序博客网 时间:2024/05/20 10:20

handler使用Message

1.定义一个Handler

2.重写消息处理函数

3.发送消息

//创建简单的View
 
import android.content.Context;  
 
import android.graphics.Canvas;  
 
import android.graphics.Color;  
 
import android.graphics.Paint;  
 
import android.graphics.Point;  
 
import android.graphics.drawable.Drawable;  
 
import android.view.View;  
   
 
public class BounceView extends View {  
     
float x = 40;  
       
     
public BounceView(Context context) {  
         
super(context);  
     
}  
   
     
@Override  
     
protected void onDraw(Canvas canvas) {  
         x
+=10;  
         
Paint mPaint = new Paint();  
         mPaint
.setAntiAlias(true);  
         mPaint
.setColor(Color.GREEN);  
         canvas
.drawCircle(x, 40, 40, mPaint);  
     
}  
 
}  

//创建Activity
 
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.View;  
 
import android.view.Window;  
   
 
public class TestHandler extends Activity {  
     
protected static final int GUIUPDATEIDENTIFIER = 0x101;  
       
     
Thread myRefreshThread = null;  
     
BounceView myBounceView = null;  
     
//1.定义一个Handler(一般更新View)
     
Handler myHandler = new Handler() {  
           
//2.重写消息处理函数
           
public void handleMessage(Message msg) {  
               
switch (msg.what) {  
                     
//判断发送的消息
                     
case TestHandler.GUIUPDATEIDENTIFIER:  
                         
//更新View
                          myBounceView
.invalidate();  
                         
break;  
               
}  
               
super.handleMessage(msg);  
           
}  
     
};  
     
public void onCreate(Bundle savedInstanceState) {  
           
super.onCreate(savedInstanceState);  
           
this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
   
           
this.myBounceView = new BounceView(this);  
           
this.setContentView(this.myBounceView);  
           
new Thread(new myThread()).start();  
     
}  
   
     
class myThread implements Runnable {  
           
public void run() {  
               
while (!Thread.currentThread().isInterrupted()) {    
                     
//3.发送消息
                     
Message message = new Message();
                     
//发送消息与处理函数里一致  
                     message
.what = TestHandler.GUIUPDATEIDENTIFIER;  
                     
//内部类调用外部类的变量
                     
TestHandler.this.myHandler.sendMessage(message);  
 
                     
try {  
                         
Thread.sleep(100);    
                     
} catch (InterruptedException e) {  
                         
Thread.currentThread().interrupt();  
                     
}  
               
}  
           
}  
     
}  
 
}  

利用handler.post()更新UI

1.创建一个Handler 2.调用Handler.post(Runnable r)方法 3.Runnable运行在UI所在线程,所以可以直接调用View.invalidate()

 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.view.View;  
   
 
public class TestHandler extends Activity {  
     
private MyView myView;  
     
private Handler mHandler;  
     
public void onCreate(Bundle savedInstanceState) {  
         
super.onCreate(savedInstanceState);  
         myView
= new MyView(this);  
         
//创建一个Handler
         mHandler
= new Handler();
         
//调用Handler.post(Runnable r)方法
         mHandler
.post(new Runnable(){  
             
@Override  
             
public void run() {  
                 
//直接调用View.invalidate(),更新组件
                 myView
.invalidate();  
                 
//延迟5毫秒后执行线程
                 mHandler
.postDelayed(this, 5);  
             
}  
         
});  
         setContentView
(myView);  
     
}  
       
     
class MyView extends View{  
         
private float x = 0f;  
         
public MyView(Context context) {  
             
super(context);  
               
     
}  
         
protected void onDraw(Canvas canvas) {  
             
super.onDraw(canvas);  
             x
+=1;  
             
Paint mPaint = new Paint();  
             mPaint
.setColor(Color.BLUE);  
             canvas
.drawRect(x, 40, x+40, 80, mPaint);  
         
}  
           
     
}  
 
}  

在线程里直接更新UI

 //在新线程里更新UI,可以直接使用postInvalidate() 

 
public void onCreate(Bundle savedInstanceState) {      
       
super.onCreate(savedInstanceState);      
       
this.requestWindowFeature(Window.FEATURE_NO_TITLE);      
     
        myView
= new MyView(this);  
       
this.setContentView(this.myView);      
       
new Thread(new myThread()).start();    
 
}      
     
 
class myThread implements Runnable {      
       
public void run() {    
           
while (!Thread.currentThread().isInterrupted()) {      
             
try {  
                   
//更新UI
                    myView
.postInvalidate();  
                   
Thread.sleep(100);      
               
} catch (InterruptedException e) {      
                   
Thread.currentThread().interrupt();      
               
}      
           
}      
       
}      
 
}