/*
软件作者:https://xuhss.com/oxox/pro
*/
一、简介
Android android.graphics.Matrix 类是一个3 x 3的矩阵(方阵),上一张几乎所有介绍Matrix的文章都会引用的Matrix内容图:
android.graphics.Matrix.png
Matrix使用非常广泛,平时我们使用的补间动画、图像变换、画布的变换、大名鼎鼎的MPAndroidChart图表库等都使用了Matrix。在平时的开发当中,Matrix的使用有时可以起到事半功倍的效果。
二、相关方法
1、equals
比较两个矩阵是否相等。
Matrix matrix1 = new Matrix();
Matrix matrix2 = new Matrix();
matrix1.setTranslate(1,2);
matrix2.setTranslate(2,2);
// 输出:matrix1 == matrix2:false
System.out.println("matrix1 == matrix2:" + matrix1.equals(matrix2));
2、+号相连/toString/toShortString
将矩阵转换为字符串。
Matrix matrix = new Matrix();
// 输出:+号相连:Matrix{[1.0, 0.0, 0.0][0.0, 1.0, 0.0][0.0, 0.0, 1.0]}
System.out.println("+号相连:" + matrix);
// 输出:Matrix{[1.0, 0.0, 0.0][0.0, 1.0, 0.0][0.0, 0.0, 1.0]}
System.out.println("toString:" + matrix.toString());
// 输出:[1.0, 0.0, 0.0][0.0, 1.0, 0.0][0.0, 0.0, 1.0]
System.out.println("toShortString:" + matrix.toShortString());
3、getValues()、setValues()
当我们调用Matrix类的getValues(float[] values)、setValues(float[] values)方法时,可以将这个矩阵转换成一个数组进行操作。转换后的数组为:
[ MSCALE_X, MSKEW_X, MTRANS_X, MSKEW_Y, MSCALE_Y, MTRANS_Y, MPERSP_0, MPERSP_1, MPERSP_2]
为了方便操作这个数组,在android.graphics.Matrix类中,定义了MSCALE_X、MSKEW_X...变量,分别代表各自功能在数组中对应的下标,具体内容如下:
public static final int MSCALE_X = 0; //!< use with getValues/setValues
public static final int MSKEW_X = 1; //!< use with getValues/setValues
public static final int MTRANS_X = 2; //!< use with getValues/setValues
public static final int MSKEW_Y = 3; //!< use with getValues/setValues
public static final int MSCALE_Y = 4; //!< use with getValues/setValues
public static final int MTRANS_Y = 5; //!< use with getValues/setValues
public static final int MPERSP_0 = 6; //!< use with getValues/setValues
public static final int MPERSP_1 = 7; //!< use with getValues/setValues
public static final int MPERSP_2 = 8; //!< use with getValues/setValues
方法示例:
Matrix matrix = new Matrix();
// matrix = [1.0, 0.0, 0.0][0.0, 1.0, 0.0][0.0, 0.0, 1.0]
System.out.println("matrix = " + matrix.toShortString());
float[] values = new float[9];
matrix.getValues(values);
// matrix转换成数组后 = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
System.out.println("matrix转换成数组后 = " + Arrays.toString(values));
// 为matrix赋值
values [Matrix.MTRANS_X] = 2;
values [Matrix.MTRANS_Y] = 3;
matrix.setValues(values);
// matrix = [1.0, 0.0, 2.0][0.0, 1.0, 3.0][0.0, 0.0, 1.0]
System.out.println("matrix = " + matrix.toShortString());
4、setXXX/preXXX/postXXX
XXX可以是Translate、Scale、Rotate、Skew和Concat。其中Concat参数为Matrix,表示直接操作Matrix。由于缩放、旋转、错切可以绕中心操作,如果指定了中心,则变换步骤为:
将原点平移到该点。
做缩放、错切、旋转操作。
原点平移到原来的原点处。
方法参数转换成了一个怎样的矩阵?
// 下面代码中参数(2,2) 转换后的矩阵为
// [2.0, 0.0, 0.0]
// [0.0, 2.0, 0.0]
// [0.0, 0.0, 1.0]
// 即根据XXX代表的功能修改矩阵中对应功能位置的值即可
matrix.postScale(2,2);
setXXX
首先会将该Matrix重置为单位矩阵,即相当于首先会调用reset()方法,然后再设置该Matrix中对应功能的值。例:
// [1.0, 0.0, 0.0]
// [0.0, 1.0, 0.0]
// [0.0, 0.0, 1.0]
Matrix matrix = new Matrix();
// [1.0, 0.0, 0.0] [2.0, 3.0, 4.0]
// [0.0, 1.0, 0.0] -> [2.0, 0.0, 0.0]
// [0.0, 0.0, 1.0] [1.0, 1.0, 1.0]
matrix.setValues(new float[]{2.0f,3.0f, 4.0f,
2.0f,0.0f, 0.0f,
1.0f,1.0f,1.0f});
// [2.0, 3.0, 4.0] [1.0, 0.0, 0.0] [2.0, 0.0, 0.0]
// [2.0, 2.0, 0.0] -> [0.0, 1.0, 0.0] -> [0.0, 2.0, 0.0]
// [1.0, 1.0, 1.0] [0.0, 0.0, 1.0] [0.0, 0.0, 1.0]
matrix.setScale(2,2);
preXXX
不会重置Matrix,相当于当前操作矩阵(A)左乘参数矩阵(B),即AB。例:
// [1.0, 0.0, 0.0]
// [0.0, 1.0, 0.0]
// [0.0, 0.0, 1.0]
Matrix matrix = new Matrix();
// [1.0, 0.0, 0.0] [2.0, 3.0, 4.0]
// [0.0, 1.0, 0.0] -> [2.0, 0.0, 0.0]
// [0.0, 0.0, 1.0] [1.0, 1.0, 1.0]
matrix.setValues(new float[]{2.0f,3.0f, 4.0f,
2.0f,0.0f, 0.0f,
1.0f,1.0f,1.0f});
// [2.0, 3.0, 4.0] [2.0, 0.0, 0.0] [4.0, 6.0, 4.0]
// [2.0, 0.0, 0.0](matrix) * [0.0, 2.0, 0.0] = [4.0, 0.0, 0.0](matrix)
// [1.0, 1.0, 1.0] [0.0, 0.0, 1.0] [2.0, 2.0, 1.0]
matrix.preScale(2,2);
postXXX
不会重置Matrix,相当于当前操作矩阵(A)右乘参数矩阵(B),即BA,例:
// [1.0, 0.0, 0.0]
// [0.0, 1.0, 0.0]
// [0.0, 0.0, 1.0]
Matrix matrix = new Matrix();
// [1.0, 0.0, 0.0] [2.0, 3.0, 4.0]
// [0.0, 1.0, 0.0] -> [2.0, 0.0, 0.0]
// [0.0, 0.0, 1.0] [1.0, 1.0, 1.0]
matrix.setValues(new float[]{2.0f,3.0f, 4.0f,
2.0f,0.0f, 0.0f,
1.0f,1.0f,1.0f});
// [2.0, 0.0, 0.0] [2.0, 3.0, 4.0] [4.0, 6.0, 8.0]
// [0.0, 2.0, 0.0] * [2.0, 0.0, 0.0] (matrix) = [4.0, 0.0, 0.0](matrix)
// [0.0, 0.0, 1.0] [1.0, 1.0, 1.0] [1.0, 1.0, 1.0]
matrix.postScale(2,2);
setContact
关于setContact(Matrix m1,Matrix m2)方法,需要单独说下,它的参数为两个Matrix对象,计算规则为:当前操作的Matrix对象 = m1 * m2;
例:
// [1.0, 0.0, 0.0]
// [0.0, 1.0, 0.0]
// [0.0, 0.0, 1.0]
Matrix matrix = new Matrix();
Matrix matrix1 = new Matrix();
Matrix matrix2 = new Matrix();
// [1.0, 0.0, 0.0] [2.0, 3.0, 4.0]
// [0.0, 1.0, 0.0] -> [2.0, 0.0, 0.0]
// [0.0, 0.0, 1.0] [1.0, 1.0, 1.0]
matrix1.setValues(new float[]{2.0f,3.0f, 4.0f,
2.0f,0.0f, 0.0f,
1.0f,1.0f,1.0f});
// [1.0, 0.0, 0.0] [2.0, 5.0, 4.0]
// [0.0, 1.0, 0.0] -> [3.0, 0.0, 0.0]
// [0.0, 0.0, 1.0] [1.0, 2.0, 1.0]
matrix2.setValues(new float[]{2.0f,5.0f, 4.0f,
3.0f,0.0f, 0.0f,
1.0f,2.0f,1.0f});
// [2.0, 3.0, 4.0] [2.0, 5.0, 4.0] [17.0, 18.0, 12.0]
// [2.0, 2.0, 0.0](matrix1) * [3.0, 0.0, 0.0](matrix2) = [4.0, 10.0, 8.0 ] (matrix)
// [1.0, 1.0, 1.0] [1.0, 2.0, 1.0] [6.0, 7.0, 5.0 ]
matrix.setConcat(matrix1,matrix2);
5、mapRadius/mapPoints/mapRect/mapVectors
可翻译为将矩阵映射到(作用于)点、矩形、半径、向量。
mapRadius
半径的计算。例:
// 一个半径为100.0f的圆,放大1倍后,半径也将增大一倍。据说用在画布中的圆随画布大小变化时
float radius = 100.0f;
float radiusAfterMatrix;
Matrix matrixRadius = new Matrix();
matrixRadius.setScale(2,2);
radiusAfterMatrix = matrixRadius.mapRadius(radius);
// 输出:radius=200.0
System.out.println("radius=" + radiusAfterMatrix);
mapPoints
此方法有3个重载方法。点数组各值分别代表pts[x0,y0,x1,y1 ... xn,yn],因为一个点的确定需要x坐标和y坐标两个值�
没有合适的资源?快使用搜索试试~ 我知道了~
AutoJs源码-教程-矩阵
共1个文件
js:1个
需积分: 1 0 下载量 163 浏览量
2022-11-15
22:54:57
上传
评论
收藏 5KB 7Z 举报
温馨提示
AutoJs源码-教程_矩阵。本资源购买前提醒:本源码都是实际autojs项目模板,安装好autojs直接运行即可打开。1、支持低版本autojs。2、资源仅供学习与参考,请勿用于商业用途,否则产生的一切后果将由您自己承担!。3、安装过程详见具体资源,不会使用autojs谨慎下载
资源推荐
资源详情
资源评论
收起资源包目录
AutoJs源码-教程_矩阵.7z (1个子文件)
AutoJs源码-教程_矩阵.js 17KB
共 1 条
- 1
资源评论
[虚幻私塾】
- 粉丝: 332
- 资源: 1559
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功