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