2013-08-01 59 views
1

我正在研究一些地理知识问答游戏。用相对坐标在图像视图上绘制点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); 

回答

0

所有你需要做的就是用ImageView.getImageMatrix()

+0

我想。但我认为还有其他的东西可以满足我的需求... – Veljko

+0

不会逆矩阵小睡点好吗? – pskink

+0

它我猜...但在相反的方向......我举例说,一个城市在0.5 *宽度和0.3 *原始图像的高度...现在我想在该位置上画一些国旗动画。 ..但是当图像的缩放类型是“centerInside”时它不正确......当它是“FitXY”时很好,但是地图看起来很丑很糟糕。 – Veljko

0

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

我的代码的伟大工程的意志小魔术描述如下:

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

我给自己定的图像缩放类型为“centerInside”但scaleImage()方法,我可以调整图像以适合我的位图IMAG e ...

这种方式的相对坐标是正确的,我可以在定义为正确的位置上绘制动画!无论如何,Tnx!

如果您有更好的想法...我很高兴尝试...我想这是不是最优化的形式给出

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,并使用imageView的getImageMatrix()来修改视图。