package com.j2men.tank;import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.util.Log; import android.view.View;public class Tank_02 extends View implements Runnable{ Thread thread = new Thread(); Resources r = this.getResources(); Bitmap bm = BitmapFactory.decodeResource(r, R.drawable.tank_right03); int x=10; Paint p = new Paint(); public Tank_02(Context context) { super(context); // TODO Auto-generated constructor stub this.thread.start(); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub canvas.drawBitmap(bm, x, 150, p); } public void run() { // TODO Auto-generated method stub while(true){ x++; Log.v("sdsd","fsdfs"); try { Thread.sleep(1000); } catch (InterruptedException e) {e.printStackTrace(); } this.postInvalidate(); } } } 大家给审查下哪写的不对
There are some problems that arise when using postInvalidate from other threads (like not having the UI updated right-away), this will be more efficient:runOnUiThread(new Runnable() { public void run() { myImageView.setImageBitmap(image); imageView.invalidate(); } });
invalidate()
//实例化一个Handler对象,并重写handleMessage方法调用invalidate()实现界面刷新,在线程中通过sendMessage发送界面更新消息,
方法二:
postInvalidate()
//直接调用
postInvalidate() 非主线程调用更新。
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;public class Tank_02 extends View implements Runnable{ Thread thread = new Thread();
Resources r = this.getResources();
Bitmap bm = BitmapFactory.decodeResource(r, R.drawable.tank_right03);
int x=10;
Paint p = new Paint();
public Tank_02(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.thread.start();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub canvas.drawBitmap(bm, x, 150, p); }
public void run() {
// TODO Auto-generated method stub
while(true){
x++;
Log.v("sdsd","fsdfs");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {e.printStackTrace();
}
this.postInvalidate();
}
}
}
大家给审查下哪写的不对
public void run() {
myImageView.setImageBitmap(image);
imageView.invalidate();
}
});