Android中从左到右滑动视图,反之亦然

时间:2015-11-24 14:46:18

标签: android animation swipe slide gesture

我有一个位于屏幕右侧的图标,我给它一个-25dp的边距,因此它不完全可见,我希望用户能够在触摸它时移动按钮,直到完全查看并且把它还给它的位置。 我尝试了很多代码,例如下面但我无法确定每个屏幕分辨率的滑动的正确值是什么,任何人都可以帮助我找到更好的解决方案enter image description here

    class DragExperimentTouchListener implements View.OnTouchListener {

    public DragExperimentTouchListener(float initalX, float initialY) {
        lastX = initalX;
        lastY = initialY;
    }

    boolean isDragging = false;
    float lastX;
    float lastY;
    float deltaX;

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {


        if (gestureDetector.onTouchEvent(arg1)) {
            // single tap
            return true;
        } else {
            // your code for move and drag


            int action = arg1.getAction();

            if (action == MotionEvent.ACTION_DOWN && !isDragging) {
                isDragging = true;
                deltaX = arg1.getX();
                return true;
            } else if (isDragging) {
                if (action == MotionEvent.ACTION_MOVE) {


                    if (arg0.getX() >= ??) {
                        arg0.setX(arg0.getX() + arg1.getX() - deltaX);
                        arg0.setY(arg0.getY());
                    }
                    return false;
                } else if (action == MotionEvent.ACTION_UP) {
                    isDragging = false;
                    lastX = arg1.getX();
                    lastY = arg1.getY();


                    return false;
                } else if (action == MotionEvent.ACTION_CANCEL) {
                    arg0.setX(lastX);
                    arg0.setY(lastY);

                    isDragging = false;
                    return false;
                }
            }
        }

        return false;
    }
}

0 个答案:

没有答案