Android Drag and Drop. Image positioning after drag and drop

When I drag the ImageView, it is not placed in the place where I released my finger. It is located a little lower and to the right. I don’t understand what happened.

Tried various parameters for ImageView image positioning, same

Markup

<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
+4
source share
4 answers

Replace

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

from

view.setX(X-(view.getWidth()/2));
view.setY(Y-(view.getHeight()/2));
+6
source

This is because it places the image in the upper left corner at the place where you realease.

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

, - :

view.setX((X - (image.width / 2));
view.setY(Y - (image.width / 2));
+3

v.setX(me.getRawX() - (v.getWidth()/2));

v.setY(me.getRawY() - (float) (v.getHeight() * 1.5/2));

, , .

0

- : , rootview, (, ), relace

findViewById(R.id.imageView1).getRootView().setOnDragListener(this);    
findViewById(R.id.imageView1).getRootView().findViewById(R.id.yourLayout).setOnDragListener(this);     

.
imageView , :

view.setX(X - iv.getWidth()/2);
view.setY(Y - iv.getHeight()/2) ;

, View :)

0

All Articles