触摸图像按钮在Android中移动时会出现什么情况会导致巨大的左偏移

时间:2017-01-16 08:39:36

标签: android ontouchlistener android-imagebutton

当触摸并移动图像按钮时,手指触摸屏幕将突然从图像按钮图标的位置分离,并且在这种情况下,我也可以移动此图像按钮,奇怪的是图像按钮图标位于我的手指左侧,请查看日志以下区域的详细信息。

这是我的imagebutton代码的TouchListener。

class TouchListener implements OnTouchListener {
        int lastX;
        int lastY;
        int screenWidth;
        int screenHeight;
        int layoutWidth;
        int left = 0;
        int top = 0;
        int right = 0;
        int bottom = 0;

        public TouchListener() {
            DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
            screenWidth = dm.widthPixels;
            screenHeight = dm.heightPixels + 28;
            Log.d(TAG, "screen width =" + screenWidth + ",screen height="
                    + screenHeight);
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d(TAG, "TouchListener -- onTouch");
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    layoutWidth = (int) (((View) v.getParent()).getWidth()
                            + mContext.getResources().getDimension(R.dimen.screen_edge));
                    lastX = (int) event.getRawX();
                    lastY = (int) event.getRawY();
                    //Log.d(TAG, "down x=" + lastX + ", y=" + lastY);
                    ShowOrHidePaint(false, v);
                    break;
                case MotionEvent.ACTION_MOVE:
                    int dx = (int) event.getRawX() - lastX;
                    int dy = (int) event.getRawY() - lastY;
                    Log.d(TAG, "move dx=" + dx + ",  dy=" + dy);
                    left = v.getLeft() + dx;
                    top = v.getTop() + dy;
                    right = v.getRight() + dx;
                    bottom = v.getBottom() + dy;
                    Log.d(TAG, "view  left=" + left + ", top=" + top + ", right=" + right + ",bottom=" + bottom);
                    // set bound
                    if (left < 0) {
                        left = 0;
                        right = left + v.getWidth();
                    }
                    if (top < 0) {
                        top = 0;
                        bottom = top + v.getHeight();
                    }
                    if (right > layoutWidth) {
                        right = layoutWidth;
                        left = right - v.getWidth();
                    }
                    if (bottom > screenHeight) {
                        bottom = screenHeight;
                        top = bottom - v.getHeight();
                    }
                    v.layout(left, top, right, bottom);
                    Log.d(TAG, "after view left=" + v.getLeft() + ", top=" + v.getTop() + ", right="
                            + v.getRight() + ", bottom=" + v.getBottom());
                    lastX = (int) event.getRawX();
                    lastY = (int) event.getRawY();

                    break;
                case MotionEvent.ACTION_UP:

                    break;
            }
            return true;
        }
    }
  

当我触摸并移动图像按钮时,这是logcat。请注意左   正确的价值变化。

01-16 15:34:49.811 1638-1638/? D/ActivityYuYue: TouchListener -- onTouch
01-16 15:34:49.811 1638-1638/? D/ActivityYuYue: move dx=0,  dy=-2
01-16 15:34:49.811 1638-1638/? D/ActivityYuYue: view  left=1140, top=372, right=1210,bottom=473
01-16 15:34:49.811 1638-1638/? D/ActivityYuYue: after view left=1140, top=372, right=1210, bottom=473
01-16 15:34:49.831 1638-1638/? D/ActivityYuYue: TouchListener -- onTouch
01-16 15:34:49.831 1638-1638/? D/ActivityYuYue: move dx=1,  dy=-1
01-16 15:34:49.831 1638-1638/? D/ActivityYuYue: view  left=1141, top=371, right=1211,bottom=472
01-16 15:34:49.841 1638-1638/? D/ActivityYuYue: after view left=1141, top=371, right=1211, bottom=472
01-16 15:34:49.851 1638-1638/? D/ActivityYuYue: TouchListener -- onTouch
01-16 15:34:49.851 1638-1638/? D/ActivityYuYue: move dx=1,  dy=0
01-16 15:34:49.851 1638-1638/? D/ActivityYuYue: view  left=175, top=305, right=245,bottom=406
01-16 15:34:49.851 1638-1638/? D/ActivityYuYue: after view left=175, top=305, right=245, bottom=406
01-16 15:34:49.861 1638-1638/? D/ActivityYuYue: TouchListener -- onTouch
01-16 15:34:49.861 1638-1638/? D/ActivityYuYue: move dx=2,  dy=-3
01-16 15:34:49.861 1638-1638/? D/ActivityYuYue: view  left=177, top=302, right=247,bottom=403
01-16 15:34:49.871 1638-1638/? D/ActivityYuYue: after view left=177, top=302, right=247, bottom=403

我尝试将上面的onTouchListener类用于我的简单演示,它运行良好。

我尝试使用if语句检查这种情况并修改左值,失败。

哪种情况会导致这个问题,以及如何解决它?

1 个答案:

答案 0 :(得分:0)

我认为问题在于你的右边界逻辑,这使你的图像始终保持在左边。

让我猜一下,您希望将正确的界限设置为screenWidth而不是layoutWidth

更改您的右边界条件:

if (right > screenWidth) {
    right = screenWidth;
    left = right - v.getWidth();
}

希望这有帮助,我是对的;)