workspace 的事件处里流程
设置workspace 的点击事件 在Launcher里面处理
mWorkspace.setOnLongClickListener(this);
1、launcher 中的长按事件将会回调到workspace
if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
// User long pressed on an item
mWorkspace.startDrag(longClickCellInfo);
}
2、workspace 中的回调方法
-->startDrag(cellInfo, false);
-->隐藏要拖拽的 Viewchild.setVisibility(INVISIBLE);
-->准备当前CellLayout 数据 layout.prepareChildForDrag(child);
-->开始拖拽 beginDragShared(child, this, accessible);
3、beginDragShared
// The outline is used to visualize where the item will land if dropped
//创建拖拽阴影
mDragOutline = createDragOutline(child, DRAG_BITMAP_PADDING);
-->//是否要回到默认屏幕 mLauncher.onDragStarted(child);
-->//创建拖拽view Bitmap createDragBitmap(child, padding);
-->//当前view 在 draylayer 上的坐标(位置)计算拖拽时的 rect
-->//调用DragController 开始拖拽并返回正在拖拽时的view
-->//DragView dv = mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(),
DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, dragRect, scale, accessible);
参数:
B:位图显示拖动图像。它将re-scaled扩大规模。
DragLayer dragLayerX :DragLayer 上的x位置
DragLayer dragLayerY :DragLayer 上的y位置
source源对象代表拖源自哪里(workpace)
dragInfo:与被拖动的对象相关的数据
dragAction:拖动行动:DRAG_ACTION_MOVE或 DRAG_ACTION_COPY
dragRegion:坐标内的位图b项被拖动的位置。让拖感觉更精确,例如你可以夹一个透明的边界
访问这个阻力是否应该发生在可访问性模式
dragOffset:
initialDragViewScale:
-->//对正在拖拽的view 进行设置缩放 dv.setIntrinsicIconScaleFactor
(source.getIntrinsicIconScaleFactor());
4、mDragController.startDrag
-->先隐藏输入法,假如输入法正显示
-->//对注册DragListener 监听的进行回调 通知拖拽开始
for (DragListener listener : mListeners) {
listener.onDragStart(source, dragInfo, dragAction);
}
-->创建拖拽对象DropTarget.DragObject();
-->创建拖拽时的View
-->显示拖拽view ,并拖拽实现
dragView.show(mMotionDownX, mMotionDownY);
handleMoveEvent(mMotionDownX, mMotionDownY);
5、handleMoveEvent(mMotionDownX, mMotionDownY);
private void handleMoveEvent(int x, int y) {
//移动到传进来时的参数
mDragObject.dragView.move(x, y);
// Drop on someone?
final int[] coordinates = mCoordinatesTemp;
//查找放下目标
DropTarget dropTarget = findDropTarget(x, y, coordinates);
mDragObject.x = coordinates[0];
mDragObject.y = coordinates[1];
checkTouchMove(dropTarget);
// Check if we are hovering over the scroll areas
//检查如果我们滚动区域的上空
mDistanceSinceScroll += Math.hypot(mLastTouch[0] - x, mLastTouch[1] - y);
mLastTouch[0] = x;
mLastTouch[1] = y;
checkScrollState(x, y);
}
-->findDropTarget();查找当前放下的target及坐标
-->checkTouchMove(dropTarget);//检查触摸移动
private void checkTouchMove(DropTarget dropTarget) {
if (dropTarget != null) {
if (mLastDropTarget != dropTarget) {//最后一个DropTarget 不=于当前DropTarget
if (mLastDropTarget != null) {
mLastDropTarget.onDragExit(mDragObject);//退出当前的
}
dropTarget.onDragEnter(mDragObject);//进入当前的
}
dropTarget.onDragOver(mDragObject);
} else {
if (mLastDropTarget != null) {
mLastDropTarget.onDragExit(mDragObject);
}
}
mLastDropTarget = dropTarget;
}
--> //检查是否要移别的前一屏或者后一屏checkScrollState(x, y);
@Thunk void checkScrollState(int x, int y) {
final int slop = ViewConfiguration.get(mLauncher).getScaledWindowTouchSlop();// 触摸窗体边沿区域判断
final int delay = mDistanceSinceScroll < slop ? RESCROLL_DELAY : SCROLL_DELAY;//
final DragLayer dragLayer = mLauncher.getDragLayer();
final int forwardDirection = mIsRtl ? SCROLL_RIGHT : SCROLL_LEFT;
final int backwardsDirection = mIsRtl ? SCROLL_LEFT : SCROLL_RIGHT;
if (x < mScrollZone) {//边缘判断
if (mScrollState == SCROLL_OUTSIDE_ZONE) {
mScrollState = SCROLL_WAITING_IN_ZONE;
if (mDragScroller.onEnterScrollArea(x, y, forwardDirection)) {//判断是否 //能发生滚动
dragLayer.onEnterScrollArea(forwardDirection);
mScrollRunnable.setDirection(forwardDirection);
mHandler.postDelayed(mScrollRunnable, delay);
}
}
} else if (x > mScrollView.getWidth() - mScrollZone) {//边缘判断
if (mScrollState == SCROLL_OUTSIDE_ZONE) {
mScrollState = SCROLL_WAITING_IN_ZONE;
if (mDragScroller.onEnterScrollArea(x, y, backwardsDirection)) {
dragLayer.onEnterScrollArea(backwardsDirection);
mScrollRunnable.setDirection(backwardsDirection);
mHandler.postDelayed(mScrollRunnable, delay);
}
}
} else {
clearScrollRunnable();
}
}
6、MotionEvent.ACTION_UP:
PointF isFlingingToDelete(DragSource source)//是否甩动删除
dropOnFlingToDeleteTarget(dragLayerX, dragLayerY, vec);
drop(dragLayerX, dragLayerY);
endDrag();
7、DropTarget
onDrop:就是在放下后做的操作;
onDragEnter:进入之后的操作;
onDragOver:在目标体之上做的操作;
onDragExit:退出之后的操作;