Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 多个图像的拖放功能_Java_Android_Drag And Drop_Android Imageview - Fatal编程技术网

Java 多个图像的拖放功能

Java 多个图像的拖放功能,java,android,drag-and-drop,android-imageview,Java,Android,Drag And Drop,Android Imageview,我想知道是否有任何教程对不同的图像具有拖放功能,使它们针对特定的目标 我的意思是,如果你有说,4个可拖动的图像图像拖动1,图像拖动2,图像拖动3,以及图像拖动4和4个拖放目标图像图像放置1,图像放置2,图像放置3和图像放置4 image\u drag\u 1应与image\u drop\u 1匹配,任何尝试将image\u drag\u 1放置在屏幕布局中的任何其他放置图像或位置上的操作都会使image\u drag\u 1捕捉回其原始位置 实现textview的拖放功能,但不指定目标 我已经尝

我想知道是否有任何教程对不同的图像具有拖放功能,使它们针对特定的目标

我的意思是,如果你有说,4个可拖动的图像<代码>图像拖动1,
图像拖动2
图像拖动3
,以及
图像拖动4
和4个拖放目标图像<代码>图像放置1,
图像放置2
图像放置3
图像放置4

image\u drag\u 1
应与image\u drop\u 1匹配,任何尝试将
image\u drag\u 1
放置在屏幕布局中的任何其他放置图像或位置上的操作都会使
image\u drag\u 1
捕捉回其原始位置

实现textview的拖放功能,但不指定目标

我已经尝试过按照上面提到的教程实现我自己的拖放,将textview和ImageView作为拖放目标。它只是使我的模拟器崩溃

下面是我的.java文件:

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.ClipData;
import android.graphics.Typeface;


public class MainActivity extends Activity {

    //Imageview to drag the image is dragged into
    private ImageView image1, image2;

    //textview the image is dragged into
    private TextView choice1, choice2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //views to drag
        image1 = (ImageView)findViewById(R.id.imageView1);
        image2 = (ImageView)findViewById(R.id.imageButton1);

        //views to drop onto
        choice1 = (TextView)findViewById(R.id.choice_1);
        choice2 = (TextView)findViewById(R.id.choice_2);

        //set touch listeners
        image1.setOnTouchListener(new ChoiceTouchListener());
        image2.setOnTouchListener(new ChoiceTouchListener());

        //set drag listeners
        choice1.setOnDragListener(new ChoiceDragListener());
        choice2.setOnDragListener(new ChoiceDragListener());

    }

    private final class ChoiceTouchListener implements OnTouchListener {
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                /*
                 * Drag details: we only need default behavior
                 * - clip data could be set to pass data as part of drag
                 * - shadow can be tailored
                 */
                ClipData data = ClipData.newPlainText("", "");
                DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                //start dragging the item touched
                view.startDrag(data, shadowBuilder, view, 0);
                return true;
            } else {
                return false;
            }
        }
    } 

    /**
     * DragListener will handle dragged views being dropped on the drop area
     * - only the drop action will have processing added to it as we are not
     * - amending the default behavior for other parts of the drag process
     *
     */
    private class ChoiceDragListener implements OnDragListener {

        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                //no action necessary
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                //no action necessary
                break;
            case DragEvent.ACTION_DRAG_EXITED:        
                //no action necessary
                break;
            case DragEvent.ACTION_DROP:
                //handle the dragged view being dropped over a drop view
                View view = (View) event.getLocalState();

                //stop displaying the view where it was before it was dragged
                view.setVisibility(View.INVISIBLE);

                //view dragged item is being dropped on
                TextView dropTarget = (TextView) v;

                //view being dragged and dropped
                ImageView dropped = (ImageView) view;

                //update the text in the target view to reflect the data being dropped
                dropTarget.setText(dropped.getImageAlpha());

                //make it bold to highlight the fact that an item has been dropped
                dropTarget.setTypeface(Typeface.DEFAULT_BOLD);

                //if an item has already been dropped here, there will be a tag
                Object tag = dropTarget.getTag();

                //if there is already an item here, set it back visible in its original place
                if(tag!=null)
                {
                    //the tag is the view id already dropped here
                    int existingID = (Integer)tag;
                    //set the original view visible again
                    findViewById(existingID).setVisibility(View.VISIBLE);
                }
                //set the tag in the target view being dropped on - to the ID of the view being dropped
                dropTarget.setTag(dropped.getId());
                break;
            case DragEvent.ACTION_DRAG_ENDED:
                //no action necessary
                break;
            default:
                break;
            }
            return true;
        }
    } 
}
我的xml文件如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="66dp"
    android:orientation="vertical"
    android:padding="10dp"
    android:paddingLeft="50dp"
    android:paddingRight="50dp" >

    <TextView
        android:id="@+id/choice_1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_marginTop="46dp"
        android:background="@drawable/choice"
        android:gravity="center"
        android:text="@string/choice_1" />

    <TextView
        android:id="@+id/choice_2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_marginTop="57dp"
        android:background="@drawable/choice"
        android:gravity="center"
        android:text="@string/choice_2" />
</LinearLayout>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/linearLayout1"
    android:layout_marginBottom="16dp"
    android:layout_toRightOf="@+id/imageButton1"
    android:contentDescription="@string/image_desc"
    android:src="@drawable/firefighter" />

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:contentDescription="@string/image_desc"
    android:src="@drawable/clown" />


关于我做错了什么有什么建议吗?提前谢谢。

您是在讨论您的问题还是在询问逻辑?这个房间只是用来讨论问题的,不是问整个逻辑,请你自己试试。嗨@umesh我已经编辑了上面的问题。请注意并提出相应建议。谢谢。:-)