如何找到 Android 中图形矩阵的比例

发布于 2024-10-20 08:56:39 字数 353 浏览 5 评论 0原文

因此,在 android 中进行捏缩放 - 但我也希望能够在图像超出范围时将其恢复,而要做到这一点,最合理的事情似乎是在比例 < > 时执行诸如此类的操作。 1,重新缩放为 1。但是,我似乎找不到从图形矩阵检索比例的好方法。

一种可能的解决方案可能是使用矩阵的 mapPoints 函数来映射一个点,并查看它最终在哪里,但除了棘手之外,这对我来说还感觉丑陋和间接​​。有没有更好的解决方案可以从 Android 图形矩阵中检索比例?

So, it appears to be fairly easy to do pinch zoom in android - but I would also like to be able to snap back the image when it goes out of bounds, and to do that the most reasonable thing seems to be to do things like when the scale < 1, rescale to 1. However, I can't seem to find a good way to retrieve the scale from the the graphics matrix.

One possible solution might be to map a point using the matrix's mapPoints function and see where it ends up, but in addition to being trickly, that just feels ugly and indirect to me. Are there any better solutions for retrieving the scale from an Android graphics matrix?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春花秋月 2024-10-27 08:56:39
float[] f = new float[9];
matrix.getValues(f);

float scaleX = f[Matrix.MSCALE_X];
float scaleY = f[Matrix.MSCALE_Y];

可能就是您正在寻找的。给出的值如下:

0:缩放 X

1:倾斜 X

2:平移 X 3

:缩放 Y

4:倾斜 Y

5:平移 Y

6:透视 0

7:透视 1

8:透视 2

float[] f = new float[9];
matrix.getValues(f);

float scaleX = f[Matrix.MSCALE_X];
float scaleY = f[Matrix.MSCALE_Y];

will probably be what you are looking for. the values given will be as followed:

0 : Scale X

1 : Skew X

2 : Translate X

3 : Scale Y

4 : Skew Y

5 : Translate Y

6 : Perspective 0

7 : Perspective 1

8 : Perspective 2

铃予 2024-10-27 08:56:39

这是记录矩阵内容的简单方法:

private void logMatrix(String label, Matrix matrix) {
    float[] matrixVals = new float[9];
    matrix.getValues(matrixVals);
    Log.e(TAG, String.format("%s: %s\nscale (x,y) = (%f, %f)\ntranslate (x,y) = (%f, %f)\nskew (x,y) "
                             + "= (%f, %f)\nperspective-0 = %f\nperspective-1 = %f\nperspective-2 = %f",
        label, matrix, matrixVals[Matrix.MSCALE_X], matrixVals[Matrix.MSCALE_Y],
        matrixVals[Matrix.MTRANS_X], matrixVals[Matrix.MTRANS_Y],
        matrixVals[Matrix.MSKEW_X], matrixVals[Matrix.MSKEW_Y],
        matrixVals[Matrix.MPERSP_0], matrixVals[Matrix.MPERSP_1], matrixVals[Matrix.MPERSP_2]));
}

Here's a simple method to log the matrix contents:

private void logMatrix(String label, Matrix matrix) {
    float[] matrixVals = new float[9];
    matrix.getValues(matrixVals);
    Log.e(TAG, String.format("%s: %s\nscale (x,y) = (%f, %f)\ntranslate (x,y) = (%f, %f)\nskew (x,y) "
                             + "= (%f, %f)\nperspective-0 = %f\nperspective-1 = %f\nperspective-2 = %f",
        label, matrix, matrixVals[Matrix.MSCALE_X], matrixVals[Matrix.MSCALE_Y],
        matrixVals[Matrix.MTRANS_X], matrixVals[Matrix.MTRANS_Y],
        matrixVals[Matrix.MSKEW_X], matrixVals[Matrix.MSKEW_Y],
        matrixVals[Matrix.MPERSP_0], matrixVals[Matrix.MPERSP_1], matrixVals[Matrix.MPERSP_2]));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文