如何将一个ImageView拖放到另一个ImageView上?

3

我对Android还比较陌生,但已经成功地完成了一些工作。

我现在想实现一个简单的拖放活动,允许用户:

  • 将一个形状“ImageView”拖放到另一个形状“ImageView”上。
  • 如果图像匹配 - 它应该替换它所放置的图像,
    否则 - 它应该回弹到原来的位置。

我知道这意味着在我的放置事件中创建一个if/else块,但是经过阅读许多教程后我仍然无法把需要的东西组合起来。而且我目前没有足够的Java知识来完成这个任务。

目前我有一个布局,其中包含6个ImageView,其中3个是静态的,3个可以在屏幕上移动并放置在布局上,但不能放置在ImageView上。

我认为这是因为我将DragListener设计为我的布局,而不是ImageView,但我有点迷失了,有人可以帮我吗?

这是我的代码:

DragandDrop.java    

import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.app.Activity;

public class DragandDrop extends Activity implements OnTouchListener, OnDragListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.draganddrop);
    findViewById(R.id.squareImage).setOnTouchListener(this);
    findViewById(R.id.circleImage).setOnTouchListener(this);
    findViewById(R.id.triangleImage).setOnTouchListener(this);
    findViewById(R.id.top_container).setOnDragListener(this);
    findViewById(R.id.bottom_container).setOnDragListener(this);
    findViewById(R.id.squareImage1).setOnDragListener(this);
    findViewById(R.id.circleImage1).setOnDragListener(this);
    findViewById(R.id.triangleImage1).setOnDragListener(this);
}

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

@Override
public boolean onDrag(View v, DragEvent e) {
    if (e.getAction()==DragEvent.ACTION_DROP) {
        View view = (View) e.getLocalState();
        ViewGroup from = (ViewGroup) view.getParent();
        from.removeView(view);
        LinearLayout to = (LinearLayout) v;
        to.addView(view);
        view.setVisibility(View.VISIBLE);
    }
    return true;
}

}

draganddrop.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".draganddrop"
android:background="@drawable/dragshapes"
android:orientation="vertical"
android:id="@+id/dropLayout">


<LinearLayout
    android:id="@+id/top_container"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@android:color/transparent"
    android:orientation="horizontal"
    android:gravity="center">

    <ImageView
        android:contentDescription="@string/square_text_content"
        android:id="@+id/squareImage1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@drawable/dragsquare1" />

    <ImageView
        android:contentDescription="@string/circle_text_content"
        android:id="@+id/circleImage1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@drawable/dragcircle1" />

    <ImageView
        android:contentDescription="@string/triangle_text_content"
        android:id="@+id/triangleImage1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@drawable/dragtriangle1" />



</LinearLayout>

<LinearLayout
    android:id="@+id/bottom_container"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@android:color/transparent"
    android:orientation="horizontal"
    android:gravity="center">
    <ImageView
        android:contentDescription="@string/square_text_content"
        android:id="@+id/squareImage"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@drawable/dragsquare" />

    <ImageView
        android:contentDescription="@string/circle_text_content"
        android:id="@+id/circleImage"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@drawable/dragcircle" />

    <ImageView
        android:contentDescription="@string/triangle_text_content"
        android:id="@+id/triangleImage"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@drawable/dragtriangle" />

</LinearLayout>

</LinearLayout>

请查看以下教程,可能会对您有所帮助。http://grishma102.blogspot.in/2013/11/simple-drag-drop-views-on-screen-in.html 还可以查看更多内容:http://grishma102.blogspot.in/2013/10/drag-and-drop-functionality-to-move.html - GrIsHu
谢谢,但这只是一堆大量的代码行,虽然我可以解释其中很多内容,但它并没有说明如何实现它。 - martinseal1987
1个回答

4

移除容器的拖动监听器设置

findViewById(R.id.top_container).setOnDragListener(this);  //Remove this
findViewById(R.id.bottom_container).setOnDragListener(this);  //Remove this
  • 检查拖动的图像视图是否与您要放置的视图匹配。如果它们匹配:
    • 将放置的ImageView的背景设置为拖动的ImageView的背景。
      (其他尺寸按照您的xml文件中的规定)

示例代码:

@Override
public boolean onDrag(View v, DragEvent e) {
    if (e.getAction()==DragEvent.ACTION_DROP) {
        View view = (View) e.getLocalState();
        
        if (view.getId()==R.id.squareImage1 && v.getId()==R.id.squareImage) {
            ViewGroup from = (ViewGroup) view.getParent();
            from.removeView(view);
            v.setBackground(@drawable/dragsquare1);  //TODO: Change this pseudo code.
            return true;
        
        } else if(view.getId()==R.id.circleImage1 && v.getId()==R.id.circleImage) {
            ViewGroup from = (ViewGroup) view.getParent();
            from.removeView(view);
            v.setBackground(@drawable/dragcircle1);  //TODO: Change this pseudo code.
            return true;

        } else if(view.getId()==R.id.triangleImage1 && v.getId()==R.id.triangleImage) {
            ViewGroup from = (ViewGroup) view.getParent();
            from.removeView(view);
            v.setBackground(@drawable/dragtriangle1);  //TODO: Change this pseudo code.
            return true;

        } else {
            return false;
        }
    }
}

希望这能对您有所帮助...
圣诞快乐

圣诞节快乐,非常感谢 :-) 但是它仍然在Ids上给我错误信息,说找不到符号getId,并且还表示有一些语法错误,意外的分号,缺少)和其他错误。 @nikvs - martinseal1987
抱歉,我在 getId 中忘记加上“()”。 - nikvs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接