0
A
回答
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;
}
}
相關問題
- 1. 如何輕鬆檢測矩形/圖像內部的點擊?
- 2. 檢測哪個帆布矩形點擊
- 3. android檢測圖像點擊webview內?
- 4. 檢測點擊圖像類
- 5. c#檢測圖像中的矩形
- 6. 檢測圖像中的紅色矩形
- 7. 矩陣操縱矩形點擊測試
- 8. Android矩形區域檢測
- 9. 檢測TreeView節點圖像點擊
- 10. 如何檢測黑白圖像中的白色斑點(矩形)?
- 11. 谷歌地圖矩形停止點擊被檢測地圖上的矩形下面的點
- 12. 檢測點擊形狀pygame
- 13. 檢測iframe中的圖像點擊
- 14. 在Python中點擊矩形的測試
- 15. 檢測三角形,從圖像的矩形與OpenCV的
- 16. 從圖像中檢測三角形,橢圓和矩形
- 17. Java矩形檢測
- 18. 如何檢測點擊了哪個矩形?
- 19. Android中的2D矩形碰撞檢測
- 20. 僅檢測矩形檢測大矩形不小於? iOS,Swift
- 21. 試圖在點擊上畫矩形
- 22. 檢測點擊視圖
- 23. 檢測哪個圖像與圖像被點擊
- 24. Jquery Flot - 點擊圖形上的檢測點
- 25. 按鈕上的打開圖像文件點擊矩形
- 26. Opengl鼠標點擊矩形
- 27. GMap - 無法檢測點擊多邊形
- 28. 碰撞檢測Xcode中的非矩形,有光澤的圖像
- 29. 使用OpenCv檢測圖像中的矩形明亮區域
- 30. 如何檢測部分矩形並使用Matlab對齊圖像?
有一個叫做'重寫方法onTouchEvent'。這將有一個參數「事件」。這是你可以得到你的x和y位置的地方。然後你可以用這些x和y位置的矩形的'contains'方法。 http://codetheory.in/android-ontouchevent-ontouchlistener-motionevent-to-detect-common-gestures/ –
感謝您的幫助@ R.Suntjens。 我已經在觸摸事件中得到了x&y的位置, 但我仍然有一個問題是:如何檢測當我用白色背景觸摸矩形內部時。 如何獲取矩形中心的位置? – PhongHv
如果它是靜態位置,您可以在背景上定義一個矩形以進行檢查。如果沒有,你應該發佈更多的代碼,我們來幫助你。 –