package com.example.updateui;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.util.Log;import android.view.View;public class MyView extends View{private static final String TAG = MyView.class.getSimpleName();private int mCount = 0;public MyView(Context context){super(context);// TODO Auto-generated constructor stub}public MyView(Context context,AttributeSet attrSet){super(context,attrSet);// TODO Auto-generated constructor stub}@Overridepublic void onDraw(Canvas canvas){Log.i(TAG,"onDraw");if (mCount < 100){mCount++;}else{mCount = 0;}// 绘图Paint paint = new Paint();switch (mCount % 4){case 0:paint.setColor(Color.BLACK);break;case 1:paint.setColor(Color.RED);break;case 2:paint.setColor(Color.BLUE);break;case 3:paint.setColor(Color.YELLOW);break;}// 绘图canvas.drawRect(this.getLeft(), this.getTop(), this.getRight(),this.getBottom(), paint);Log.i(TAG,"drawRect count:"+mCount);}}


package com.example.updateui;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.Menu;import android.view.Window;public class MainActivity extends Activity{private MyView mView;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);this.requestWindowFeature(Window.FEATURE_NO_TITLE);mView = new MyView(this);setContentView(mView);//需要绑定到window,invalidate有效new Thread(new Runnable(){@Overridepublic void run(){// TODO Auto-generated method stubwhile (!Thread.currentThread().isInterrupted()){try{Thread.sleep(1000);}catch (InterruptedException e){Thread.currentThread().interrupt();}mView.postInvalidate();//postInvalidate可以在工作线程直接刷新UI}}}).start();// setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}


更多相关文章

  1. 多线程下载测试TestDownload
  2. JNI|在子线程中获得JNIEnv|AttachCurrentThread
  3. Glide 下载多张图片
  4. 83 Android(安卓)Handler ProgressBar 进度条
  5. Android(安卓)ContentResolver 使用例子,读取联系人
  6. Android(安卓)照片墙功能实现
  7. 在子线程中更新UI(后台服务)
  8. 开源项目Universal Image Loader for Android(安卓)说明文档 (1)
  9. Android(安卓)AsyncTask 解析

随机推荐

  1. Android 他人面试题总结
  2. android 城市列表
  3. Android中的开机和关机广播
  4. android运行后台服务de生命周期yu使用技
  5. Android(安卓)O上获取Adaptive Icon的Bit
  6. eclipse无法导入Android工程的解决办法
  7. android service 学习(上)
  8. 利用drozer进行Android渗透测试
  9. (转)Android(安卓)TextView背景色、圆角
  10. android_camera