Android變形(Transform) 之 Matrix


引言

最近在研究Android的變形,Android的2D變形(包括縮放,扭曲,平移,旋轉等)可以通過Matrix來實現,3D變形可以通過Camera來實現。接下來就將我這倆天研究的東西和大家分享下,先來看看Matrix的用法。

 

效果圖

變形以后

 

 

Matrix矩陣

坐標變換矩陣,即一個3*3的矩陣,用來對圖形進行坐標變換。

圖1.1  A為坐標矩陣,C為原始矩陣,R是A和C矩陣相乘記過,那么可以知道:(矩陣知識,大學沒學好的傷不起啊)
x’ = a*x + b*y + c
y’ = d*x + b*y + f
最后一列很少有資料提到,不過初始值g=h=0,大家可以去改變值試試,變化為3D效果,但是值沒看出規律,那么i為縮放比例,初始值為1。
初始化坐標矩陣為{1,0,0,   0,1,0,   0,0,1}
 
上面講到的是基本的算法,那么具體這個矩陣x行x列的值代表上面呢,不防簡單的來看看

如果A={1,0,100,  0,1,-100,  0,0,2},那么可以算出來

x’ = x + 100;
y’ = y - 100;
也即在原始的基礎上右移100,上移100,單位為像素。第三列第三行為2,表示為以前比例的1/2,記住這塊容易弄錯。
下面給出具體坐標對應變形的屬性
|scaleX, skewX, translateX| 
|skewY, scaleY, translateY|
|0       ,0        , scale       |

 

實踐

通過代碼來看看具體的用法

 1  public  class  MatrixTransformView  extends  View {
 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的時候再糾結。

 

看代碼:

 1  public  class  MatrixTransformView  extends  View {
 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 }
Matrix的基本用法就這么多。
 
擴展
變形是需要canvas來進行繪制的,canvas的繪制需要bitmap,所以這塊利用一個繼承自View的控件,通過setDrawable方式設置bitmap,那么選擇目標必須是個bitmap,在文章的demo中,通過參數為int型resource的 setDrawable方法進行bitmap獲取,如果想對別的控件進行變形,例如ViewGroup,可以通過如下方式:
 1         Matrix m =  new Matrix();
 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);
通過將ViewGroup轉換成Bitmap,然后自定義一個Image來變形,隱藏ViewGroup來達到效果。
 
疑問
1.如果誰知道post,pre的區別,請告訴我下,看看我的理解是否正確。
2.能否實現ViewGroup直接變形,而非我上面講的那種。
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM