我正在做一些地理问答游戏。
用户必须在地图上找到一些定义。
我制作了地图点生成器,可以计算相对于图像大小的 X 和 Y 位置。所以地图上这个地方的X和Y表示为:float * imageWidth = X float * imageHeight = Y;
现在,当用户触摸图像视图时,我必须在正确位置的地图上绘制。
当图像视图的比例类型被定义为“FitXY”时,我的代码使用这些相对坐标和图像边界的大小计算位置,并且效果很好,但是地图被拉伸并且看起来不太好。
当我使用“centerInside”比例类型时......地图图像看起来不错,但我的正确位置坐标没有指向正确的位置。
如何将这些坐标转换为与屏幕上绘制的真实图像相对应?
我还需要位图的触摸坐标而不是图像视图的坐标……所以我可以计算正确位置和触摸位置之间的距离。
如何在 ImageView 内的屏幕上绘制位图画布?
这对我来说有点令人困惑......
任何想法?
吗?
编辑:
到目前为止,这是我的代码:
Drawable drawable = stageImage.getDrawable(); 矩形 imageBounds = drawable.getBounds();
// height and width of the visible (scaled) image
int scaledHeight = imageBounds.height();
int scaledWidth = imageBounds.width();
Matrix inverse = new Matrix();
stageImage.getImageMatrix().invert(inverse);
// map touch point from ImageView to image
float[] touchPoint = new float[] { event.getX(),
event.getY() };
inverse.mapPoints(touchPoint);
// Relative touch points
float relativeX = touchPoint[0] / scaledWidth;
float relativeY = touchPoint[1] / scaledHeight;
// Prevent errors
if (relativeX < 0) {
relativeX = 0;
}
if (relativeX > 1) {
relativeX = 1;
}
if (relativeY < 0) {
relativeY = 0;
}
if (relativeY > 1) {
relativeY = 1;
}
touchX = event.getX();
touchY = event.getY();
//THIS WORKS FINE!!!!!
// Draw touched point
stageImage.drawTouchedSpot(touchX, touchY);
// Get correct relative position and draw it
double correctRelativeX = gameHandler
.getCurrentMapElement().getxCoordinate()
* imageBounds.width();
double correctRelativeY = gameHandler
.getCurrentMapElement().getyCoordinate()
* imageBounds.height();
//THIS IS NOT RIGHT WHEN STAGE IMAGE SCALE TYPE IS "CENTER INSIDE"
stageImage.drawFlag(correctRelativeX, correctRelativeY);