对安卓Matrix矩阵的理解

前提:矩阵是大学线性代数课程的教学内容,跟我一样已经还给老师的同学看本章前一定要去补充一下矩阵的基本知识。

具体知识点可以自行知乎/百度(矩阵原理、矩阵乘法规则、矩阵缩放原理、矩阵平移原理、单位矩阵、左乘、右乘...)

以上大概搞明白了,那就可以接着往下看了!

上代码!

一、preTranslate

Matrix A=new Matrix();
A.setScale(0.5f,0.5f);
A.preTranslate(10,10);

以上代码具体矩阵操作分步的含义:

1.new Matrix()

A=
\begin{matrix} 1&0&0\\ 0&1&0\\ 0&0&1\\ \end{matrix}
A为单位矩阵

2.A.setScale(0.5f,0.5f)

设置x/y轴缩放比例
A =
\begin{matrix} 0.5&0&0\\ 0&0.5&0\\ 0&0&1\\ \end{matrix}

3.A.preTranslate(10,10)

C =
\begin{matrix} 0&0&10\\ 0&0&10\\ 0&0&1\\ \end{matrix}
A = A * C =
\begin{matrix} 0.5&0&5\\ 0&0.5&5\\ 0&0&1\\ \end{matrix}

总结:preTranslate为左乘,A左乘C,记AxC

补充:

A.preTranslate(B)
A.preTranslate(C)

= A*B*C

二、postTranslate

Matrix A=new Matrix();
A.setScale(0.5f,0.5f);
A.postTranslate(10,10);

以上代码具体矩阵操作分步的含义:

1、2两步同上
3.A.postTranslate(10,10)

A = C * A =
\begin{matrix} 0.5&0&10\\ 0&0.5&10\\ 0&0&1\\ \end{matrix}

总结:postTranslate为右乘,A右乘C,记CxA

补充:

A.postTranslate(B)
A.postTranslate(C)

= C*B*A

三、setScale、postSkew

RectF rect1 = new RectF();
RectF rect2 = new RectF();

RectF rect = new RectF(400, 400, 1000, 800);
Matrix matrix = new Matrix();
matrix.setScale(0.5f, 1f);
matrix.mapRect(rect1, rect);
Log.i(TAG, "mapRadius1: "+rect.toString());

matrix.postSkew(1,0);
matrix.mapRect(rect2, rect);
Log.i(TAG, "mapRadius2: "+rect.toString());

mapRadius1: RectF(200.0, 400.0, 500.0, 800.0)
mapRadius2: RectF(600.0, 400.0, 1300.0, 800.0)
1.setScale(0.5f, 1f)计算同上
2.postSkew(1, 0)

A =

\begin{matrix} 1&1&0\\ 0&1&0\\ 0&0&1\\ \end{matrix}

x1y1 = (200, 400) ,写作数组为:

\begin{matrix} 200\\ 400\\ 1\\ \end{matrix}

新的x1y1坐标为 (600, 400)

A * x1y1 =

1 * 200 + 1 * 400 + 0*1 = 600

0 * 200 + 1 * 400 + 0*1 = 400

0 * 200 + 0 * 400 + 1*1 = 1

\begin{matrix} 600\\ 400\\ 1\\ \end{matrix}

参考博客:

1.https://github.com/GcsSloop/AndroidNote/blob/master/CustomView/Advance/%5B09%5DMatrix_Basic.md

2.https://blog.csdn.net/qq_21727627/article/details/104340349
3.https://blog.csdn.net/CAir2/article/details/106646680

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容