Reputation: 3430
I wrote a custom ImageView (Android) for my app which works fine. For several purposes I need to map view- coordinates (e.g. coordinates of a click) to image- coordinates (where in the image did the click happen), as the image can be zoomed and scrolled. So far the methods below seemed to work fine:
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;
}
}
In most cases I can use these methods without problems but now I noticed that they are not 100% accurate as I tried out the following:
PointF a = new PointF(100,100);
PointF b = imgPointToViewPointF(a);
PointF a2 = viewPointToImgPointF(b);
PointF b2 = imgPointToViewPointF(a2);
PointF a3 = viewPointToImgPointF(b2);
and got these values:
a: Point(100, 100)
b: Point(56, 569)
a2: Point(99, 99)
b2: Point(55, 568)
a3: Point(97, 97)
If everything worked correctly all a- values and all b- values should have stayed the same.
I also found out, that this little difference is decreasing towards the center of the image. If (100, 100) would be the center of the image, the methods would deliver correct results!
Has anybody experienced something similar and maybe even has a solution? Or do I do something wrong?
(inSampleSize is == 1 for the image I tested. it represent the factor that the image has been resized by to save memory)
Upvotes: 2
Views: 1008
Reputation: 3430
Ok, it seems like it really has been a bug in Android < 4.4.
It works now on devices that were updated but still happens on devices with 4.3 and older. So I assume there is not much I can do to avoid it on older devices (except for doing the whole matrix calculations by myself. Which I surely won't...)
Upvotes: 0
Reputation: 11
I haven't checked this exact scenario but it looks like your problem is with repeatedly casting the float back to an int and then using that value as a float again in the second iteration. The loss of detail in the cast will be compounded each time.
Upvotes: 1