这个ontouchListener试图通过跟踪pointerID来解决这个问题.这个ontouchListener的问题是在拖动ImageVIEw时,ImageVIEw非常疯狂地跳转. getX()和getY()值跳转.
我觉得我正确地做到了这一点.我不想为此编写自定义视图,因为我已经实现了scaleGestureDetector并编写了一个有效的自定义rotateGestureDetector.一切正常但我需要解决使用getRawX()和getRawY()时遇到的问题.
有谁知道我在这里做错了什么?
这是我的ontouchListener:
final VIEw.OntouchListener ontouchListener = new VIEw.OntouchListener(){ @OverrIDe public boolean ontouch(VIEw v,MotionEvent event) { relativeLayoutParams = (relativeLayout.LayoutParams) v.getLayoutParams(); final int action = event.getAction(); switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { final float x = event.getX(); final float y = event.getY(); // Where the user started the drag lastX = x; lastY = y; activePointerID = event.getPointerID(0); break; } case MotionEvent.ACTION_MOVE: { // Where the user's finger is during the drag final int pointerIndex = event.findPointerIndex(activePointerID); final float x = event.getX(pointerIndex); final float y = event.getY(pointerIndex); // Calculate change in x and change in y final float dx = x - lastX; final float dy = y - lastY; // Update the margins to move the vIEw relativeLayoutParams.leftmargin += dx; relativeLayoutParams.topmargin += dy; v.setLayoutParams(relativeLayoutParams); // Save where the user's finger was for the next ACTION_MOVE lastX = x; lastY = y; v.invalIDate(); break; } case MotionEvent.ACTION_UP: { activePointerID = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_CANCEL: { activePointerID = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_POINTER_UP: { // Extract the index of the pointer that left the touch sensor final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; final int pointerID = event.getPointerID(pointerIndex); if(pointerID == activePointerID) { // This was our active pointer going up. Choose a new // active pointer and adjust accordingly final int newPointerIndex = pointerIndex == 0 ? 1 : 0; lastX = (int) event.getX(newPointerIndex); lastY = (int) event.getY(newPointerIndex); activePointerID = event.getPointerID(newPointerIndex); } break; } } return true; }};image1.setontouchListener(ontouchListener);解决方法 问题很简单,但出人意料. getRawX / Y()返回绝对坐标,而getX / Y()返回相对于视图的坐标.我会移动视图,重置lastX / Y,图像将不再在同一个位置,所以当我得到新的值时,它们将被关闭.在这种情况下,我只需要我最初按下图像的位置(不是使用`getRawX / Y’时的情况).
因此,解决方案是简单地删除以下内容:
// Save where the user's finger was for the next ACTION_MOVElastX = x;lastY = y;
我希望这将有助于将来的某些人,因为我见过其他人有这个问题,他们有类似的代码给我(重置lastX / Y)
总结以上是内存溢出为你收集整理的android – 拖动时跳转ImageView. getX()和getY()值正在跳跃全部内容,希望文章能够帮你解决android – 拖动时跳转ImageView. getX()和getY()值正在跳跃所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)