gpt4 book ai didi

android - 自定义 ImageView、ImageMatrix.mapPoints() 和 invert() 不准确?

转载 作者:行者123 更新时间:2023-11-29 01:48:18 26 4
gpt4 key购买 nike

我为我的应用程序编写了一个自定义 ImageView (Android),它运行良好。出于多种目的,我需要将 View 坐标(例如点击坐标)映射到图像坐标(图像中发生点击的位置),因为图像可以缩放和滚动。到目前为止,以下方法似乎工作正常:

private PointF viewPointToImgPointF(PointF viewPoint) {
final float[] coords = new float[] { viewPoint.x, viewPoint.y };
Matrix matrix = new Matrix();
getImageMatrix().invert(matrix);
matrix.mapPoints(coords); // --> PointF in image as scaled originally
if (coords != null && coords.length > 1) {
return new PointF(coords[0] * inSampleSize,coords[1] * inSampleSize);
} else {
return null;
}
}

private PointF imgPointToViewPointF(PointF imgPoint) {
final float[] coords = new float[] { imgPoint.x / inSampleSize, imgPoint.y / inSampleSize };
getImageMatrix().mapPoints(coords);
if (coords != null && coords.length > 1) {
return new PointF(coords[0], coords[1]);
} else {
return null;
}
}

在大多数情况下,我可以毫无问题地使用这些方法,但现在我发现它们并非 100% 准确,因为我尝试了以下方法:

            PointF a = new PointF(100,100);
PointF b = imgPointToViewPointF(a);
PointF a2 = viewPointToImgPointF(b);
PointF b2 = imgPointToViewPointF(a2);
PointF a3 = viewPointToImgPointF(b2);

并得到这些值:

a: Point(100, 100)
b: Point(56, 569)
a2: Point(99, 99)
b2: Point(55, 568)
a3: Point(97, 97)

如果一切正常,所有 a- 值和所有 b- 值应该保持不变。

我还发现,这个微小的差异正在向图像中心减小。如果 (100, 100) 是图像的中心,这些方法将提供正确的结果!

有没有人经历过类似的事情,甚至有解决方案?还是我做错了什么?

(我测试的图片inSampleSize == 1,表示为节省内存调整图片大小的系数)

最佳答案

我还没有检查这个确切的场景,但看起来你的问题是反复将 float 转换回 int,然后在第二次迭代中再次将该值用作 float。 Actor 阵容中的细节损失每次都会加剧。

关于android - 自定义 ImageView、ImageMatrix.mapPoints() 和 invert() 不准确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19958256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com