How to drag images from one imageview and drop to another image view.

牧云@^-^@ 提交于 2019-12-11 17:33:42

问题


 public class Quiz extends Activity {

        int windowwidth;
        int windowheight;
        ImageView ima ima2;
        private View selected_item = null;
        private int offset_x = 0;
        private int offset_y = 0;

        private android.widget.LinearLayout.LayoutParams layoutParams1,
                layoutParams2;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.quiz);

            windowwidth = getWindowManager().getDefaultDisplay().getWidth();
            windowheight = getWindowManager().getDefaultDisplay().getHeight();

            System.out.println("width" + windowwidth);
            System.out.println("height" + windowheight);
   //on touch listener for image view 
            ima1 = (ImageView) findViewById(R.id.shuffledimage);
            ima1.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    layoutParams1 = (LinearLayout.LayoutParams) ima1
                            .getLayoutParams();
                    try {//action performed in image view
                        switch (event.getActionMasked()) {
                        case MotionEvent.ACTION_DOWN:
                            break;
                        case MotionEvent.ACTION_MOVE:
                            int x_cord = (int) event.getRawX();
                            int y_cord = (int) event.getRawY();
                            if (x_cord > windowwidth) {
                                x_cord = windowwidth;
                            }
                            if (y_cord > windowheight) {
                                y_cord = windowheight;
                            }
                            layoutParams1.leftMargin = x_cord - 25;
                            layoutParams1.topMargin = y_cord - 75;
                            ima1.setLayoutParams(layoutParams1);
                            break;

                        default:
                            break;
                        }
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.getMessage();
                    }

                    return true;
                }
            });

            ima2 = (ImageView) findViewById(R.id.userimage);
            ima2.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    layoutParams2 = (LinearLayout.LayoutParams) ima2
                            .getLayoutParams();
                    try {
                        switch (event.getActionMasked()) {
                        case MotionEvent.ACTION_DOWN:
                            break;
                        case MotionEvent.ACTION_MOVE:
                            int x_cord = (int) event.getRawX();
                            int y_cord = (int) event.getRawY();
                            if (x_cord > windowwidth) {
                                x_cord = windowwidth;
                            }
                            if (y_cord > windowheight) {
                                y_cord = windowheight;
                            }
                            layoutParams2.leftMargin = x_cord - 25;
                            layoutParams2.topMargin = y_cord - 75;
                            ima2.setLayoutParams(layoutParams2);
                            break;
                        default:
                            break;

                        }

                    } catch (Exception e) {
                        e.getMessage();
                    }
                    return true;

                }

            });

        }

    }

//xml file

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ipodbackground"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@drawable/headingmain1"
            android:gravity="center_horizontal" >

            <Button
                android:id="@+id/button1"
                android:layout_width="65dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:background="#00000000" />

            <Button
                android:id="@+id/button2"
                android:layout_width="40dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:background="#00000000" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="@drawable/quiztitle1" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp" >

            <LinearLayout
                android:id="@+id/l1"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="25dp"
                android:background="@drawable/box"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="5dp"
                    android:text="TextView" />


                <ImageView
                    android:id="@+id/userimage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="160dp"
                    android:layout_weight="1"
                    android:background="#FFFFFF"
                    android:src="@drawable/ic_launcher" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_marginRight="5dp"
                android:layout_toRightOf="@+id/l1" >

                <ImageView
                    android:id="@+id/shuffledimage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/iconsearch" />
            </LinearLayout>
        </RelativeLayout>

    </LinearLayout>

// i want to drag and drop image from one image view to another imageview and in above code section image can be dragged correctly when i drag but the image cant dropped to the source image view. anybody please give solution


回答1:


For drag and drop please go through the following links .

http://blahti.wordpress.com/2011/10/03/drag-drop-for-android-gridview/

http://www.twylah.com/dodulz/tweets/169426982533206016




回答2:


You can achieve it in followin way

package edu.sbcc.cs123.draganddropbasic;

import android.app.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

@SuppressWarnings("deprecation")
public class DragAndDropBasicActivity extends Activity implements OnTouchListener {
    private ImageView letterView;                       // The letter that the user drags.
    private ImageView emptyLetterView;              // The letter outline that the user is supposed to drag letterView to.
    private AbsoluteLayout mainLayout;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mainLayout = (AbsoluteLayout) findViewById(R.id.mainLayout);
        mainLayout.setOnTouchListener(this);
        letterView = (ImageView) findViewById(R.id.letterView);
        letterView.setOnTouchListener(this);

        emptyLetterView = (ImageView) findViewById(R.id.emptyLetterView);
    }

    private boolean dragging = false;
    private Rect hitRect = new Rect();

    @Override
    /**
     * NOTE:  Had significant problems when I tried to react to ACTION_MOVE on letterView.   Kept getting alternating (X,Y) 
     * locations of the motion events, which caused the letter to flicker and move back and forth.  The only solution I could 
     * find was to determine when the user had touched down on the letter, then process moves in the ACTION_MOVE 
     * associated with the mainLayout.
     */
    public boolean onTouch(View v, MotionEvent event) {
        boolean eventConsumed = true;
        int x = (int)event.getX();
        int y = (int)event.getY();

        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            if (v == letterView) {
                letterView.setImageResource(R.drawable.carkey1);
                dragging = true;
                eventConsumed = false;
            }
        } else if (action == MotionEvent.ACTION_UP) {

            if (dragging) {
                emptyLetterView.getHitRect(hitRect);
                if (hitRect.contains(x, y)) {
                    letterView.setImageResource(R.drawable.carkey3);
                    setSameAbsoluteLocation(letterView, emptyLetterView);
                }
            }
            dragging = false;
            eventConsumed = false;

        } else if (action == MotionEvent.ACTION_MOVE) {
            if (v != letterView) {
                if (dragging) {
                    setAbsoluteLocationCentered(letterView, x, y);
                }
            }
        }

        return eventConsumed;

    }


    private void setSameAbsoluteLocation(View v1, View v2) {
        AbsoluteLayout.LayoutParams alp2 = (AbsoluteLayout.LayoutParams) v2.getLayoutParams();
        setAbsoluteLocation(v1, alp2.x, alp2.y);
    }


    private void setAbsoluteLocationCentered(View v, int x, int y) {
        setAbsoluteLocation(v, x - v.getWidth() / 2, y - v.getHeight() / 2);
    }


    private void setAbsoluteLocation(View v, int x, int y) {
        AbsoluteLayout.LayoutParams alp = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
        alp.x = x;
        alp.y = y;
        v.setLayoutParams(alp);
    }
}






xml
----

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


     <ImageView
        android:id="@+id/emptyLetterView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/carkey2" >
    </ImageView>

    <ImageView
        android:id="@+id/letterView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="200dp"
        android:layout_y="450dp"
        android:src="@drawable/carkey1" >
    </ImageView>

</AbsoluteLayout>


来源:https://stackoverflow.com/questions/13247050/how-to-drag-images-from-one-imageview-and-drop-to-another-image-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!