android launcher3拖拽事件響應解析長按事件處理
AndroidICS4.0版本的launcher拖拽的流程,基本和2.3的相似。就是比2.3寫的封裝的介面多了一些,比如刪除類的寫法就多了個類。等等。4.0的改變有一些,但是不是特別大。這個月一直在改動Launcher的縮圖的效果,4.0的縮圖的功能沒有實現,還得從2.3的Launcher中摘出來。通過做這個縮圖對Launcher的模組有一點點了解,拿來分享一下Launcher拖拽的工作流程。有圖有真相!
(1) 先來看看類之間的繼承關係
圖(1)
(2)再來看看Launcher拖拽流程的時序圖
圖(2)
下面咱們分步來解析Launcher拖拽的詳細過程:
step 1 :先來看看Launcher.java這個類的onCreate()方法中的setupViews()方法中的一部分程式碼:
[java] view plaincopyprint?-
<strong> </strong><span style=
- mWorkspace.setHapticFeedbackEnabled(false);
- mWorkspace.setOnLongClickListener(this);
- mWorkspace.setup(dragController);
- dragController.addDragListener(mWorkspace);</span>
Workspace設定長按事件的監聽交給了Launcher.java這個類了。所以在主屏上長按事件會走到Launcher.java----->
onLongClick()這個方法中去;
step 2 :接著我們來看看Launcher.java中onLongClick()的程式碼:
[java] view plaincopyprint?- publicboolean onLongClick(View v) {
- ··············
- // The hotseat touch handling does not go through Workspace, and we always allow long press
- // on hotseat items.
- final View itemUnderLongClick = longClickCellInfo.cell;
- boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
- if (allowLongPress && !mDragController.isDragging()) {
- if (itemUnderLongClick == null) {
- // User long pressed on empty space
- mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
- HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
- startWallpaper();
- } else {
- if (!(itemUnderLongClick instanceof Folder)) {
- // User long pressed on an item
- mWorkspace.startDrag(longClickCellInfo);
- }
- }
- }
- returntrue;
- }
通過itemUnderLongClick == null 來判斷,在螢幕上觸發長按事件是否選中了shortcut或者widget。如果為空,就啟動桌面的桌布,else,就把拖拽事件往Workspace.java這個類傳遞。
Step 3 :通過mWorkspace.startDrag(longClickCellInfo),把長按事件傳遞給workspace來處理,具體來看程式碼:
[java] view plaincopyprint?- void startDrag(CellLayout.CellInfo cellInfo) {
- View child = cellInfo.cell;
- // Make sure the drag was started by a long press as opposed to a long click.
- if (!child.isInTouchMode()) {
- return;
- }
- mDragInfo = cellInfo;
- //隱藏拖拽的child
- child.setVisibility(GONE);
- child.clearFocus();
- child.setPressed(false);
- final Canvas canvas = new Canvas();
- // We need to add extra padding to the bitmap to make room for the glow effect
- finalint bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
- // The outline is used to visualize where the item will land if dropped
- mDragOutline = createDragOutline(child, canvas, bitmapPadding);
- beginDragShared(child, this);
- }
上面的程式碼主要做的工作是:把正在拖拽的這個view隱藏掉,在主螢幕上繪製一個藍色的,大小和圖示相似的一個邊框,以表示能在主屏的這個位置放置。
Step 4 :接著呼叫beginDragShared(child, this)這個方法,程式碼如下:
[java] view plaincopyprint?- publicvoid beginDragShared(View child, DragSource source) {
- ··· ···
- // Clear the pressed state if necessary
- if (child instanceof BubbleTextView) {
- BubbleTextView icon = (BubbleTextView) child;
- icon.clearPressedOrFocusedBackground();
- }
- mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(),
- DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, dragRect);
- b.recycle();
- }
這個方法做的工作是:開始進行拖拽,繪製正在拖拽的圖片,把拖拽的事件交給DragController來處理。
Step 5 :接著來看看mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(), DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, dragRect)這個方法,程式碼如下:
[java] view plaincopyprint?- publicvoid startDrag(Bitmap b, int dragLayerX, int dragLayerY,
- DragSource source, Object dragInfo, int dragAction, Point dragOffset, Rect dragRegion) {
- ··· ···
- mDragObject.dragComplete = false;
- mDragObject.xOffset = mMotionDownX - (dragLayerX + dragRegionLeft);
- mDragObject.yOffset = mMotionDownY - (dragLayerY + dragRegionTop);
- mDragObject.dragSource = source;
- mDragObject.dragInfo = dragInfo;
- mVibrator.vibrate(VIBRATE_DURATION);
- final DragView dragView = mDragObject.dragView = new DragView(mLauncher, b, registrationX,
- registrationY, 0, 0, b.getWidth(), b.getHeight());
- if (dragOffset != null) {
- dragView.setDragVisualizeOffset(new Point(dragOffset));
- }
- if (dragRegion != null) {
- dragView.setDragRegion(new Rect(dragRegion));
- }
- dragView.show(mMotionDownX, mMotionDownY);
- handleMoveEvent(mMotionDownX, mMotionDownY);
- }
這個方法的作用是:計算要拖拽的view的大小,顯示在workspace上,dragView.show(mMotionDownX, mMotionDownY);這個show()會根據手指的移動而移動的。然後在通過handleMoveEvent()方法來分發拖拽的目標到底在哪個目標上。DropTarget一共有3個:workspace,ButtonDropTarget(刪除類),Folder;他們分別實現了DropTarget這個介面。
下面來看看這個介面有一下幾個方法:
[java] view plaincopyprint?- boolean isDropEnabled();
- void onDrop(DragObject dragObject);
- void onDragEnter(DragObject dragObject);
- void onDragOver(DragObject dragObject);
- void onDragExit(DragObject dragObject);
- DropTarget getDropTargetDelegate(DragObject dragObject);
- boolean acceptDrop(DragObject dragObject);
- // These methods are implemented in Views
- void getHitRect(Rect outRect);
- void getLocationInDragLayer(int[] loc); <