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

在Android中,当触摸图像按钮移动时,什么情况会导致巨大的左偏移,android,ontouchlistener,android-imagebutton,Android,Ontouchlistener,Android Imagebutton,当触摸并移动imagebutton时,手指触摸屏幕的地方会突然从imagebutton图标的位置分离出来,在这种情况下,我也可以移动这个imagebutton,奇怪的是图像按钮图标位于我手指的左侧,请查看下面区域的详细信息日志 这是我的imagebutton代码的TouchListener class TouchListener implements OnTouchListener { int lastX; int lastY; int scree

当触摸并移动imagebutton时,手指触摸屏幕的地方会突然从imagebutton图标的位置分离出来,在这种情况下,我也可以移动这个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;
        }
    }
我尝试将上面的onTouchListener类用于我的简单演示,它运行良好

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


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

我认为问题在于您的右边界逻辑,它使您的图像始终保持在左侧

让我猜猜,您想将右边界设置为
screenWidth
,而不是
layoutWidth

更改右边界条件:

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

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

我认为问题在于您的右边界逻辑,它使您的图像始终位于左侧

让我猜猜,您想将右边界设置为
screenWidth
,而不是
layoutWidth

更改右边界条件:

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

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

当左值大于0Nishan我不明白你的意思,我的代码应该保证左值大于0,请解释更多细节,谢谢。当左值大于0Nishan我不明白你的意思,我的代码应该保证左值大于0,请解释更多细节,谢谢。谢谢,我的设备分辨率是1280*720,但某些功能的父布局宽度是4345px。所以右边界逻辑是ok的,最大宽度是layoutWidth。我告诉你的方式,仍然失去“焦点”。你可能想检查
getLocationOnScreen(location)
方法,正如这里所建议的。谢谢,我的设备分辨率是1280*720,但某些功能的父布局宽度是4345px。所以右边界逻辑是ok的,最大宽度是layoutWidth。我告诉你的是,仍然会失去“焦点”。你可能想检查
getLocationOnScreen(location)
方法,如这里所建议的。