2015-12-02 92 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