我正在尝试开发一个类似于google play上的“颜色测量”或“颜色抓取”的android应用程序。我在获取视图的确切位置(其作用类似于焦点正方形)并将其移动到用户触摸屏幕的位置时遇到了问题。我做了很多搜索,但都以onTouchEvent()结尾,我使用了它,但它不能正常工作,或者我做错了什么。实际上,视图会移动,但不会精确地放置在用户触摸的位置,它会在y轴上移动到触摸区域的下方,并有一段距离。
下面是我的代码,其中mFocusRectangle是我想要移动到触摸位置的自定义视图:
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if (event.getPointerCount() > 1) {
// handle multi-touch events
} else {
// handle single touch events
if(action==MotionEvent.ACTION_DOWN){
}
if (action == MotionEvent.ACTION_UP) {
int pointerId = event.getPointerId(0);
int pointerIndex = event.findPointerIndex(pointerId);
// Get the pointer's current position
float x = event.getX(pointerIndex);
float y = event.getY(pointerIndex);
mFocusRectangle.showStart();
mFocusRectangle.setX(x);
mFocusRectangle.setY(y);
}
}
return true;
}我也尝试过MotionEvent.ACTION_DOWN,但结果是一样的。提前谢谢。
发布于 2015-08-05 19:17:18
尝尝这个
float x = event.getX(pointerIndex) - (mFocusRectangle.getWidth() / 2);
float y = event.getY(pointerIndex) - (mFocusRectangle.getHeight() / 2);来源:马尼托巴省- Android move view on touch event
https://stackoverflow.com/questions/31811038
复制相似问题