Bitmap位圖渲染與操做(圖片移動,放大,縮小,旋轉,鏡像發轉)--android遊戲開發

位圖操做主要有2中方式:android

1.使用canvas 畫布操做: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.經過矩陣操做位圖:app

   // ----------旋轉位圖(方式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);ide

案例界面:函數

案例源碼:post

  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)」網站

相關文章
相關標籤/搜索