Chinaunix首页 | 论坛 | 博客
  • 博客访问: 378222
  • 博文数量: 117
  • 博客积分: 2530
  • 博客等级: 少校
  • 技术积分: 1262
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-11 08:56
文章分类

全部博文(117)

文章存档

2015年(8)

2011年(2)

2010年(2)

2009年(16)

2008年(27)

2007年(42)

2006年(20)

我的朋友

分类: Android平台

2015-04-27 09:15:48

 Android中的Matrix,以及set,pre和post的区别

http://blog.csdn.net/gaojinshan/article/details/17334181

Matrix主要用于对平面进行平移(Translate),缩放(Scale),旋转(Rotate)以及斜切(Skew)操作。
为简化矩阵变换,Android封装了一系列方法来进行矩阵变换;其中包括:
set系列方法:setTranslate,setScale,setRotate,setSkew;设置,会覆盖之前的参数。
pre系列方法:preTranslate,preScale,preRotate,preSkew;矩阵先乘,如M' = M * T(dx, dy)。
post系列方法:postTranslate,postScale,postRotate,postSkew;矩阵后乘,如M' = T(dx, dy) * M。
通过将变换矩阵与原始矩阵相乘来达到变换的目的,例如:
平移(x'=x+tx;y'=y+ty):

缩放(x'=sx*x;y'=sy*y):

旋转(x'=cosβ*x-sinβ*y;y'=sinβ*x+cosβ*y):

选择需要用到如下的三角函数的公式:
①sin(α+β)=sinαcosβ+cosαsinβ
②cos(α+β)=cosαcosβ-sinαsinβ
公式①可以由单位圆方法或托勒密定理推导出来。
推导过程参见:http://blog.sina.com.cn/s/blog_58260f420100c03j.html

斜切(x'=x+k1*y;y'=k2*x+y):



点击(此处)折叠或打开

  1. //源码文件:external\skia\legacy\src\core\SkMatrix.cpp

  2. #define SK_Scalar1 (1.0f)
  3. #define kMatrix22Elem SK_Scalar1
  4. typedef float SkScalar;
  5. #define SkScalarMul(a, b) ((float)(a) * (b))

  6. enum {
  7.     kMScaleX, kMSkewX, kMTransX,
  8.     kMSkewY, kMScaleY, kMTransY,
  9.     kMPersp0, kMPersp1, kMPersp2
  10. };

  11. void SkMatrix::reset() {
  12.     fMat[kMScaleX] = fMat[kMScaleY] = SK_Scalar1; //其值为1
  13.     fMat[kMSkewX] = fMat[kMSkewY] =
  14.     fMat[kMTransX] = fMat[kMTransY] =
  15.     fMat[kMPersp0] = fMat[kMPersp1] = 0; //其值,全为0
  16.     fMat[kMPersp2] = kMatrix22Elem; //其值为1
  17.     this->setTypeMask(kIdentity_Mask | kRectStaysRect_Mask);
  18. }

  19. void SkMatrix::setTranslate(SkScalar dx, SkScalar dy) {
  20.     if (SkScalarToCompareType(dx) || SkScalarToCompareType(dy)) {
  21.         fMat[kMTransX] = dx; //以新值dx覆盖原值,原值无效了
  22.         fMat[kMTransY] = dy;

  23.         fMat[kMScaleX] = fMat[kMScaleY] = SK_Scalar1; //其值为1
  24.         fMat[kMSkewX] = fMat[kMSkewY] =
  25.         fMat[kMPersp0] = fMat[kMPersp1] = 0; //其值,全为0
  26.         fMat[kMPersp2] = kMatrix22Elem; //其值为1

  27.         this->setTypeMask(kTranslate_Mask | kRectStaysRect_Mask);
  28.     } else {
  29.         this->reset();
  30.     }
  31. }

  32. bool SkMatrix::preTranslate(SkScalar dx, SkScalar dy) {
  33.     if (this->hasPerspective()) {
  34.         SkMatrix m;
  35.         m.setTranslate(dx, dy);
  36.         return this->preConcat(m); //矩阵的先乘运算
  37.     }
  38.     
  39.     if (SkScalarToCompareType(dx) || SkScalarToCompareType(dy)) {
  40.         fMat[kMTransX] += SkScalarMul(fMat[kMScaleX], dx) +
  41.                           SkScalarMul(fMat[kMSkewX], dy); //先乘,需要矩阵运算过
  42.         fMat[kMTransY] += SkScalarMul(fMat[kMSkewY], dx) +
  43.                           SkScalarMul(fMat[kMScaleY], dy);

  44.         this->setTypeMask(kUnknown_Mask | kOnlyPerspectiveValid_Mask);
  45.     }
  46.     return true;
  47. }

  48. bool SkMatrix::postTranslate(SkScalar dx, SkScalar dy) {
  49.     if (this->hasPerspective()) {
  50.         SkMatrix m;
  51.         m.setTranslate(dx, dy);
  52.         return this->postConcat(m); //矩阵的后乘运算
  53.     }
  54.     
  55.     if (SkScalarToCompareType(dx) || SkScalarToCompareType(dy)) {
  56.         fMat[kMTransX] += dx; //后乘,直接加新值dx即可
  57.         fMat[kMTransY] += dy;
  58.         this->setTypeMask(kUnknown_Mask | kOnlyPerspectiveValid_Mask);
  59.     }
  60.     return true;
  61. }

  62. bool SkMatrix::preConcat(const SkMatrix& mat) { //矩阵的先乘运算(this在前)
  63.     // check for identity first, so we don't do a needless copy of ourselves
  64.     // to ourselves inside setConcat()
  65.     return mat.isIdentity() || this->setConcat(*this, mat); //矩阵运算
  66. }

  67. bool SkMatrix::postConcat(const SkMatrix& mat) { //矩阵的后乘运算(this在后)
  68.     // check for identity first, so we don't do a needless copy of ourselves
  69.     // to ourselves inside setConcat()
  70.     return mat.isIdentity() || this->setConcat(mat, *this); //矩阵运算
  71. }


阅读(1323) | 评论(0) | 转发(0) |
0

上一篇:云计算摘录

下一篇:Happy Number

给主人留下些什么吧!~~