如何获得任意矩阵的缩放值? [英] How to get zoom value of arbitrary matrix?
问题描述
图像按矩阵缩放:
Matrix matrix = new Matrix();
matrix.postScale(...);
matrix.postTranslate(...);
matrix.postRotate(...);
...
我希望缩放后的图像不会小于原始图像的一半,因此总缩放比例应不小于0.5.
I hope the zoomed image won't be less than the half of original, so the total zoom should not less that 0.5.
但是该怎么做呢?我试图获取要检查的矩阵的第一个值:
But how to do that? I tried to get the first value of matrix to check:
float pendingZoom = 0.6f;
float[] values = new float[9];
Matrix.getValues(values);
float scalex = values[Matrix.MSCALE_X];
然后:
if(scalex<0.5) {
pendingZoom = pendingZoom * (0.5f / scalex);
}
不幸的是,有时它不起作用.如果图像已旋转,则scalex
可能为负,pendingZoom
也将为负.
unfortunately, it doesn't work sometimes. If the image has been rotated, the scalex
may be negative, the pendingZoom
will be negative too.
如何正确执行此操作?
更新
我刚刚发现values[Matrix.MSCALE_X]
不是可实现的缩放值.我用它来计算矩形的新宽度,这是不正确的.
I just found the values[Matrix.MSCALE_X]
is not a realiable zoom value. I use it to calculate the new width of a rect, it's not correct.
相反,我尝试通过矩阵映射两个点,然后计算两个距离:
Instead, I tried to map two points by the matrix, and calculate the two distances:
PointF newP1 = mapPoint(matrix, new PointF(0, 0));
PointF newP2 = mapPoint(matrix, new PointF(width, 0));
float scale = calcDistance(newP1, newP2) / width;
我现在可以获得正确的scale
值.但是我不确定这是否是最佳解决方案.
I can get correct scale
value now. But I'm not sure if it's best solution.
推荐答案
float scalex = values[Matrix.MSCALE_X];
float skewy = values[Matrix.MSKEW_Y];
float scale = (float) Math.sqrt(scalex * scalex + skewy * skewy);
此解决方案看起来更简单,但我认为您的解决方案要清晰得多,并且几乎与该解决方案一样快.
This solution looks simpler but I think that your's is much more clear and almost as fast as this one.
这篇关于如何获得任意矩阵的缩放值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!