平滑拖动视图

时间:2017-12-26 13:15:13

标签: android draggable ontouchlistener

我有一个活动,包括工具栏,bottommenu(类似于instagram)和宽度和高度= 500dp屏幕右下方的一个小的可拖动relativeLayout。

我已经对该relativeLayout实施了如下拖动:

  public void touchListenerPip(RelativeLayout relativeLayoutPIP) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    float screenHeight = displayMetrics.heightPixels;
    float screenWidth = displayMetrics.widthPixels;
    relativeLayoutPIP.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            float newX, newY;
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    dX = view.getX() - event.getRawX();
                    dY = view.getY() - event.getRawY();
                    lastAction = MotionEvent.ACTION_DOWN;
                    break;

                case MotionEvent.ACTION_MOVE:
                    newX = event.getRawX() + dX;
                    newY = event.getRawY() + dY;
                    if ((newX <= 0 || newX >= screenWidth - view.getWidth()) || (newY <= 0 || newY >= screenHeight - view.getHeight())) {
                        lastAction = MotionEvent.ACTION_MOVE;
                        break;
                    }
     //to block the view being dragged out of toolbar
                    if (newY < (float) toolbar.getHeight()) {
                        lastAction = MotionEvent.ACTION_MOVE;
                        break;
                    }
       //to block the view being dragged out of bottombar

                    if (event.getRawY() + (float) view.getHeight() > screenHeight - (float) bottomBar.getHeight()) {
                        lastAction = MotionEvent.ACTION_MOVE;
                        break;
                    }
                    view.setX(newX);
                    view.setY(newY);
                    lastAction = MotionEvent.ACTION_MOVE;
                    break;

                case MotionEvent.ACTION_UP:
                    if (lastAction == MotionEvent.ACTION_DOWN)
                        break;

                default:
                    return false;
            }
            return true;
        }
    });
}

视图拖得很好,拖动不顺畅。有关平滑拖动视图的任何建议吗?

1 个答案:

答案 0 :(得分:0)

可以顺利使用以下代码,也可以使用

执行点击
  private final static float CLICK_DRAG_TOLERANCE = 10; // Often, there will be a slight, unintentional, drag when the user taps the view, so we need to account for this.

private float downRawX, downRawY;
private float dX, dY;


public void touchListenerPip(RelativeLayout relativeLayoutPIP) {
    relativeLayoutPIP.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
                downRawX = event.getRawX();
                downRawY = event.getRawY();
                dX = view.getX() - downRawX;
                dY = view.getY() - downRawY;
                return true;
            } else if (action == MotionEvent.ACTION_MOVE) {
                int viewWidth = view.getWidth();
                int viewHeight = view.getHeight();

                View viewParent = (View) view.getParent();
                int parentWidth = viewParent.getWidth();
                int parentHeight = viewParent.getHeight();

                float newX = event.getRawX() + dX;
                newX = Math.max(0, newX); // Don't allow the view past the left hand side of screen
                newX = Math.min(parentWidth - viewWidth, newX); // Don't allow the view past the right hand side of screen

                float newY = event.getRawY() + dY;
                newY = Math.max(toolbar.getHeight(), newY); // Don't allow the view past the top of screen including toolbar
                int bottomHeight = viewHeight + bottomBar.getHeight();
                newY = Math.min(parentHeight - bottomHeight, newY); // Don't allow the view past the bottom of screen including bottombar

                view.animate()
                        .x(newX)
                        .y(newY)
                        .setDuration(0)
                        .start();

                return true;

            } else if (action == MotionEvent.ACTION_UP) {

                float upRawX = event.getRawX();
                float upRawY = event.getRawY();

                float upDX = upRawX - downRawX;
                float upDY = upRawY - downRawY;

                if (Math.abs(upDX) < CLICK_DRAG_TOLERANCE && Math.abs(upDY) < CLICK_DRAG_TOLERANCE) { // A click
                    viewClick();
                    return true;
                } else { // A drag
                    return true;
                }

            }
            return true;
        }
    });
}