Android UI 的更新

來源:互聯網
上載者:User

http://rayleung.javaeye.com/blog/435147

調用Handler.post(Runnable r)方法,Runnable運行在UI所線上程,所以可以直接調用View.invalidate()

 1 package com.Test.androidtest;  
2  
3  import android.app.Activity;  
4  import android.content.Context;  
5  import android.graphics.Canvas;  
6  import android.graphics.Color;  
7  import android.graphics.Paint;  
8  import android.os.Bundle;  
9  import android.os.Handler;  
10 import android.view.View;  
11  
12 public class TestHandler extends Activity {  
13     private MyView myView;  
14     private Handler mHandler;  
15     public void onCreate(Bundle savedInstanceState) {  
16         super.onCreate(savedInstanceState);  
17         myView = new MyView(this);  
18         mHandler = new Handler();  
19         mHandler.post(new Runnable(){  
20             @Override 
21             public void run() {  
22                 myView.invalidate();  
23                 mHandler.postDelayed(this, 5);  
24             }  
25          });  
26         setContentView(myView);  
27     }  
28       
29     class MyView extends View{  
30         private float x = 0f;  
31         public MyView(Context context) {  
32             super(context);  
33               
34     }  
35         protected void onDraw(Canvas canvas) {  
36             super.onDraw(canvas);  
37             x+=1;  
38             Paint mPaint = new Paint();  
39             mPaint.setColor(Color.BLUE);  
40             canvas.drawRect(x, 40, x+40, 80, mPaint);  
41         }  
42           
43     }  
44 } 
45

 

在新線程裡更新UI,可以直接postInvalidate()

 

1 public void onCreate(Bundle savedInstanceState) {      
2                super.onCreate(savedInstanceState);      
3                this.requestWindowFeature(Window.FEATURE_NO_TITLE);      
4      
5                myView = new MyView(this);  
6        this.setContentView(this.myView);      
7        new Thread(new myThread()).start();     
8 }      
9     
10     class myThread implements Runnable {      
11           public void run() {     
12               while (!Thread.currentThread().isInterrupted()) {      
13                    try {  
14                           myView.postInvalidate();   
15                         Thread.sleep(100);       
16                    } catch (InterruptedException e) {      
17                         Thread.currentThread().interrupt();      
18                    }      
19                }      
20           }      
21     }  
22

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.