双缓冲
package com.example.examples_05_12;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class GameView extends View implements Runnable {
//创建Bitmap
Bitmap mBitmap;
Paint paint=null;
//创建一个双缓冲区
Bitmap mSCBitmap=null;
//创建Canvas
Canvas mCanvas=null;
public GameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
//装载资源
mBitmap=((BitmapDrawable)getResources().getDrawable(R.drawable.qq)).getBitmap();
//创建屏幕大小的缓冲区
mSCBitmap=Bitmap.createBitmap(320, 480, Config.ARGB_8888);
//创建Canvas
mCanvas=new Canvas();
//设置将内容绘制在mSCBitmap上
mCanvas.setBitmap(mSCBitmap);
paint=new Paint();
//将mBitmap绘制到mSCBitmap上
mCanvas.drawBitmap(mBitmap, 0, 0, paint);
new Thread(this).start();
}
public void run() {
// TODO Auto-generated method stub
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception
Thread.currentThread().interrupt();
}
postInvalidate();
}
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制到屏幕上
canvas.drawBitmap(mSCBitmap, 0, 0, paint);
}
}
package com.example.examples_05_12;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GameView(MainActivity.this));
}
public boolean onTouchEvent(MotionEvent event) {
//获得x和y坐标,并弹出消息框
Toast.makeText(MainActivity.this, event.getRawX()+"+"+event.getRawY(), Toast.LENGTH_SHORT).show();
return true;
}
}