我正在研究一些地理知识问答游戏。用相对坐标在图像视图上绘制点android
用户必须在地图上找到一些定义的。
我制作了地图点生成器,用于计算相对于图像大小的X和Y位置。 因此,地图上地点的X和Y被表示为: float * imageWidth = X float * imageHeight = Y;
现在当用户触摸图像视图时,我必须绘制正确位置的地图。
当图像视图的缩放类型被定义为“FitXY”时,我的代码会使用这些相对坐标和图像边界的大小计算位置,并且工作正常,但地图拉伸且看起来不太好。
当我使用“centerInside”缩放类型...地图图像看起来不错,但我的坐标正确的位置不是指向正确的地方。
如何将这些坐标转换为与屏幕上绘制的真实图像相对应的坐标?
此外,我需要位图的触摸坐标而不是图像视图...所以我可以计算正确位置和触摸位置之间的距离。
如何获取在ImageView屏幕上绘制的位图画布?
这对我来说有点困惑......
有什么想法吗?
Tnx?
编辑:
这是到目前为止我的代码:
可绘制可绘制= stageImage.getDrawable(); Rect 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);
我想。但我认为还有其他的东西可以满足我的需求... – Veljko
不会逆矩阵小睡点好吗? – pskink
它我猜...但在相反的方向......我举例说,一个城市在0.5 *宽度和0.3 *原始图像的高度...现在我想在该位置上画一些国旗动画。 ..但是当图像的缩放类型是“centerInside”时它不正确......当它是“FitXY”时很好,但是地图看起来很丑很糟糕。 – Veljko