直接上代碼:
package xutao.myapplication; import android.content.ClipData; import android.graphics.drawable.Drawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.DragEvent; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.ImageView; import android.widget.LinearLayout; public class MainActivity extends AppCompatActivity { private ImageView mIv1; private LinearLayout mLl1; private ImageView mIv2; private LinearLayout mLl2; private Drawable normalShape; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); normalShape = getResources().getDrawable(R.drawable.shape); initView(); initEvent(); } private void initEvent() { mIv1.setOnTouchListener(MyTouchEvent); mIv2.setOnTouchListener(MyTouchEvent); mLl1.setOnDragListener(MyDragListener); mLl2.setOnDragListener(MyDragListener); } private void initView() { mIv1 = (ImageView) findViewById(R.id.iv1); mLl1 = (LinearLayout) findViewById(R.id.ll1); mIv2 = (ImageView) findViewById(R.id.iv2); mLl2 = (LinearLayout) findViewById(R.id.ll2); } View.OnTouchListener MyTouchEvent = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { ClipData data = ClipData.newPlainText("", ""); View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v); v.startDrag(data, shadowBuilder, v, 0); v.setAlpha((float) 0.5); return true; } else { return false; } } }; View.OnDragListener MyDragListener=new View.OnDragListener() { @Override public boolean onDrag(View v, DragEvent event) { View visitorView = (View) event.getLocalState(); ViewGroup visitorOwner = (ViewGroup) visitorView.getParent(); ViewGroup visitedOwner = (ViewGroup) v; View visitedImage = (View)visitedOwner.getChildAt(0); switch (event.getAction()) { case DragEvent.ACTION_DRAG_STARTED: break; case DragEvent.ACTION_DRAG_ENTERED: v.setAlpha((float)0.7); v.setScaleX(0.7f); v.setScaleY(0.7f); v.setBackground(normalShape); break; case DragEvent.ACTION_DRAG_EXITED: v.setAlpha((float)1.0); v.setScaleX(1); v.setScaleY(1); v.setBackground(normalShape); break; case DragEvent.ACTION_DROP: visitorView.setAlpha((float)1.0); visitedImage.setAlpha((float)1.0); if (visitorOwner != visitedOwner) { /* * swap the imageviews from the containers */ visitedOwner.removeView(visitedImage); visitorOwner.removeView(visitorView); visitorOwner.addView(visitedImage); LinearLayout container = (LinearLayout) v; container.addView(visitorView); } else { } break; case DragEvent.ACTION_DRAG_ENDED: v.setBackgroundDrawable(normalShape); v.setAlpha((float)1.0); visitedImage.setAlpha((float)1.0); default: break; } return true; } }; }
layout.XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="xutao.myapplication.MainActivity"> <LinearLayout android:id="@+id/ll1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/iv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> </LinearLayout> <LinearLayout android:id="@+id/ll2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="100dp"> <ImageView android:id="@+id/iv2" android:layout_width="50dp" android:layout_height="50dp" android:scaleType="centerCrop" android:src="@drawable/logo" /> </LinearLayout> </LinearLayout>
demo下載鏈接:http://download.csdn.net/download/xutaojxx/10036214