Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Drag and Drop. Positioning image after dragging

When I drag the ImageView it is put not on the place where I let go the finger. It is placed just below and to the right. I do not understand what's wrong.

Tried various options for ImageView positioning result is the same

Layout

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF8989"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

code

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.imageView1).setOnTouchListener(this);
            findViewById(R.id.imageView1).getRootView().setOnDragListener(this);
        }

public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(null, shadowBuilder, view, 0);
        view.setVisibility(View.INVISIBLE);
        return true;
    } else {
        return false;
    }
}

public boolean onDrag(View v, DragEvent event) {

    switch (event.getAction()) {
        case DragEvent.ACTION_DROP:

        float X = event.getX();
        float Y = event.getY();

        Log.d(LOGCAT, "X " + (int) X + "Y " + (int) Y);
        View view = (View) event.getLocalState();
        view.setX(X);
        view.setY(Y);
        view.setVisibility(View.VISIBLE);

    default:
        break;
    }
    return true;
} 

LogCat

12-04 23:44:40.548: D/myLogs(32658): Width image 72 Height image 72
12-04 23:44:40.558: D/myLogs(32658): ACTION_DROP X 216Y 390
12-04 23:44:40.568: D/myLogs(32658): Real position: X 180.0Y 354.0
12-04 23:44:40.598: D/myLogs(32658): Drag ended
12-04 23:44:41.928: D/myLogs(32658): Width image 72 Height image 72
12-04 23:44:41.928: D/myLogs(32658): ACTION_DROP X 442Y 329
12-04 23:44:41.948: D/myLogs(32658): Real position: X 406.0Y 293.0
12-04 23:44:41.968: D/myLogs(32658): Drag ended
like image 338
KillAndEat Avatar asked Dec 11 '22 09:12

KillAndEat


2 Answers

Replace

view.setX(X);
view.setY(Y);

with

view.setX(X-(view.getWidth()/2));
view.setY(Y-(view.getHeight()/2));
like image 154
Subramanian Ramsundaram Avatar answered Jan 09 '23 19:01

Subramanian Ramsundaram


It's because it's placing the image's top left corner to where you realease.

To have it do what you want, basically you will need to subtract half of the image's width from X and half of it's height from Y.

So you should have something like this:

view.setX((X - (image.width / 2));
view.setY(Y - (image.width / 2));
like image 30
Aftab Hussain Avatar answered Jan 09 '23 18:01

Aftab Hussain