最近在研究android图形动画时,用到了Matrix和Camera,找了好多资料,感觉有个哥们讲的特别详细,挺不错的,以下是链接:http://blog.youkuaiyun.com/pathuang68/article/details/6991867;学习了一下,做个总结。
Matrix是一个三行三列的矩阵,它可以对图像做四种基本变化:平移,旋转,缩放,倾斜,所做的例子是对一张300×255的图片,做各种动画效果,图片的width=300,height=255。通过matrix.getValues()可以获得对应的矩阵
1.Translate 平移:
平移所涉及到的方法有:setTranslate(float dx, float dy);preTranslate(float dx, float dy);postTranslate(float dx, float dy)。
setTranslate(width,height),通过getValues()获得的矩阵是
1.0 0.0 300.0
0.0 1.0 225.0
0.0 0.0 1.0
由此可见我们所传过去的值就是增量
2.Rotate 旋转:
旋转所涉及到的方法:setRotate(float degrees, float px, float py);setRotate(float degrees) ; preRotate(float degrees, float px, float py);preRotate(float degrees) ;postRotate(float degrees, float px, float py);postRotate(float degrees)。
其中三个参数的是围绕某个点旋转:
如果是围绕点顺时针旋转
,那么可以用矩阵表示为:
可以化为:
0.70710677,-0.70710677,123.4835
0.70710677,0.70710677,-73.115524
0.0, 0.0, 1.0
θ就是45f,Xp就是width/2,Yp就是height/2
其中一个参数的是围绕坐标原点旋转:
setRotate(45f):围绕坐标原点旋转45度角,通过getValues()获得的矩阵是:
0.70710677 ,-0.70710677, 0.0,
0.70710677 ,0.70710677, 0.0,
0.0, 0.0, 1.0,
θ就是45f