2

我正在做一些地理问答游戏。

用户必须在地图上找到一些定义。

我制作了地图点生成器,可以计算相对于图像大小的 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);
4

3 回答 3

0

我想我找到了所有问题的答案......

我的代码很好用,这里描述的小魔法:

http://argillander.wordpress.com/2011/11/24/scale-image-into-imageview-then-resize-imageview-to-match-the-image/

我已将图像缩放类型设置为“centerInside”,但使用 scaleImage() 方法我可以调整图像视图的大小以适合我的位图图像......

这样相对坐标是正确的,我可以在定义为正确的地方绘制动画!反正Tnx!

如果您有更好的主意...我很乐意尝试...我想这不是最优化的方法

于 2013-08-01T16:37:58.637 回答
0

此处的此映射应该已经考虑了缩放的宽度/高度

float[] touchPoint = new float[] { event.getX(), event.getY() };
inverse.mapPoints(touchPoint);

所以我不相信你需要这个:

            // Relative touch points
            float relativeX = touchPoint[0] / scaledWidth;
            float relativeY = touchPoint[1] / scaledHeight;

此外,尝试将您的比例类型更改为 SCALE_TYPE.MATRIX 并使用 imageViewgetImageMatrix()修改视图。

于 2013-09-23T19:03:01.627 回答
0

您所要做的就是使用 ImageView.getImageMatrix()

于 2013-08-01T09:43:40.330 回答