控件触摸移动

2020-04-24  本文已影响0人  爱我O就直说

2020-04-24

用的Blankj/AndroidUtilCode的触摸监听,他帮我做了触摸偏移的计算

 target.setOnTouchListener(new TouchUtils.OnTouchUtilsListener() {

            @Override
            public boolean onDown(View view, int x, int y, MotionEvent event) {
                return true;
            }

            @Override
            public boolean onMove(View view, int direction, int x, int y, int dx, int dy, int totalX, int totalY, MotionEvent event) {
                //自己的位置+触摸偏移
                target.setX(target.getX() + dx); //dx:触摸偏移:触摸点相对于移动前的触摸点的距离
                target.setY(target.getY() + dy);
                //或
//                target.setTranslationX(target.getTranslationX() + dx);
//                target.setTranslationY(target.getTranslationY() + dy);

//                target.requestLayout();
                return true;
            }

            @Override
            public boolean onStop(View view, int direction, int x, int y, int totalX, int totalY, int vx, int vy, MotionEvent event) {
                return true;
            }
        });

上一篇下一篇

猜你喜欢

热点阅读