android游戏开发学习笔记四(学习书籍 Android游戏编程之从零开始)
/**
*位图
*
*@author岳振华
*
*/
@SuppressWarnings("unused")
publicclassBitMapSrufaceViewextendsSurfaceViewimplementsCallback,Runnable{
privateSurfaceHoldersfh;
privateThreadthread;
privateintsurfaceW,surfaceH;
privatePaintpaint;
privateCanvascanvas;
privatebooleanflag;//surfaceView是否消毁标志位
privateBitmapbitmap;//位图图像
publicBitMapSrufaceView(Contextcontext){
super(context);
/**********画图所必需*********************/
sfh=this.getHolder();//获得surfaceHolder对象
sfh.addCallback(this);
thread=newThread(this);
paint=newPaint();//新建画笔
paint.setColor(Color.WHITE);//设置画笔颜色
paint.setAntiAlias(true);//消除锯齿
this.setKeepScreenOn(true);//保持屏幕常亮
this.setFocusable(true);
/********************************************/
bitmap=BitmapFactory.decodeResource(this.getResources(),R.drawable.test);
}
/**
*开始动画
*/
@Override
publicvoidstartAnimation(Animationanimation){
super.startAnimation(animation);
}
/****************************callback的几个方法********************/
/**
*当Surface第一次创建后会立即调用该函数一般情况下都是在另外的线程来绘制界面,所以不要在这个函数中绘制Surface
*/
@Override
publicvoidsurfaceCreated(SurfaceHolderholder){
surfaceW=this.getWidth();
surfaceH=this.getHeight();
thread=newThread(this);
flag=true;
thread.start();//开启线程
}
/**
*当Surface的状态(大小和格式)发生变化的时候会调用该函数在surfaceCreated调用后该函数至少会被调用一次
*/
@Override
publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,intheight){
}
@Override
publicvoidsurfaceDestroyed(SurfaceHolderholder){
flag=false;
}
/****************************callback的几个方法********************/
/**
*绘图方法
*/
privatevoiddraw(){
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();Matrixmatrix=newMatrix();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轴镜像
/*MatrixmaMiX=newMatrix();
maMiX.postTranslate(100,100);*/
//Y轴镜像
/*MatrixmaMiX=newMatrix();
maMiX.postTranslate(100,100);*/
/**********************************************************/
}
}catch(Exceptione){
e.printStackTrace();
}finally{
if(null!=canvas){
sfh.unlockCanvasAndPost(canvas);//提交画布
}
}
}
@SuppressWarnings("static-access")
@Override
publicvoidrun(){
while(flag){
//绘图
draw();
try{
thread.sleep(100);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
}