Matrix

来源:互联网 发布:云南广电网络总经理 编辑:程序博客网 时间:2024/04/30 03:15
 matrix.setScale(interpolatedTime, interpolatedTime);
 
      matrix.preTranslate(-centerX, -centerY);

       matrix.postTranslate(centerX, centerY);



经常在中心缩放的应用中看到这段代码.


preTranslate是指在setScale前,平移,postTranslate是指在setScale后平移

注意他们参数是平移的距离,而不是平移目的地的坐标!


由于缩放是以(0,0)为中心的,所以为了把界面的中心与(0,0)对齐,就要preTranslate(-centerX,-centerY),

setScale完成后,调用postTranslate(centerX,centerY),再把图片移回来,这样看到的动画效果就是activity的界面图片从中心不停的缩放了


注:centerX和centerY是界面中心的坐标

____________________________________________________________

java.lang.Object   ↳

 

android.graphics.Matrix

 

 

Public Constructors Matrix()
Create an identity matrix
 Matrix(Matrix src)
Create a matrix that is a (deep) copy of src

 

 

 

 

 

 


 

旋转

 

voidsetRotate(float degrees)
Set the matrix to rotate about (0,0) by the specified number of degrees.
voidsetRotate(float degrees, float px, float py)
Set the matrix to rotate by the specified number of degrees, with a pivot point at (px, py).

围绕点px, py 旋转 degrees度, 如果没设置坐标,默认以0,0点旋转.

例子: setRotate(45, 180, 120);


 

 

 

缩放,翻转

 

voidsetScale(float sx, float sy)
Set the matrix to scale by sx and sy.
voidsetScale(float sx, float sy, float px, float py)
Set the matrix to scale by sx and sy, with a pivot point at (px, py).

以点px,py为原点缩放 >=0   1为正常大小  

如果是负数,图形就会翻转

如果没设置原点坐标,默认以0,0点缩放(如果发现图片不见了,检查一下是不是翻转出了屏幕)

例子:setScale(-0.5f, 1,180, 120);  //左右翻转并缩放到一半大小


 

 

倾斜

 

voidsetSkew(float kx, float ky, float px, float py)
Set the matrix to skew by sx and sy, with a pivot point at (px, py).
voidsetSkew(float kx, float ky)
Set the matrix to skew by sx and sy.

以点px,py为原点倾斜如果没有设置原点,则以0,0点为原点.

例子:setSkew(0, 1, 180, 120); //Y 方向拉伸


 

坐标

voidsetTranslate(float dx, float dy)
Set the matrix to translate by (dx, dy).

是图片移动到某一个位置

 

 

 

注意

Matrix中带有pre, post的函数需要考虑先后顺序

例如:想要旋转45度,然后平移到100,100的位置需要

 

 

Java代码  收藏代码
  1. Matrix matrix = new Matrix();  
  2. matrix.postRotate(45);  
  3. matrix.postTranslate(100100);    

 

或者

 

Java代码  收藏代码
  1. Matrix matrix = new Matrix();  
  2. matrix.setTranslate(100100);  
  3. matrix.preRotate(45);  

 

 

这就要考虑到矩阵的前乘和后乘了,不然的话你会发现可能坐标位置不是你想要的,可能图像都不见了.

如果在复杂一些,需要选择,缩放,倾斜同时起作用,并且还要设置坐标到屏幕指定位置你会发现很麻烦,需要自己计算出各个方法的参数,然后考虑调用的先后顺序.

 

但这里有一种更简便的方法,叫系统帮我们计算

 

booleansetConcat(Matrix a, Matrix b)
Set the matrix to the concatenation of the two specified matrices, returning true if the the result can be represented.

这个方法的意思是帮我们把两个 Matrix对象计算并连接起来.

这样的话我们就可以这样使用了

 

 

Java代码  收藏代码
  1. Matrix mRotateMatrix = new Matrix();    //控制旋转  
  2. Matrix mScaleMatrix = new Matrix(); //控制缩放  
  3. Matrix mSkewMatrix = new Matrix();  //控制倾斜  
  4. Matrix mPosMatrix = new Matrix();       //控制坐标  
  5. Matrix mMatrix = new Matrix();      //合并  
  6.       
  7. mMatrix.setConcat(mRotateMatrix, mScaleMatrix);  
  8. mMatrix.setConcat(mMatrix, mSkewMatrix);  
  9. mMatrix.setConcat(mMatrix, mPosMatrix);  
  10.       
  11. canvas.drawBitmap(mBitmap, mMatrix, mPaint);  
 

注意:合并的第一步不能直接用mMatrix自身去连接其他的Matrix,我试过几次结果图像又飞了,大家再试试

 

例子:

同时设置

setRotate(45, 180, 120);

setScale(-0.5f, 1,180, 120);  //左右翻转并缩放到一半大小

setSkew(0, 1, 180, 120);      //Y 方向拉伸


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


0 0
原创粉丝点击