Android游戏开发基础part4--Bitmap位图的渲染与操作
游戏开发基础part4--Bitmap位图的渲染与操作
知识点1:Bitmap与BitmapFactory
如果想通过一张图片资源文件创建一个位图,则要通过位图工厂来索引图片资源文件,从而生成一张位图实例
BitmapFactory.decodeResource(Resources res,int Id)
作用:通过资源文件生成一张位图
第一个参数:资源实例
第二个参数:资源ID
实例:Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),R.drawable.icon);
知识点2:绘制位图
drawBitmap(Bitmap bitmap,float left,float top,Paint paint)
作用:在画布上绘制一张位图
第一个参数:位图实例
第二、三个参数:位图的X,Y坐标
第四个参数:画笔实例
实例:canvas.drawBitmap(bmp,0,0,paint);
知识点3:旋转位图
rotate(float degrees,float px,float py)
作用:旋转位图
第一个参数:画布旋转的角度
第二、三个参数:画布的旋转点
如果旋转的角度大于0,顺时针旋转,反则逆时针旋转
实例:canvas.rotate(30,bmp.getWidth()/2,bmp.getHeight()/2);
canvas.drawBitmap(bmp,0,0,paint);
知识点4:对画布的状态的操作
save(): 作用是用于保存当前画布的状态;
restore():作用是恢复上次保存的画布状态;
针对一张位图进行旋转的操作:
首先用画布的save()函数将其画布的状态保存起来,然后进行旋转,紧接着绘制位图,最后当位图绘制完后将画布的状态恢复restore()。
实例:canvas.save();
canvas.rotate(30,bmp.getWidth()/2,bmp.getHeight()/2);
canvas.drawBitmap(bmp,0,0,paint);
canvas.restore();
canvas.drawBitmap(bmp,100,0,paint);
知识点5:平移位图
translate(float dx, float dy)
作用:平移画布
第一个参数:在X轴上平移画布距离
第二个参数:在Y轴上平移画布距离
实例:canvas.save();
canvas.translate(10,10);
canvas.drawBitmap(bmp,0,0,paint)
canvas.restore();
知识点6:缩放位图
scale(float sx,float sy,float px,float py)
作用:对画布进行缩放
第一个参数:对画布X轴的缩放比例
第二个参数:对画布Y周到缩放比例
第三、四个参数:对画布缩放的起始点
当缩放比例为1时表示画布没有进行缩放;当比例值大于1时表示放大;当比例值小于1且大于0时表示缩小。
实例:canvas.save();
canvas.scale(2f,2f,50 + bmp.getWidth()/2,50 + bmp.getHeight()/2);
canvas.drawBitmap(bmp,50,50,paint);
canvas.restore();
canvas.drawBitmap(bmp,50,50,paint);
知识点7:镜像反转位图
X轴镜像操作:设置X轴的缩放比例为-1
canvas.scale(-1,1,100 + bmp.getWidth() /2,100 + bmp.getHieght()/2);
Y轴镜像操作:设置Y周的缩放比例为-1
canvas.scale(1,-1,100 + bmp.getWidth() /2 , 100 + bmp.getHeight() /2);
创建项目实例:BitmapProject
项目源代码:
==>MySurfaceView.java
package com.bitmap; 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.view.KeyEvent; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; public class MySurfaceView extends SurfaceView implements Callback,Runnable{ //用于控制SurfaceView private SurfaceHolder sfh; //声明一个画笔 private Paint paint; //声明一个线程 private Thread th; //线程消亡的标识位 private boolean flag; //声明一个画布 private Canvas canvas; //声明屏幕的宽高 private int screenW,screenH; //声明位图对象 private Bitmap bmp; public MySurfaceView(Context context){ super(context); //实例SurfaceHolder sfh = this.getHolder(); //为SurfaceView添加状态监听 sfh.addCallback(this); //实例一个画笔 paint = new Paint(); //设置画笔颜色为白色 paint.setColor(Color.WHITE); //设置焦点 setFocusable(true); bmp =BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher); } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub screenW = this.getWidth(); screenH = this.getHeight(); flag = true; //实例线程 th = new Thread(this); //启动线程 th.start(); } /** * 游戏绘图 * */ public void myDraw(){ try { canvas = sfh.lockCanvas(); if(canvas != null){ canvas.drawColor(Color.BLACK); //----绘制位图 //canvas.drawBitmap(bmp, 0,0, paint); //----旋转位图 //canvas.save(); //canvas.rotate(30, bmp.getWidth()/2, bmp.getHeight()/2); //canvas.drawBitmap(bmp, 0,0, paint); //canvas.restore(); //canvas.drawBitmap(bmp, 100, 0, paint); //利用矩阵Matrix实现上面的效果 //Matrix mx = new Matrix(); //mx.postRotate(30,bmp.getWidth()/2,bmp.getHeight()/2); //canvas.drawBitmap(bmp, mx, paint); //----平移位图 //canvas.save(); //canvas.translate(20, 20); //canvas.drawBitmap(bmp, 0, 0, paint); //canvas.restore(); //----利用矩阵对位图平移 //Matrix maT = new Matrix(); //maT.postTranslate(20,20); //canvas.drawBitmap(bmp, maT, paint); //----缩放位图 //canvas.save(); //canvas.scale(2f, 2f, 50 + bmp.getWidth()/2, // 50 + bmp.getHeight()/2); //canvas.drawBitmap(bmp, 50, 50, paint); //canvas.restore(); //canvas.drawBitmap(bmp,50,50,paint); //----利用矩阵对位图进行缩放 //Matrix maS = new Matrix(); //maS.postTranslate(50, 50); //maS.postScale(2f, 2f, 50 + bmp.getWidth()/2, bmp.getHeight()/2); //canvas.drawBitmap(bmp, maS, paint); //canvas.drawBitmap(bmp, 50, 50, paint); /*X轴镜像 canvas.save(); canvas.scale(-1, 1, bmp.getWidth()/2, bmp.getHeight()/2); canvas.drawBitmap(bmp, 0,0, paint); canvas.restore(); canvas.drawBitmap(bmp, 100, 100, paint); */ /*Y轴镜像 canvas.save(); canvas.scale(1, -1, bmp.getWidth()/2, bmp.getHeight()/2); canvas.drawBitmap(bmp, 0, 0, paint); canvas.restore(); canvas.drawBitmap(bmp, 100,100, paint); */ } } catch (Exception e){ } finally{ if(canvas !=null) sfh.unlockCanvasAndPost(canvas); } } /** * 按键事件监听 * */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub return super.onKeyDown(keyCode, event); } /** * 触屏事件监听 * */ @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub return true; } /** * 游戏逻辑 * */ private void logic(){ } /** *运行线程 * */ @Override public void run() { // TODO Auto-generated method stub while(flag){ long start = System.currentTimeMillis(); myDraw(); logic(); long end = System.currentTimeMillis(); try{ if(end - start <50){ Thread.sleep(50 - (end - start)); } } catch (InterruptedException e){ e.printStackTrace(); } } } /** * SurfaceView视图状态发生变化,响应此函数 */ @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } /** * SurfaceView视图状态消亡时,响应此函数 */ @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub flag = false; } }