0
点赞
收藏
分享

微信扫一扫

android开发的Matrix的mapPoints方法是如何计算的呢?


在android的自定义View中,使用Matrix来转换坐标非常方便,其中有一个方法叫mapPoints,它的计算方式是怎么样的呢?

首先来看看Matrix矩阵长得什么样?它是一个3x3的矩阵。

android开发的Matrix的mapPoints方法是如何计算的呢?_数组

在编程里,它对应一个长度为9的float[]数组,通过以下变量准确取到数组中相应的值。

android开发的Matrix的mapPoints方法是如何计算的呢?_数组_02

现在再来看看,每一位的含意:

  • MSCALE_X 与 MSCALE_Y:x坐标和y坐标的缩放
  • MSKEW_X 与 MSKEW_Y:x坐标和y坐标的斜切(就是偏斜)
  • MTRANS_X 与 MTRANS_Y:x坐标和y坐标的平移
  • MPERSP_0、MPERSP_1和MPERSP_2:控制透视

好了,我们现在来看一下,mapPoints的计算过程:

// 初始化 matrix矩阵
val values = floatArrayOf(
2f, 3f, 10f,
4f, 5f, 30f,
0f, 0f, 1f
)
val matrix = Matrix()
matrix.setValues(values)

// 待转换的点(1,2)
val pointsSrc = floatArrayOf(1f,2f)
// 存放转换后的结果
val pointsDst = FloatArray(2)
// 调用mapPoints方法转换数据
matrix.mapPoints(pointsDst,pointsSrc) // pointsDst [18.0, 44.0]

// 以下的方法也可以完成mapPoints的转换
val points2 = FloatArray(2)
// 转换pointsSrc[0]
points2[0] = values[Matrix.MSCALE_X] * pointsSrc[0] + values[Matrix.MSKEW_X] * pointsSrc[1] + values[Matrix.MTRANS_X]
// 转换pointsSrc[1]
points2[1] = values[Matrix.MSCALE_Y] * pointsSrc[1] + values[Matrix.MSKEW_Y] * pointsSrc[0] + values[Matrix.MTRANS_Y]
// points2 [18.0, 44.0]

那么mapPoints的计算方式就如上所示了。


举报

相关推荐

0 条评论