zoukankan      html  css  js  c++  java
  • Android UI 的更新

    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

  • 相关阅读:
    如何使用plsql导出oracle中的建表语句文件
    启动Tomcat 7.0 报 Server Tomcat v7.0 was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor.
    Spring + springMvc + MyBatis 整合步骤(收藏)
    同台电脑部署多组Tomcat负载均衡(或集群)
    Apache+Tomcat部署负载均衡(或集群)
    WebLogic部署集群和代理服务器
    iOS视频直播初窥:高仿<喵播APP>
    iOS高仿:花田小憩3.0.1
    Preprocessor Macros
    App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. 解决方案
  • 原文地址:https://www.cnblogs.com/tt_mc/p/1938248.html
Copyright © 2011-2022 走看看