将ImageView拖放到另一个ImageView上

时间:2015-06-05 16:04:12

标签: android drag-and-drop imageview

我正在开发一款Android应用,用户可以将ImageView拖到另一个ImageViewImageView需要更改为拖动ImageView如果它符合某个条件,如果它没有它应该快速回到原来的位置。我试过这样做但我无法让它工作

xml文件背后的代码

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:columnWidth="320dp"
android:orientation="vertical"
android:rowCount="14"
android:stretchMode="columnWidth" >

<LinearLayout
    android:id="@+id/topleft"
    android:layout_width="match_parent"
    android:layout_height="413dp"
    android:layout_row="0"
    android:background="@drawable/shape" >

    <ImageView
        android:id="@+id/test_house"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/test_house" />
</LinearLayout>

<LinearLayout
    android:id="@+id/topright"
    android:layout_width="match_parent"
    android:layout_height="106dp"
    android:background="@drawable/shape"
    android:layout_row="13"
    android:layout_column="0"
    android:weightSum="1">

    <ImageView
        android:id="@+id/house"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/house"
        android:layout_weight="0.26" />

    <ImageView
        android:id="@+id/face"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/face"
        android:layout_weight="0.24" />

    <ImageView
        android:id="@+id/duck"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/duck"
        android:layout_weight="0.24" />

    </LinearLayout>

 </GridLayout>

java文件背后的代码

public class cs_game_one extends Activity {

boolean state;

/** Called when the activity is first created. */

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cs_game_one);
    findViewById(R.id.test_house).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.house).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.face).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.duck).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
    findViewById(R.id.topright).setOnDragListener(new MyDragListener());


    }

private final class MyTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new   View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            return false;
        }
    }
}

class MyDragListener implements OnDragListener {


    @Override
    public boolean onDrag(View v, DragEvent e) {
        if (e.getAction() == DragEvent.ACTION_DROP) {
            View view = (View) e.getLocalState();
            if (view.getId() == R.id.house && v.getId() == R.id.test_house){                       
                ViewGroup from = (ViewGroup) view.getParent();
                from.removeView(view);
                v.setBackgroundResource(R.drawable.house);

                state = true;

            } else {
                state =  false;
            }

        }
    return state;
    }

    }
}

1 个答案:

答案 0 :(得分:0)

v.setBackgroundResource(R.drawable.house);之后添加: v.invalidate(); 这告诉您的UI线程需要重绘您尝试设置为Drawable房屋的视图。

else声明中,请更改为:

state = false;
view.setVisibility(View.VISIBLE); //This will make it look like your dragged image snaps back to it's original spot.

关于Android中拖放的有趣之处在于,当您开始拖动时,它只是在拖动开始时隐藏您最初触摸的图像(代码中为view)。您在手指下围绕屏幕移动的图像实际上是拖影。因此,通过更改view语句中else的可见性,图片看起来就会快照回到您开始拖动的位置。