Android-->Launcher拖拽事件详解【androidICS4.0--Launcher系列二】

来源:互联网 发布:最新网络手游 编辑:程序博客网 时间:2024/05/29 03:16
AndroidICS4.0版本的launcher拖拽的流程,基本和2.3的相似。就是比2.3写的封装的接口多了一些,比如删除类的写法就多了个类。等等。4.0的改变有一些,但是不是特别大。这个月一直在改动Launcher的缩略图的效果,4.0的缩略图的功能没有实现,还得从2.3的Launcher中摘出来。通过做这个缩略图对Launcher的模块有一点点了解,拿来分享一下Launcher拖拽的工作流程。微笑有图有真相!吐舌头

转载请标明出处:http://blog.csdn.net/wdaming1986/article/details/7671318

(1) 先来看看类之间的继承关系

图(1)

(2)再来看看Launcher拖拽流程的时序图

图(2)

下面咱们分步来解析Launcher拖拽的详细过程:

step 1 :先来看看Launcher.java这个类的onCreate()方法中的setupViews()方法中的一部分代码:

[java] view plaincopyprint?
  1. <STRONG> </STRONG><SPAN style="COLOR: #000000; FONT-SIZE: 16px">// Setup the workspace
  2. mWorkspace.setHapticFeedbackEnabled(false);
  3. mWorkspace.setOnLongClickListener(this);
  4. mWorkspace.setup(dragController);
  5. dragController.addDragListener(mWorkspace);</SPAN>

Workspace设置长按事件的监听交给了Launcher.java这个类了。所以在主屏上长按事件会走到Launcher.java----->

onLongClick()这个方法中去;

step 2 :接着我们来看看Launcher.java中onLongClick()的代码:

[java] view plaincopyprint?
  1. public boolean onLongClick(View v) {
  2. ··············
  3. // The hotseat touch handling does not go through Workspace, and we always allow long press
  4. // on hotseat items.
  5. final View itemUnderLongClick = longClickCellInfo.cell;
  6. boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
  7. if (allowLongPress && !mDragController.isDragging()) {
  8. if (itemUnderLongClick == null) {
  9. // User long pressed on empty space
  10. mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
  11. HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
  12. startWallpaper();
  13. } else {
  14. if (!(itemUnderLongClick instanceof Folder)) {
  15. // User long pressed on an item
  16. mWorkspace.startDrag(longClickCellInfo);
  17. }
  18. }
  19. }
  20. return true;
  21. }

通过itemUnderLongClick == null 来判断,在屏幕上触发长按事件是否选中了shortcut或者widget。如果为空,就启动桌面的壁纸,else,就把拖拽事件往Workspace.java这个类传递。

Step 3 :通过mWorkspace.startDrag(longClickCellInfo),把长按事件传递给workspace来处理,具体来看代码:

[java] view plaincopyprint?
  1. void startDrag(CellLayout.CellInfo cellInfo) {
  2. View child = cellInfo.cell;
  3. // Make sure the drag was started by a long press as opposed to a long click.
  4. if (!child.isInTouchMode()) {
  5. return;
  6. }
  7. mDragInfo = cellInfo;
  8. //隐藏拖拽的child
  9. child.setVisibility(GONE);
  10. child.clearFocus();
  11. child.setPressed(false);
  12. final Canvas canvas = new Canvas();
  13. // We need to add extra padding to the bitmap to make room for the glow effect
  14. final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
  15. // The outline is used to visualize where the item will land if dropped
  16. mDragOutline = createDragOutline(child, canvas, bitmapPadding);
  17. beginDragShared(child, this);
  18. }

上面的代码主要做的工作是:把正在拖拽的这个view隐藏掉,在主屏幕上绘制一个蓝色的,大小和图标相似的一个边框,以表示能在主屏的这个位置放置。

Step 4 :接着调用beginDragShared(child, this)这个方法,代码如下:

[java] view plaincopyprint?
  1. public void beginDragShared(View child, DragSource source) {
  2. ··· ···
  3. // Clear the pressed state if necessary
  4. if (child instanceof BubbleTextView) {
  5. BubbleTextView icon = (BubbleTextView) child;
  6. icon.clearPressedOrFocusedBackground();
  7. }
  8. mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(),
  9. DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, dragRect);
  10. b.recycle();
  11. }

这个方法做的工作是:开始进行拖拽,绘制正在拖拽的图片,把拖拽的事件交给DragController来处理。

Step 5 :接着来看看mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(), DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, dragRect)这个方法,代码如下:

[java] view plaincopyprint?
  1. public void startDrag(Bitmap b, int dragLayerX,int dragLayerY,
  2. DragSource source, Object dragInfo, int dragAction, Point dragOffset, Rect dragRegion) {
  3. ··· ···
  4. mDragObject.dragComplete = false;
  5. mDragObject.xOffset = mMotionDownX - (dragLayerX + dragRegionLeft);
  6. mDragObject.yOffset = mMotionDownY - (dragLayerY + dragRegionTop);
  7. mDragObject.dragSource = source;
  8. mDragObject.dragInfo = dragInfo;
  9. mVibrator.vibrate(VIBRATE_DURATION);
  10. final DragView dragView = mDragObject.dragView =new DragView(mLauncher, b, registrationX,
  11. registrationY, 0, 0, b.getWidth(), b.getHeight());
  12. if (dragOffset != null) {
  13. dragView.setDragVisualizeOffset(new Point(dragOffset));
  14. }
  15. if (dragRegion !=null) {
  16. dragView.setDragRegion(new Rect(dragRegion));
  17. }
  18. dragView.show(mMotionDownX, mMotionDownY);
  19. handleMoveEvent(mMotionDownX, mMotionDownY);
  20. }

这个方法的作用是:计算要拖拽的view的大小,显示在workspace上,dragView.show(mMotionDownX, mMotionDownY);这个show()会根据手指的移动而移动的。然后在通过handleMoveEvent()方法来分发拖拽的目标到底在哪个目标上。DropTarget一共有3个:workspace,ButtonDropTarget(删除类),Folder;他们分别实现了DropTarget这个接口。

下面来看看这个接口有一下几个方法:

[java] view plaincopyprint?
  1. boolean isDropEnabled();
  2. void onDrop(DragObject dragObject);
  3. void onDragEnter(DragObject dragObject);
  4. void onDragOver(DragObject dragObject);
  5. void onDragExit(DragObject dragObject);
  6. DropTarget getDropTargetDelegate(DragObject dragObject);
  7. boolean acceptDrop(DragObject dragObject);
  8. // These methods are implemented in Views
  9. void getHitRect(Rect outRect);
  10. void getLocationInDragLayer(int[] loc);
  11. int getLeft();
  12. int getTop();

这些方法不是每个类继承了DropTarget的接口,都要把每个方法都实现,这要看具体的需要来定。

另外这个接口中有个内部类-----DragObject:如下

[java] view plaincopyprint?
  1. class DragObject {
  2. public int x = -1;
  3. public int y = -1;
  4. /** X offset from the upper-left corner of the cell to where we touched. */
  5. public int xOffset = -1;
  6. /** Y offset from the upper-left corner of the cell to where we touched. */
  7. public int yOffset = -1;
  8. /** This indicates whether a drag is in final stages, either drop or cancel. It
  9. * differentiates onDragExit, since this is called when the drag is ending, above
  10. * the current drag target, or when the drag moves off the current drag object.
  11. */
  12. public boolean dragComplete =false;
  13. /** The view that moves around while you drag. */
  14. public DragView dragView = null;
  15. /** The data associated with the object being dragged */
  16. public Object dragInfo =null;
  17. /** Where the drag originated */
  18. public DragSource dragSource =null;
  19. /** Post drag animation runnable */
  20. public Runnable postAnimationRunnable =null;
  21. /** Indicates that the drag operation was cancelled */
  22. public boolean cancelled =false;
  23. public DragObject() {
  24. }
  25. }

这个类的作用是存储一些坐标,拖拽点距离整个view左上角x轴上的距离,y轴上的距离,还有一些拖拽的信息都保存在这个类中,还有动画线程类等等。在拖拽过程中这些信息都是会用到的。

Step 6 :接着来看看handleMoveEvent()这个类,这个类频繁被调用,因为在DragLayer.java这个类中onTouchEvent()方法,最后调用的是 mDragController.onTouchEvent(ev)这个方法,长按后,移动的事件就传递到了DragController中的onTouchEvent()方法中,先来看看mDragController.onTouchEvent(ev)这个方法,代码如下:

[java] view plaincopyprint?
  1. /**
  2. * Call this from a drag source view.
  3. */
  4. public boolean onTouchEvent(MotionEvent ev) {
  5. if (!mDragging) {
  6. return false;
  7. }
  8. final int action = ev.getAction();
  9. final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
  10. final int dragLayerX = dragLayerPos[0];
  11. final int dragLayerY = dragLayerPos[1];
  12. switch (action) {
  13. case MotionEvent.ACTION_DOWN:
  14. // Remember where the motion event started
  15. mMotionDownX = dragLayerX;
  16. mMotionDownY = dragLayerY;
  17. if ((dragLayerX < mScrollZone) || (dragLayerX > mScrollView.getWidth() - mScrollZone)) {
  18. mScrollState = SCROLL_WAITING_IN_ZONE;
  19. mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
  20. } else {
  21. mScrollState = SCROLL_OUTSIDE_ZONE;
  22. }
  23. break;
  24. case MotionEvent.ACTION_MOVE:
  25. handleMoveEvent(dragLayerX, dragLayerY);
  26. break;
  27. case MotionEvent.ACTION_UP:
  28. // Ensure that we've processed a move event at the current pointer location.
  29. handleMoveEvent(dragLayerX, dragLayerY);
  30. mHandler.removeCallbacks(mScrollRunnable);
  31. if (mDragging) {
  32. drop(dragLayerX, dragLayerY);
  33. }
  34. endDrag();
  35. break;
  36. case MotionEvent.ACTION_CANCEL:
  37. cancelDrag();
  38. break;
  39. }
  40. return true;
  41. }

在这个方法中清楚的可以看见handleMoveEvent()这个方法会在move,up的时候频繁地调用。

现在再来看看这个handleMoveEvent()方法,看看它的庐山真面目:

[java] view plaincopyprint?
  1. private void handleMoveEvent(int x,int y) {
  2. mDragObject.dragView.move(x, y);
  3. // Drop on someone?
  4. final int[] coordinates = mCoordinatesTemp;
  5. DropTarget dropTarget = findDropTarget(x, y, coordinates);
  6. mDragObject.x = coordinates[0];
  7. mDragObject.y = coordinates[1];
  8. if (dropTarget !=null) {
  9. DropTarget delegate = dropTarget.getDropTargetDelegate(mDragObject);
  10. if (delegate != null) {
  11. dropTarget = delegate;
  12. }
  13. if (mLastDropTarget != dropTarget) {
  14. if (mLastDropTarget != null) {
  15. mLastDropTarget.onDragExit(mDragObject);
  16. }
  17. dropTarget.onDragEnter(mDragObject);
  18. }
  19. dropTarget.onDragOver(mDragObject);
  20. } else {
  21. if (mLastDropTarget !=null) {
  22. mLastDropTarget.onDragExit(mDragObject);
  23. }
  24. }
  25. mLastDropTarget = dropTarget;
  26. ··· ···
  27. }

这个方法的作用:通过findDropTarget(x, y, coordinates),来判断在哪个拖拽目标里面,然后通过下面的if判断来执行不同的onDragOver,onDragExit等的方法。这样就在相应的类中去做处理,以后的事情就明朗了。这就是Launcher的拖拽事件的分发与处理,用到了MVC的思想,代码阅读起来还是比较顺利的。有图有真相。

欢迎大家留言讨论相关问题。

原创粉丝点击