mardi 4 août 2015

Auto-moving image on drag listener

I set a drag listener to an ImageView in my app, but when I click it, I don't want it to center the image based on where I pressed. It does this:

http://ift.tt/1IjkGXw

Basically, if I press on bottom right of the image, it takes where I press as central point and moves image's center point on that exact location. But I don't want it to do that. If I press on bottom right , it shouldn't auto move itself and I can drag the image from that point. I don't think any code is necessary but just in case:

@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;
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire