0

正如标题中所写,我正在尝试检测 ImageView 上的触摸事件。我可以通过在 ImageTouchHandler 类中使用 onTouch 事件来做到这一点。问题是,如果 ImageView 中包含的图像被调整大小以适合屏幕,我会在 ImageView 坐标中而不是在我的实际图像中获得触摸位置。

比如我的图片是480*648,我的imageView是1000*450,不知道怎么检测图片中的触摸位置。

我尝试用这个 : 获得实际图像的左侧位置_imageView.getDrawable().getBounds().left,但我总是有 0。

我也试过这个:

float leftImage = (_imageView.getWidth()-_imageView.getDrawable().getIntrinsicWidth())/2;

但这并不好。

你有什么主意吗?

提前致谢, 瓦伦丁

4

1 回答 1

0

如果图像放置在屏幕的左侧,或者正好在中心(宽度或高度),或者在屏幕的右侧,您可以计算它leftX rightX 等等...

所以你要做的是计算这些位置。例如,如果图像位于屏幕中央:

  • leftx = (ScreenWidth - ImageWidth) / 2
  • 添加imagewidth, 现在你有rightX( rightX = leftX + imageWidth)
  • 对高度做同样的事情;您也可以为此使用@dennisdrew 的答案

尝试将 API 更新为等的最新getWidth版本getHeight

我的代码:

public void calculateImageDimensions() {
    if(ScreenWidth < 480) {
        HeightDartBoard = ScreenWidth;
        WidthDartBoard = ScreenWidth;
        RadiusBoard = ScreenWidth / 2;
        if(WidthDartBoard == 320) {
            RadMulti = 0.75; // multiplier for different scaled images
        } else {
            RadMulti = 0.5; // means half the image width and height
        }
        LeftXBoard = 0;
    } else {
        HeightDartBoard = 480; // I already know the width and height
        WidthDartBoard = 480;
        RadiusBoard = 240;
        LeftXBoard = (ScreenWidth - WidthDartBoard) / 2; // this is what interests you
    }
    RightXBoard = LeftXBoard + WidthDartBoard; // now add the width of image to left X
    TopYBoard = intActionBar; // actionbar height is start of my image topY
    BottomYBoard = TopYBoard + HeightDartBoard; // and again add height of board
}

现在给你:

LeftX = (ScreenWidth - (WidthDartBoard * RadMulti)) / 2;

现在你有一个计算图像widthheight它的位置的例子......

在该onTouchEvent方法中,您现在可以对其进行测试:

if(((eventX > leftX) && (eventX < rightX)) && ((eventY > topY) && (eventY < bottomY)))     {
   // code here
}

如果这是true您单击图像上的某个位置。

如果你想制作新的xy相对于图像本身的:

newX = eventX - leftX
newY = eventY - topY

现在newXnewY是实际图像的X和。Y

于 2013-01-05T01:42:23.317 回答