2015-12-02 95 views
0

我有一个图像包含一些矩形。 我想要检测触摸矩形内的事件以及如何获取坐标x & y。检测点击矩形图像android

在此先感谢您的帮助。

请参阅上面的照片:

enter image description here

+0

有一个叫做'重写方法onTouchEvent'。这将有一个参数“事件”。这是你可以得到你的x和y位置的地方。然后你可以用这些x和y位置的矩形的'contains'方法。 http://codetheory.in/android-ontouchevent-ontouchlistener-motionevent-to-detect-common-gestures/ –

+0

感谢您的帮助@ R.Suntjens。 我已经在触摸事件中得到了x&y的位置, 但我仍然有一个问题是:如何检测当我用白色背景触摸矩形内部时。 如何获取矩形中心的位置? – PhongHv

+0

如果它是静态位置,您可以在背景上定义一个矩形以进行检查。如果没有,你应该发布更多的代码,我们来帮助你。 –

回答

1

为得到一个触摸的X和Y坐标,你可以覆盖触摸,比获得X和Y坐标。

@Override 
public boolean onTouch(View v, MotionEvent ev) { 

    boolean handledHere = false; 

    final int action = ev.getAction(); 

    final int evX = (int) ev.getX(); 
    final int evY = (int) ev.getY(); 

    switch (action) { 
     case MotionEvent.ACTION_DOWN: 

      handledHere = true; 
      break; 

     case MotionEvent.ACTION_UP: 

      try { 
       InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0); 
      } catch (Exception e) { 
      } 

      defineArea(evX, evY); 

      handledHere = true; 
      break; 

     default: 
      handledHere = false; 
    } // end switch 

    return handledHere; 
} 

为了得到触摸彩色

int touchColor = getHotspotColor(R.id.image, evX, evY); 

在getHotspotColor返回触摸

public int getHotspotColor(int hotspotId, int x, int y) { 
    if (imgHome == null) { 
     if (IConstants.debug) 
      Loger.d("ImageAreasActivity", "Hot spot image not found"); 
     return 0; 
    } else { 
     imgHome.setDrawingCacheEnabled(true); 
     Bitmap hotspots = Bitmap.createBitmap(imgHome.getDrawingCache()); 
     if (hotspots == null || ((x < 1 || y < 1) || (x > hotspots.getWidth() || y > hotspots.getHeight()))) { 
      if (IConstants.debug) 
       Loger.d("ImageAreasActivity", "Hot spot bitmap was not created"); 
      return 0; 
     } else { 
      imgHome.setDrawingCacheEnabled(false); 
      return hotspots.getPixel(x, y); 
     } 
    } 
} 

你得到的彩色触摸的颜色。

1

您可以指定一个可点击的区域,并拒绝这个区域之外的点击次数:

MainActivity

// Status Bar Height 
final int statusBarId = this.getResources().getIdentifier("status_bar_height", "dimen", "android"); 
final int statusBarHeight = statusBarId > 0 ? this.getResources().getDimensionPixelSize(statusBarId) : 0; 

// OnTouchZone 
final OnTouchZone onTouchZone = new OnTouchZone(100, 50, 350, 150); 

// Image 
final ImageView image = new ImageView(this); 
image.setImageResource(R.drawable.your_image); 
image.setOnTouchListener(new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     switch (event.getActionMasked()) { 
      case MotionEvent.ACTION_DOWN: 
       if (onTouchZone.contains(event.getX(), event.getY() - statusBarHeight)) { 
        // Your action 

        return true; 
       } 
       break; 
     } 

     return false; 
    } 
}); 

OnTouchZone

public final class OnTouchZone { 

    private final int left, top, right, bottom; 

    public OnTouchZone(final int left, final int top, final int right, final int bottom) { 
     this.left = left; 
     this.top = top; 
     this.right = right; 
     this.bottom = bottom; 
    } 

    public final boolean contains(final int x, final int y) { 
      return x > this.left && x < this.right && y > this.top && y < this.bottom; 
    } 

} 
+0

感谢@Denis的帮助。 但我没有完全触摸区域。 它只是一个相同的颜色区域,例如:上面的照片为白色区域。 – PhongHv

+0

@sheep,颜色检测的问题是这是一个非常繁重的操作,这将适用于图像的所有白色像素,甚至是那些你不想要的。此外,经过PNG或JPG压缩后,一些白色像素组变成灰色,因此您还需要设置一个容差。在不知道要使用的图像的情况下很难给出建议,但我有一条黄金法则,规定“如果解决方案很复杂,那么这不是正确的解决方案。” :) – Denis

+0

哈哈...我喜欢你的黄金法则:D @Denis 我会尝试一些方法来解决它;) – PhongHv