android自定義view的重新整理
http://blog.csdn.net/ganlijianstyle/article/details/7490700
postInvalidate() ,Invalidate() 區別
postInvalidate() 用在子執行緒向UI執行緒傳送介面重新整理訊息請求,
invalidate()呼叫直接在UI執行緒重新整理介面。
第1種情況 postInvalidate();
package com.ganstyle.activity;import android.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Switch;
public class GameView extends View{
private Paint paint=null;
private int x;
public GameView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Style.STROKE);
Log.i("GameView", String.valueOf(Thread.currentThread().getId()));
this.setFocusable(true);
// TODO Auto-generated constructor stub
//Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.ic_lock_silent_mode);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawLine(0,0,400, 0, paint);
canvas.drawLine(x,0,x, 400, paint);
Log.i("onDraw", String.valueOf(Thread.currentThread().getId()));
}
MyThread t=null;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// while (x<300)
// {
// x++;
// GameView.this.postInvalidate();
// } 執行這個方法不會在主執行緒重繪,因為onKeyDown 在主線執行,
//由於迴圈時間過長,主線不會進重繪,所以要另一個執行緒 MyThread T 執行緒執行迴圈,
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_CENTER:
if(t==null||x<300)
{
t= new MyThread();
t.start();
}System.out.println("GameView "+ keyCode );
//設定返回鍵生效
case KeyEvent.KEYCODE_BACK:
return super.onKeyDown(keyCode, event);
default :
break;
}
//return super.onKeyDown(keyCode, event);
return true; //設定為true,表示不向下面的元件或View傳遞
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//event.getAction 得到三種事件 0 代表獲得焦點、1代表離開螢幕、2代表按著螢幕
System.out.println("GameView "+ event.getX()+" "+event.getY()+" event type "+event.getAction());
x =(int) event.getX();
//return super.onTouchEvent(event);
return true; //設定為true,表示不向下面的元件或View傳遞
}
private class MyThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
while (x<300)
{
x++;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("MyThread", String.valueOf(Thread.currentThread().getId()));
GameView.this.postInvalidate();
}}
}
}
第2種情況 invalidate(); 根據handler 收到的訊息,對介面進行重繪
package com.ganstyle.activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
public class HandleGameView extends View{
private Paint paint=null;
private int x;
private Handler handler = new Handler()
{
};@Override
public void handleMessage(Message msg) {
if(msg.what == 1)
{
invalidate();
}
Log.i("Handler", String.valueOf(Thread.currentThread().getId()));
}
public HandleGameView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Style.STROKE);
this.setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(0,0,400, 0, paint);
canvas.drawLine(x,0,x, 400, paint);
Log.i("onDraw", String.valueOf(Thread.currentThread().getId()));
}
MyThread t=null;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// while (x<300)
// {
// x++;
// GameView.this.postInvalidate();
// } 執行這個不會在主執行緒重繪
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_CENTER:
if(t==null||x<300)
{
t= new MyThread();
t.start();
}System.out.println("GameView "+ keyCode );
//設定 返回鍵 生效
case KeyEvent.KEYCODE_BACK:
return super.onKeyDown(keyCode, event);
default :
break;
}
//return super.onKeyDown(keyCode, event);
return true; //設定為true,表示不向下面的元件或View傳遞
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//event.getAction 得到三種事件 0 代表獲得焦點、1代表離開螢幕、2代表按著螢幕
System.out.println("GameView "+ event.getX()+" "+event.getY()+" event type "+event.getAction());
Log.i("onTouchEvent", String.valueOf(Thread.currentThread().getId()));
x =(int) event.getX();
//return super.onTouchEvent(event);
return true; //設定為true,表示不向下面的元件或View傳遞
}
private class MyThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
while (x<300)
{
x++;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("MyThread", String.valueOf(Thread.currentThread().getId()));
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}}
}
}