Android中實(shí)現(xiàn)拖拽其實(shí)很簡單湾盒,系統(tǒng)早已經(jīng)提供了api讓我使用萝招,主要用到了View的startDrag(startDragAndDrop API24+)
方法以及OnDragListener
忍级。
startDrag
先來看下方法介紹:
/**
* Starts a drag and drop operation. When your application calls this method, it passes a
* {@link android.view.View.DragShadowBuilder} object to the system. The
* system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
* to get metrics for the drag shadow, and then calls the object's
* {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
* <p>
* Once the system has the drag shadow, it begins the drag and drop operation by sending
* drag events to all the View objects in your application that are currently visible. It does
* this either by calling the View object's drag listener (an implementation of
* {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
* View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
* Both are passed a {@link android.view.DragEvent} object that has a
* {@link android.view.DragEvent#getAction()} value of
* {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
* </p>
* <p>
* Your application can invoke {@link #startDragAndDrop(ClipData, DragShadowBuilder, Object,
* int) startDragAndDrop()} on any attached View object. The View object does not need to be
* the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to be related
* to the View the user selected for dragging.
* </p>
* @param data A {@link android.content.ClipData} object pointing to the data to be
* transferred by the drag and drop operation.
* @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
* drag shadow.
* @param myLocalState An {@link java.lang.Object} containing local data about the drag and
* drop operation. When dispatching drag events to views in the same activity this object
* will be available through {@link android.view.DragEvent#getLocalState()}. Views in other
* activities will not have access to this data ({@link android.view.DragEvent#getLocalState()}
* will return null).
* <p>
* myLocalState is a lightweight mechanism for the sending information from the dragged View
* to the target Views. For example, it can contain flags that differentiate between a
* a copy operation and a move operation.
* </p>
* @param flags Flags that control the drag and drop operation. This can be set to 0 for no
* flags, or any combination of the following:
* <ul>
* <li>{@link #DRAG_FLAG_GLOBAL}</li>
* <li>{@link #DRAG_FLAG_GLOBAL_PERSISTABLE_URI_PERMISSION}</li>
* <li>{@link #DRAG_FLAG_GLOBAL_PREFIX_URI_PERMISSION}</li>
* <li>{@link #DRAG_FLAG_GLOBAL_URI_READ}</li>
* <li>{@link #DRAG_FLAG_GLOBAL_URI_WRITE}</li>
* <li>{@link #DRAG_FLAG_OPAQUE}</li>
* </ul>
* @return {@code true} if the method completes successfully, or
* {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
* do a drag, and so no drag operation is in progress.
*/
public final boolean startDragAndDrop(ClipData data, DragShadowBuilder shadowBuilder,Object myLocalState, int flags)
看到英文就頭大挑社?沒事,我來翻譯解釋一下炕横。
啟動拖放操作依溯。當(dāng)應(yīng)用程序調(diào)用此方法時老厌,它將傳遞一個DragShadowBuilder對象到系統(tǒng)。系統(tǒng)調(diào)用此對象的onProvideShadowMetrics(Point, Point)方法獲取拖動陰影的參數(shù)指標(biāo)黎炉,然后調(diào)用onDrawShadow(Canvas)來繪制陰影梅桩。一旦系統(tǒng)有了拖動陰影,它就開始拖拽操作拜隧,通過將拖拽事件發(fā)送到當(dāng)前可見的應(yīng)用程序中的所有視圖對象宿百。這些視圖可以通過設(shè)置OnDragListener在或者實(shí)現(xiàn)onDragEvent方法接受DragEvent(事件)來響應(yīng)和拖拽事件。
可以看到有四個參數(shù):
ClipData data
其實(shí)就是一個封裝數(shù)據(jù)的對象洪添,通過拖放操作傳遞給接受者垦页。該對象可以存放一個Item的集合,Item可以存放如下數(shù)據(jù):
public static class Item {
final CharSequence mText;
final String mHtmlText;
final Intent mIntent;
Uri mUri;
}
注意到可以存放Intent干奢,因此痊焊,通常可以將參數(shù)存入intent忿峻,然后通過靜態(tài)方法直接創(chuàng)建ClipData對象:
ClipData clipData = ClipData.newIntent("label", intent);
該數(shù)據(jù)可以在監(jiān)聽的中的DragEvent獲取
ClipData clipData = event.getClipData();
簡單點(diǎn)說就是可以將一些數(shù)據(jù)傳遞給拖拽的接受者薄啥,該拖拽其實(shí)可以跨Activity的,如果只是同一個Activity可以使用第三個參數(shù)傳遞數(shù)據(jù)逛尚。
DragShadowBuilder shadowBuilder
用于創(chuàng)建拖拽view是的陰影垄惧,也就是跟隨手指移動的視圖,通常直接使用默認(rèn)即可生成與一個原始view相同绰寞,帶有透明度的陰影:
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
Object myLocalState
當(dāng)你的拖拽行為是在同一個Activity中進(jìn)行時可以傳遞一個任意對象到逊,在監(jiān)聽中可以通過{@link android.view.DragEvent#getLocalState()}獲得。如果是跨Activity拖拽中無法訪問此數(shù)據(jù)滤钱,getLocalState()
將返回null觉壶。
int flags
控制拖放操作的標(biāo)志。因?yàn)闆]有標(biāo)志可以設(shè)置為0件缸,flag標(biāo)志拖動是否可以跨越窗口以及一些訪問權(quán)限(需要API24+)铜靶。
了解了方法參數(shù)含義,接下來就是啟用拖拽了他炊,通常會通過長按來觸發(fā)拖拽:
iv.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, null, 0);
//震動反饋
v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
return true;
}
});
開始拖拽后還要有來接受這些拖拽事件争剿,這就需要OnDragListener了。
OnDragListener
OnDragListener是在View中定義的接口佑稠,用于響應(yīng)拖拽事件,可以通過View的setOnDragListener
方法設(shè)置監(jiān)聽秒梅,有點(diǎn)類似于點(diǎn)擊事件。
public interface OnDragListener {
boolean onDrag(View v, DragEvent event);
}
設(shè)置監(jiān)聽舌胶,實(shí)現(xiàn)onDrag(View v, DragEvent event)
方法捆蜀,其中View是設(shè)置該監(jiān)聽的view,DragEvent是拖拽事件幔嫂,可以通過event.getAction()
獲取具體事件類型辆它,這和TouchEvent非常類似,具體事件類型有如下幾種:
fl_blue.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
//v 永遠(yuǎn)是設(shè)置該監(jiān)聽的view履恩,這里即fl_blue
String simpleName = v.getClass().getSimpleName();
Log.w(BLUE, "view name:" + simpleName);
//獲取事件
int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
Log.i(BLUE, "開始拖拽");
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.i(BLUE, "結(jié)束拖拽");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.i(BLUE, "拖拽的view進(jìn)入監(jiān)聽的view時");
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.i(BLUE, "拖拽的view離開監(jiān)聽的view時");
break;
case DragEvent.ACTION_DRAG_LOCATION:
float x = event.getX();
float y = event.getY();
long l = SystemClock.currentThreadTimeMillis();
Log.i(BLUE, "拖拽的view在監(jiān)聽view中的位置:x =" + x + ",y=" + y);
break;
case DragEvent.ACTION_DROP:
Log.i(BLUE, "釋放拖拽的view");
break;
}
//是否響應(yīng)拖拽事件锰茉,true響應(yīng),返回false只能接受到ACTION_DRAG_STARTED事件切心,后續(xù)事件不會收到
return true;
}
});
此處通過event.getX(); event.getY();
獲取的x飒筑,y是手指(也即是被拖拽view的中心點(diǎn))在監(jiān)聽view的位置片吊。
釋放手指會觸發(fā)ACTION_DRAG_ENDED
事件,如果此時被拖拽的view正好在監(jiān)聽的view中协屡,則會先觸發(fā)ACTION_DROP
事件俏脊。
可以同時有多個view設(shè)置拖拽監(jiān)聽接受事件,我給紅色和藍(lán)色view都設(shè)置了OnDragListener肤晓,然后拖動Android圖片到藍(lán)色區(qū)域后釋放爷贫,可以看到日志如下:
03-09 14:53:54.518 12937-12937/com.huburt.app.androiddrag I/RED: 開始拖拽
03-09 14:53:54.518 12937-12937/com.huburt.app.androiddrag I/BLUE: 開始拖拽
03-09 14:53:55.689 12937-12937/com.huburt.app.androiddrag I/BLUE: 拖拽的view進(jìn)入監(jiān)聽的view時
03-09 14:53:55.689 12937-12937/com.huburt.app.androiddrag I/BLUE: 拖拽的view在BLUE中的位置:x =111.0,y=2.0
03-09 14:53:55.870 12937-12937/com.huburt.app.androiddrag I/BLUE: 拖拽的view在BLUE中的位置:x =112.0,y=23.0
03-09 14:53:56.014 12937-12937/com.huburt.app.androiddrag I/BLUE: 釋放拖拽的view
03-09 14:53:56.017 12937-12937/com.huburt.app.androiddrag I/RED: 結(jié)束拖拽
03-09 14:53:56.017 12937-12937/com.huburt.app.androiddrag I/BLUE: 結(jié)束拖拽
現(xiàn)在我們已經(jīng)可以把Android圖片拖出來,但是還不能把它放入目標(biāo)view补憾,其實(shí)也挺簡單的漫萄,只需要在ACTION_DROP事件做一些處理即可:
case DragEvent.ACTION_DROP:
Log.i(BLUE, "釋放拖拽的view");
ImageView localState = (ImageView) event.getLocalState();
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.topMargin = (int) event.getY() - localState.getWidth() / 2;
layoutParams.leftMargin = (int) event.getX() - localState.getHeight() / 2;
((ViewGroup) localState.getParent()).removeView(localState);
fl_blue.addView(localState, layoutParams);
break;
這里因?yàn)槭窃谕粋€Activity中,我是將拖拽的view直接傳遞過來了盈匾,當(dāng)然也可以只傳遞圖片腾务,然后在接收的view中重新new一個imageview現(xiàn)實(shí)圖片。
運(yùn)行一下就可以看到view可以拖拽到目標(biāo)位置了威酒。
可能文字描述不是特別清楚窑睁,請看demo