如何将图像拖放到图像上

时间:2013-12-24 10:52:12

标签: android drag-and-drop imageview

对于明天(英国)的大家好,欢乐和圣诞快乐,我对Android很新,但已经成功管理了一些点点滴滴我现在想要实现的是一个简单的拖放活动,允许用户将形状ImageView拖放到另一个ImageView上,如果图像匹配则它应该替换它重叠的图像,如果它没有它应该快速回到原来的位置,我知道这意味着在我的drop事件中创建一个if / else块但是在翻阅了很多教程之后我无法拼凑出我想要的东西,而且我目前还没有足够的java知识来解决这个问题目前我有一个布局,其中包含6个图像视图,3个是静态的,另外3个可以移动到屏幕并放置在布局上但不放在图像视图上,我认为这是因为我已经将拖拉机设置为我的布局,而不是我的图像视图,但我有点迷失,有人可以帮我吗?谢谢你的任何建议。 这是我的代码

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>

1 个答案:

答案 0 :(得分:3)

删除容器的设置拖动侦听器

  findViewById(R.id.top_container).setOnDragListener(this);//remove this
    findViewById(R.id.bottom_container).setOnDragListener(this);//remove this

检查拖动的图像视图是否与您删除的视图匹配。如果它们匹配,则将删除的图像视图的背景设置为拖动的背景。(其他尺寸与您的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;
}

        }

}

希望这可以帮助你...圣诞节