自动移动图像拖动监听器



我在我的应用程序中设置了一个拖动侦听器到ImageView,但是当我单击它时,我不希望它根据我按下的位置将图像放在中心。它的作用是:

https://gfycat.com/ConstantDisguisedKudu

基本上,如果我按下图像的bottom right,它会在我按下central point的地方移动图像的center point。但我不希望它那样。如果我按下bottom right,它不会自动移动我可以从那个点拖动图像。我不认为任何代码是必要的,但以防万一:

@Override
public boolean onDrag(View v, DragEvent event) {
    switch (event.getAction()) {
        // Signal for the start of drag and drop operation
        case DragEvent.ACTION_DRAG_STARTED: {
            // do nothing
            break;
        }
        // The drag point has entered the bounding box of the View
        case DragEvent.ACTION_DRAG_ENTERED: {
            // do nothing
            break;
        }
        // The user has moved the drag shadow outside the bounding box of the view
        case DragEvent.ACTION_DRAG_EXITED: {
            // do nothing
            break;
        }
        // Drag shadow has been released, the drag point is within the bounding box of the view
        case DragEvent.ACTION_DROP: {
            // Get the image and its position
            ImageView view = (ImageView) event.getLocalState();
            int position = (int) view.getTag(R.id.piece_position);
            /**
             * If it is dropped on the left pane, remove it from its parent and also
             * remove the bitmap at the position and notify the adapter.
             * Add it to the left pane and set the position.
             */
            if (v == puzzlePane) {
                ViewGroup viewgroup = (ViewGroup) view.getParent();
                viewgroup.removeView(view);
                if (position != -1) {
                    pieces.remove(position);
                    mAdapter.notifyDataSetChanged();
                }
                FrameLayout containView = (FrameLayout) v;
                containView.addView(view);
                view.setVisibility(View.VISIBLE);
                view.setTag(R.id.piece_state, "left");
                view.setTag(R.id.piece_position, -1);
                view.setOnLongClickListener(null);
                view.setOnTouchListener(mAdapter);
            } else {
                view.setVisibility(View.VISIBLE);
                view.setTag(R.id.piece_state, "right");
                view.setOnTouchListener(null);
                view.setOnLongClickListener(mAdapter);
            }
            Log.d(MyDragListener.class.getSimpleName(), view.getTag(R.id.piece_state) + "");
            view.setX(event.getX() - (view.getWidth() / 2));
            view.setY(event.getY() - (view.getHeight() / 2));
            break;
        }
        // The drag and drop operation has concluded
        case DragEvent.ACTION_DRAG_ENDED: {
            // do nothing
            break;
        }
    }
    return true;
}

您需要扩展View。DragShadowBuilder,并发送手指相对于容器的位置。像这样:

//where you trigger the dragging
View yourImage; //
yourImage.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        final ClipData dragData = new ClipData("label", new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN}, null);
        startDrag(dragData,  // the data to be dragged
                new MyDragShadowBuilder(v, event.getX(), event.getY()), // the drag shadow builder
                null,
                0          // flags (not currently used, set to 0)
        );
        return true;
    }
});

和class:

private static class MyDragShadowBuilder extends View.DragShadowBuilder {
    int width, height, relativeX, relativeY;
    public MyDragShadowBuilder(View v, float relativeX,float relativeY) {
        super(v);
        this.relativeX = (int) relativeX;
        this.relativeY = (int) relativeY;
        width = (v.getWidth());
        height = (v.getHeight());
    }
    @Override
    public void onProvideShadowMetrics (@NonNull Point size, @NonNull Point touch){
        size.set(width, height);
        touch.set(relativeX, relativeY);
    }
}

最新更新