5.User Interface/Drag and Drop
package mirror.android.dragdrop; import android.app.Activity; import android.content.ClipDescription; import android.os.Bundle; import android.view.DragEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.DragShadowBuilder; import android.view.View.OnClickListener; import android.view.View.OnDragListener; import android.view.View.OnLongClickListener; import android.widget.Button; import android.widget.Toast; public class DragActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_drag); button = (Button)findViewById(R.id.dragButton); myDragEventListener mDragListenr = new myDragEventListener(); button.setOnDragListener(mDragListenr); button.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { button.startDrag(null, new DragShadowBuilder(v), (Object)v, 0); return true; } }); } class myDragEventListener implements OnDragListener{ @Override public boolean onDrag(View v, DragEvent event) { // Defines a variable to store the action type for the incoming event final int action = event.getAction(); switch (action) { case DragEvent.ACTION_DRAG_STARTED: // Returns false. During the current drag and drop operation, this View will // not receive events again until ACTION_DRAG_ENDED is sent. return false; case DragEvent.ACTION_DRAG_ENTERED: //when the drag shadow has just entered the bounding box of the View return true; case DragEvent.ACTION_DRAG_LOCATION: //receives this event action type after it receives ACTION_DRAG_ENTERED return true; case DragEvent.ACTION_DRAG_EXITED: //receives a ACTION_DRAG_ENTERED and at least one ACTION_DRAG_LOCATION return true; case DragEvent.ACTION_DROP: //when the user releases the drag shadow over the View object return true; case DragEvent.ACTION_DRAG_ENDED: //when the system is ending the drag operation return true; default: break; } return false; } } }
Example2:
http://blog.csdn.net/mayingcai1987/article/details/6221988
package mirror.android.draganddropactivity; import android.content.Context; import android.util.AttributeSet; import android.view.View; public class AreaOne extends View { public AreaOne(Context context) { super(context); } public AreaOne(Context context, AttributeSet attrs){ super(context,attrs); } }
package mirror.android.draganddropactivity; import android.content.Context; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.util.Log; import android.view.DragEvent; import android.view.View; public class AreaTwo extends View { public AreaTwo(Context context) { super(context); } public AreaTwo(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onDragEvent(DragEvent event) { boolean result = false; switch (event.getAction()) { case DragEvent.ACTION_DRAG_STARTED: { Log.i("mayingcai", "有View开始被拖动!"); //在拖动开始时,只有返回true,后面的动作(ACTION_DRAG_ENTERED, ACTION_DRAG_LOCATION, ACTION_DROP)才会被执行! result = true; break; } case DragEvent.ACTION_DRAG_ENTERED: { Log.i("mayingcai", "被拖动的View进入当前View!"); break; } case DragEvent.ACTION_DRAG_LOCATION: { Log.i("mayingcai", "被拖动的View进入当前View后,位置发生改变!"); break; } case DragEvent.ACTION_DROP: { Log.i("mayingcai", "拖动的View被放入当前View!"); //在放时交互两个View的背景。 View mDragView = (View) event.getLocalState(); Drawable mDragViewBackgroud = mDragView.getBackground(); mDragView.setBackgroundDrawable(this.getBackground()); this.setBackgroundDrawable(mDragViewBackgroud); break; } case DragEvent.ACTION_DRAG_ENDED: { Log.i("mayingcai", "拖动结束!"); break; } case DragEvent.ACTION_DRAG_EXITED: { Log.i("mayingcai", "拖动退出!"); break; } default: break; } return result; } }
<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="mirror.android.draganddropactivity.DragAndDropActivity" > <mirror.android.draganddropactivity.AreaOne android:id = "@+id/areaOne" android:layout_width = "100dip" android:layout_height = "100dip" android:background = "#FF0000"/> <TextView android:layout_width = "100dip" android:layout_height = "100dip"/> <mirror.android.draganddropactivity.AreaTwo android:id = "@+id/areaTwo" android:layout_width = "100dip" android:layout_height = "100dip" android:background = "#00FF00" /> </LinearLayout>
package mirror.android.draganddropactivity; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.DragShadowBuilder; import android.view.View.OnLongClickListener; public class DragAndDropActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_drag_and_drop); final View mAreaOne = findViewById(R.id.areaOne); mAreaOne.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { //长按AreaOne开始启动 mAreaOne.startDrag(null, new DragShadowBuilder(v), (Object)v, 0); return true; } }); } }