Bitmap位图渲染与操作(图片移动,放大,缩小,旋转,镜像发转)--android游戏开发

位图操作主要有2中方式:

1.使用canvas 画布操作:

canvas.drawColor(Color.BLACK);

                // ----------旋转位图(方式1)
                canvas.save();
                canvas.rotate(30, bmp.getWidth() / 2, bmp.getHeight() / 2);// 旋转弧度,旋转中心点x,旋转中心点y
                canvas.drawBitmap(bmp, bmp.getWidth() / 2 + 20,bmp.getHeight() + 10, paint);
                canvas.restore();

                // ----------平移位图(方式1)
                canvas.save();
                canvas.translate(100, 0);// 平移坐标X,Y
                canvas.drawBitmap(bmp, 0, 2 * bmp.getHeight() + 10, paint);
                canvas.restore();

                // ----------缩放位图(方式1)
                canvas.save();
                canvas.scale(2f, 2f, 50 + bmp.getWidth() / 2,50 + bmp.getHeight() / 2);// X方向缩放比例,Y方向缩放比例,缩放中心X,缩放中心Y
                canvas.drawBitmap(bmp, 150, 3 * bmp.getHeight() + 10, paint);
                canvas.restore();

                // ----------镜像反转位图(方式1)
                // X轴镜像
                canvas.save();
                canvas.scale(-1, 1, 100 + bmp.getWidth() / 2,100 + bmp.getHeight() / 2);// scale()当第一个参数为负数时表示x方向镜像反转,同理第二参数,镜像反转x,镜像反转Y
                canvas.drawBitmap(bmp, 0, 4 * bmp.getHeight() + 10, paint);
                canvas.restore();
                // Y轴镜像
                canvas.save();
                canvas.scale(1, -1, 100 + bmp.getWidth() / 2,100 + bmp.getHeight() / 2);
                canvas.drawBitmap(bmp, 4 * bmp.getHeight(),bmp.getWidth() + 10, paint);
                canvas.restore();
2.通过矩阵操作位图:

   // ----------旋转位图(方式2)--通过矩阵旋转
                Matrix mx = new Matrix();
                mx.postRotate(60, bmp.getWidth() / 2, bmp.getHeight() / 2);// 旋转弧度,旋转中心点x,旋转中心点y
                canvas.drawBitmap(bmp, mx, paint);

                // ----------平移位图(方式2)--通过矩阵
                Matrix maT = new Matrix();
                maT.postTranslate(2 * bmp.getWidth(), 0);// 平移坐标X,Y
                canvas.drawBitmap(bmp, maT, paint);

                // ----------镜像反转位图(方式2)
                // X轴镜像
                Matrix maMiX = new Matrix();
                maMiX.postTranslate(0, 2 * bmp.getHeight());
                maMiX.postScale(-1, 1, 100 + bmp.getWidth() / 2,100 + bmp.getHeight() / 2);
                canvas.drawBitmap(bmp, maMiX, paint);

                // Y轴镜像
                Matrix maMiY = new Matrix();
                maMiY.postScale(1, -1, 100 + bmp.getWidth() / 2,100 + bmp.getHeight() / 2);
                canvas.drawBitmap(bmp, maMiY, paint);

                // ----------缩放位图(方式2)-放大
                Matrix maS = new Matrix();
                maS.postTranslate(200, 2 * bmp.getHeight());
                maS.postScale(2f, 2f, 50 + bmp.getWidth() / 2,50 + bmp.getHeight() / 2);// X方向缩放比例,Y方向缩放比例,缩放中心X,缩放中心Y
                canvas.drawBitmap(bmp, maS, paint);
                // ----------缩放位图(方式2)-缩小
                Matrix maS1 = new Matrix();
                maS1.postTranslate(0, 2 * bmp.getHeight());
                maS1.postScale(0.5f, 0.5f, 50 + bmp.getWidth() / 2,50 + bmp.getHeight() / 2);// X方向缩放比例,Y方向缩放比例,缩放中心X,缩放中心Y
                canvas.drawBitmap(bmp, maS1, paint);

案例界面:

案例源码:

  1 package caicai.animation;
  2 
  3 import android.content.Context;
  4 import android.graphics.Bitmap;
  5 import android.graphics.BitmapFactory;
  6 import android.graphics.Canvas;
  7 import android.graphics.Color;
  8 import android.graphics.Matrix;
  9 import android.graphics.Paint;
 10 import android.graphics.PaintFlagsDrawFilter;
 11 import android.graphics.Path;
 12 import android.graphics.Rect;
 13 import android.graphics.RectF;
 14 import android.util.Log;
 15 import android.view.MotionEvent;
 16 import android.view.SurfaceHolder;
 17 import android.view.SurfaceHolder.Callback;
 18 import android.view.SurfaceView;
 19 
 20 public class bitmapView extends SurfaceView implements Callback, Runnable {
 21     private Paint paint;// 画布
 22     private SurfaceHolder sfh; // 用于控制SurfaceView
 23     private Canvas canvas;// 画布
 24     private boolean flag;// 关闭线程标志
 25     private Thread th;// 新建线程
 26     private Bitmap bmp;// 位图
 27 
 28     // 设置画布绘图无锯齿
 29     private PaintFlagsDrawFilter pfd = new PaintFlagsDrawFilter(0,
 30             Paint.ANTI_ALIAS_FLAG);
 31 
 32     public bitmapView(Context context) {
 33         super(context);
 34         sfh = this.getHolder();// 实例SurfaceHolder
 35         sfh.addCallback(this);// 为SurfaceView添加状态监听
 36         paint = new Paint();// 实例一个画笔
 37         paint.setColor(Color.WHITE);// 设置画笔颜色为白色
 38         setFocusable(true);// 设置焦点
 39         bmp = BitmapFactory.decodeResource(getResources(), R.drawable.tanke);
 40     }
 41 
 42     @Override
 43     public void surfaceCreated(SurfaceHolder holder) {
 44         flag = true;
 45         // 实例线程
 46         th = new Thread(this);
 47         // 启动线程
 48         th.start();
 49     }
 50 
 51     public void mydraw() {
 52         try {
 53             canvas = sfh.lockCanvas();// 锁定画布
 54             if (canvas != null) {
 55                 canvas.drawColor(Color.BLACK);
 56 
 57                 // ----------旋转位图(方式1)
 58                 canvas.save();
 59                 canvas.rotate(30, bmp.getWidth() / 2, bmp.getHeight() / 2);// 旋转弧度,旋转中心点x,旋转中心点y
 60                 canvas.drawBitmap(bmp, bmp.getWidth() / 2 + 20,bmp.getHeight() + 10, paint);
 61                 canvas.restore();
 62 
 63                 // ----------平移位图(方式1)
 64                 canvas.save();
 65                 canvas.translate(100, 0);// 平移坐标X,Y
 66                 canvas.drawBitmap(bmp, 0, 2 * bmp.getHeight() + 10, paint);
 67                 canvas.restore();
 68 
 69                 // ----------缩放位图(方式1)
 70                 canvas.save();
 71                 canvas.scale(2f, 2f, 50 + bmp.getWidth() / 2,50 + bmp.getHeight() / 2);// X方向缩放比例,Y方向缩放比例,缩放中心X,缩放中心Y
 72                 canvas.drawBitmap(bmp, 150, 3 * bmp.getHeight() + 10, paint);
 73                 canvas.restore();
 74 
 75                 // ----------镜像反转位图(方式1)
 76                 // X轴镜像
 77                 canvas.save();
 78                 canvas.scale(-1, 1, 100 + bmp.getWidth() / 2,100 + bmp.getHeight() / 2);// scale()当第一个参数为负数时表示x方向镜像反转,同理第二参数,镜像反转x,镜像反转Y
 79                 canvas.drawBitmap(bmp, 0, 4 * bmp.getHeight() + 10, paint);
 80                 canvas.restore();
 81                 // Y轴镜像
 82                 canvas.save();
 83                 canvas.scale(1, -1, 100 + bmp.getWidth() / 2,100 + bmp.getHeight() / 2);
 84                 canvas.drawBitmap(bmp, 4 * bmp.getHeight(),bmp.getWidth() + 10, paint);
 85                 canvas.restore();
 86 
 87                 // ----------旋转位图(方式2)--通过矩阵旋转
 88                 Matrix mx = new Matrix();
 89                 mx.postRotate(60, bmp.getWidth() / 2, bmp.getHeight() / 2);// 旋转弧度,旋转中心点x,旋转中心点y
 90                 canvas.drawBitmap(bmp, mx, paint);
 91 
 92                 // ----------平移位图(方式2)--通过矩阵
 93                 Matrix maT = new Matrix();
 94                 maT.postTranslate(2 * bmp.getWidth(), 0);// 平移坐标X,Y
 95                 canvas.drawBitmap(bmp, maT, paint);
 96 
 97                 // ----------镜像反转位图(方式2)
 98                 // X轴镜像
 99                 Matrix maMiX = new Matrix();
100                 maMiX.postTranslate(0, 2 * bmp.getHeight());
101                 maMiX.postScale(-1, 1, 100 + bmp.getWidth() / 2,100 + bmp.getHeight() / 2);
102                 canvas.drawBitmap(bmp, maMiX, paint);
103 
104                 // Y轴镜像
105                 Matrix maMiY = new Matrix();
106                 maMiY.postScale(1, -1, 100 + bmp.getWidth() / 2,100 + bmp.getHeight() / 2);
107                 canvas.drawBitmap(bmp, maMiY, paint);
108 
109                 // ----------缩放位图(方式2)-放大
110                 Matrix maS = new Matrix();
111                 maS.postTranslate(200, 2 * bmp.getHeight());
112                 maS.postScale(2f, 2f, 50 + bmp.getWidth() / 2,50 + bmp.getHeight() / 2);// X方向缩放比例,Y方向缩放比例,缩放中心X,缩放中心Y
113                 canvas.drawBitmap(bmp, maS, paint);
114                 // ----------缩放位图(方式2)-缩小
115                 Matrix maS1 = new Matrix();
116                 maS1.postTranslate(0, 2 * bmp.getHeight());
117                 maS1.postScale(0.5f, 0.5f, 50 + bmp.getWidth() / 2,50 + bmp.getHeight() / 2);// X方向缩放比例,Y方向缩放比例,缩放中心X,缩放中心Y
118                 canvas.drawBitmap(bmp, maS1, paint);
119             } else {
120                 Log.i("tt", "获取不到画布");// 释放画布
121             }
122         } catch (Exception e) {
123 
124         } finally {
125             if (canvas != null)
126                 sfh.unlockCanvasAndPost(canvas);
127         }
128     }
129 
130     /**
131      * SurfaceView视图状态发生改变,响应此函数
132      */
133     @Override
134     public void surfaceChanged(SurfaceHolder holder, int format, int width,
135             int height) {
136         // TODO Auto-generated method stub
137 
138     }
139 
140     /**
141      * SurfaceView视图消亡时,响应此函数
142      */
143     @Override
144     public void surfaceDestroyed(SurfaceHolder holder) {
145         flag = false;// 结束游戏,设置线程关闭标志为false
146 
147     }
148 
149     public void logic() {
150 
151     };
152 
153     @Override
154     public void run() {
155         while (flag) {
156             long start = System.currentTimeMillis();
157             mydraw();
158             logic();
159             long end = System.currentTimeMillis();
160             try {
161                 if (end - start < 50) {
162                     Thread.sleep(50 - (end - start));
163                 }
164             } catch (InterruptedException e) {
165                 e.printStackTrace();
166             }
167         }
168     }
169 
170     // 获取点击坐标
171     @Override
172     public boolean onTouchEvent(MotionEvent event) {
173         return true;
174     }
175 
176 }
bitmapView
 1 package caicai.animation;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.Window;
 6 import android.view.WindowManager;
 7 
 8 public class MainActivity extends Activity {
 9     /** Called when the activity is first created. */
10     @Override
11     public void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         //设置全屏
14         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
15         requestWindowFeature(Window.FEATURE_NO_TITLE);
16         //显示自定义的SurfaceView视图
17         setContentView(new bitmapView(this));
18 
19     }
20 }
MainActivity

谢谢支持网站“趣淘鼓浪屿(www.qtgly.com)”

原文地址:https://www.cnblogs.com/clarence/p/3285192.html