/**
* 位图
*
* @author 岳振华
*
*/
@SuppressWarnings("unused")
public class BitMapSrufaceView extends SurfaceView implements Callback, Runnable {
private SurfaceHolder sfh;
private Thread thread;
private int surfaceW, surfaceH;
private Paint paint;
private Canvas canvas;
private boolean flag;// surfaceView是否消毁标志位
private Bitmap bitmap;// 位图图像
public BitMapSrufaceView(Context context) {
super(context);
/********** 画图所必需 *********************/
sfh = this.getHolder();// 获得surfaceHolder对象
sfh.addCallback(this);
thread = new Thread(this);
paint = new Paint();// 新建画笔
paint.setColor(Color.WHITE);// 设置画笔颜色
paint.setAntiAlias(true);// 消除锯齿
this.setKeepScreenOn(true);// 保持屏幕常亮
this.setFocusable(true);
/********************************************/
bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.test);
}
/**
* 开始动画
*/
@Override
public void startAnimation(Animation animation) {
super.startAnimation(animation);
}
/**************************** callback的几个方法 ********************/
/**
* 当Surface第一次创建后会立即调用该函数 一般情况下都是在另外的线程来绘制界面,所以不要在这个函数中绘制Surface
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
surfaceW = this.getWidth();
surfaceH = this.getHeight();
thread = new Thread(this);
flag = true;
thread.start();// 开启线程
}
/**
* 当Surface的状态(大小和格式)发生变化的时候会调用 该函数 在surfaceCreated调用后该函数至少会被调用一次
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
}
/**************************** callback的几个方法 ********************/
/**
* 绘图方法
*/
private void draw() {
try {
// 获得canvas实例
canvas = sfh.lockCanvas();
if (canvas != null) {
canvas.drawColor(Color.BLACK);// 填充,刷屏
// 绘制位图
// canvas.drawBitmap(bitmap,10,10,paint);
// 缩放位图
/*
* canvas.save(); canvas.scale( 0.1f, 0.1f); canvas.drawBitmap(bitmap, 10, 10, paint);
*/
// 恢复缩放
/*
* canvas.restore(); canvas.scale( 0.1f, 0.1f, 50 + bitmap.getWidth()/1000, 50 + bitmap.getHeight()/1000); canvas.drawBitmap(bitmap, 30, 30, paint);
*/
// 图片太大先缩小下
canvas.scale(0.1f, 0.1f, 50 + bitmap.getWidth() / 1000, 50 + bitmap.getHeight() / 1000);
// 旋转位图
/*
* canvas.save(); canvas.rotate(30); canvas.drawBitmap(bitmap, 10, 10, paint); canvas.restore(); canvas.drawBitmap(bitmap, 10, 400, paint);
*/
// 平移方式一
/*
* canvas.save(); canvas.translate(10, 10); canvas.drawBitmap(bitmap, 10, 10, paint); canvas.restore(); canvas.drawBitmap(bitmap, 100, 400, paint);
*/
// 平移方式二
/*
* canvas.save(); Matrix matrix=new Matrix(); matrix.postTranslate(0, 200); canvas.drawBitmap(bitmap, matrix, paint); canvas.restore(); canvas.drawBitmap(bitmap, 100, 400, paint);
*/
/*********************** 镜像反转位图 *****************/
// X轴镜像
canvas.drawBitmap(bitmap, 10, 10, paint);
canvas.save();
canvas.scale(-1, 1, bitmap.getWidth(), bitmap.getHeight());
canvas.drawBitmap(bitmap, 0, 200, paint);
canvas.restore();
// Y轴镜像
canvas.drawBitmap(bitmap, 10, 10, paint);
canvas.save();
canvas.scale(1, -1, bitmap.getWidth(), bitmap.getHeight());
canvas.drawBitmap(bitmap, 300, 0, paint);
canvas.restore();
//方式二
// X轴镜像
/*Matrix maMiX = new Matrix();
maMiX.postTranslate(100, 100);*/
// Y轴镜像
/*Matrix maMiX = new Matrix();
maMiX.postTranslate(100, 100);*/
/**********************************************************/
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != canvas) {
sfh.unlockCanvasAndPost(canvas);// 提交画布
}
}
}
@SuppressWarnings("static-access")
@Override
public void run() {
while (flag) {
// 绘图
draw();
try {
thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}