Android从触摸位置开始拖动

时间:2015-05-26 08:53:23

标签: android drag-and-drop touch move

我想通过拖放移动textview。并给它一个新的位置。

我使用以下代码:

ClipData data = ClipData.newPlainText("", "");

                        double rotationRad = Math.toRadians(view.getRotation());
                        final int w = (int) (view.getWidth() * view.getScaleX());
                        final int h = (int) (view.getHeight() * view.getScaleY());
                        double s = Math.abs(Math.sin(rotationRad));
                        double c = Math.abs(Math.cos(rotationRad));
                        final int width = (int) (w * c + h * s);
                        final int height = (int) (w * s + h * c);
                        final View v = view;

                        View.DragShadowBuilder shadow = new View.DragShadowBuilder(view) {
                            @Override
                            public void onDrawShadow(Canvas canvas) {
                                canvas.scale(v.getScaleX(), v.getScaleY(), width / 2, height / 2);
                                canvas.rotate(v.getRotation(), width / 2, height / 2);
                                canvas.translate((width - v.getWidth()) / 2, (height - v.getHeight()) / 2);
                                super.onDrawShadow(canvas);
                            }

                            @Override
                            public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
                                shadowSize.set(width, height);
                                shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
                            }
                        };
                        view.startDrag(data, shadow, view, 0);

现在,拖动阴影总是从触摸的图像视图的中心构建。

如何从触摸视图的位置精确构建拖动阴影?

1 个答案:

答案 0 :(得分:1)

shadowTouchPoint.set()函数是指示startDrag()函数开始拖动的函数。 所以在你的代码中, 改变shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2)shadowTouchPoint.set(desiredX, desiredY)

因此,在您的要求中,除了您motionEvent.getX()之前的motionEvent.getY()onTouch(),这些内容就不过了。

这是它的样子,

public class TouchListener implements View.OnTouchListener {

        @Override
        public boolean onTouch(final View view, final MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                view.startDrag(data, new View.DragShadowBuilder(view) {
                    @Override
                    public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
                        shadowSize.set(view.getWidth(), view.getHeight());
                        shadowTouchPoint.set((int) motionEvent.getX(), (int) motionEvent.getY());
                    }
                }, view, 0);
                return true;
            }
            return false;
        }
    }

和用于视图的TouchListener将是

view.setOnTouchListener(new TouchListener());