引言
最近在研究Android的變形,Android的2D變形(包括縮放,扭曲,平移,旋轉等)可以通過Matrix來實現,3D變形可以通過Camera來實現。接下來就將我這倆天研究的東西和大家分享下,先來看看Matrix的用法。
效果圖
變形以后
Matrix矩陣
坐標變換矩陣,即一個3*3的矩陣,用來對圖形進行坐標變換。
如果A={1,0,100, 0,1,-100, 0,0,2},那么可以算出來
實踐
通過代碼來看看具體的用法
2
3 private Matrix mMatrix;
4 private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
5 private Bitmap mBitmap;
6
7 public MatrixTransformView(Context context) {
8 super (context);
9 }
10
11 public MatrixTransformView(Context context, AttributeSet attrs) {
12 super (context, attrs);
13 }
14
15 public void setDrawable( int resId) {
16 mBitmap = BitmapFactory.decodeResource(getContext().getResources(), resId);
17 }
18
19 /*
20 * 設置矩陣,並重繪
21 */
22 public void setMatrixValues( float [] array) {
23 if (mMatrix == null ) {
24 mMatrix = new Matrix();
25 }
26 mMatrix.reset();
27 mMatrix.setValues(array);
28 invalidate();
29 }
30
31 public void resetMatrix() {
32 if (mMatrix != null ) {
33 mMatrix.reset();
34 }
35 invalidate();
36 }
37
38 @Override
39 protected void onDraw(Canvas canvas) {
40 if (mMatrix != null ) {
41 Paint paint = mPaint;
42 canvas.drawBitmap(mBitmap, mMatrix, paint);
43 }
44
45 super .onDraw(canvas);
46 }
47 }
通過Matrix的setValues方法,將3*3的矩陣坐標值進行設置即可。
強調的一點是,在調用setMatrixValues的時候需要調用invalidate方法,讓View進行調用onDraw進行重繪。
矩陣的基本用法就是這些,往往在開發過程中,不直接通過矩陣坐標去實現變形,因為如果要實現選擇,那么就比較復雜了,涉及到三角函數,對於數據早已經忘差不多的人,很是痛苦,當然如果非要用的話,算起來也不難。
那么為了避免直接使用矩陣坐標來操作變形,Matrix類提供方法來進行變:
set方式:setScale, setSkew, setTranslate, setRotate
post方式:postScale, postSkew, postTranslate, postRotate
pre方式:preScale, preSkew, preTranslate, preRotate
set方式為直接設置,每一次調用set方法都會先重置矩陣。post可以理解成設置多次有效,效果是累加的。pre這里暫且理解成和post方式完全一樣,后面3D的時候再糾結。
看代碼:
2
3 private Matrix mMatrix;
4 private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
5 private Bitmap mBitmap;
6
7 public MatrixTransformView(Context context) {
8 super (context);
9 }
10
11 public MatrixTransformView(Context context, AttributeSet attrs) {
12 super (context, attrs);
13 }
14
15 public void setDrawable( int resId) {
16 mBitmap = BitmapFactory.decodeResource(getContext().getResources(), resId);
17 }
18
19 /*
20 * 設置矩陣,並重繪
21 */
22 public void setMatrixValues( float [] array) {
23 if (mMatrix == null ) {
24 mMatrix = new Matrix();
25 }
26 mMatrix.reset();
27 mMatrix.setValues(array);
28 invalidate();
29 }
30
31 public void postMatrixScale( float scaleX, float scaleY, float centerX, float centerY) {
32 if (mMatrix == null ) {
33 mMatrix = new Matrix();
34 }
35 mMatrix.preScale(scaleX, scaleY, centerX, centerY);
36 invalidate();
37 }
38
39 public void postMatrixSkew( float skewX, float skewY, float centerX, float centerY) {
40 if (mMatrix == null ) {
41 mMatrix = new Matrix();
42 }
43 mMatrix.postSkew(skewX, skewY, centerX, centerY);
44 invalidate();
45 }
46
47 public void postMatrixTranslate( float translateX, float translateY) {
48 if (mMatrix == null ) {
49 mMatrix = new Matrix();
50 }
51 mMatrix.postTranslate(translateX, translateY);
52 invalidate();
53 }
54
55 public void postMatrixRotate( float degree, float centerX, float centerY) {
56 if (mMatrix == null ) {
57 mMatrix = new Matrix();
58 }
59 mMatrix.postRotate(degree, centerX, centerY);
60 invalidate();
61 }
62
63 public void resetMatrix() {
64 if (mMatrix != null ) {
65 mMatrix.reset();
66 }
67 invalidate();
68 }
69
70 @Override
71 protected void onDraw(Canvas canvas) {
72 if (mMatrix != null ) {
73 Paint paint = mPaint;
74 canvas.drawBitmap(mBitmap, mMatrix, paint);
75 }
76
77 super .onDraw(canvas);
78 }
79 }
2 m.setValues( new float[] {
3 1, 0, 0,
4 0, 1, 0,
5 0, 0, 1
6 });
7 Bitmap bp = Bitmap.createBitmap(viewGroup.getWidth(), viewGroup.getHeight(), Bitmap.Config.RGB_565);
8 Canvas can = new Canvas(bp);
9 viewGroup.draw(can);
10 bp = Bitmap.createBitmap(bp, 0, 0, bp.getWidth(), bp.getHeight(), m, true);
11 img.setImageBitmap(bp);