Android 深入研究拖放功能Launcher(一)

  Luancher有一個相對比較複雜的功能就是拖放功能,要深入瞭解launcher,深入理解拖放功能是有必要的,這篇blog,我將對launcher的拖放功能做深入的瞭解

  1.首先直觀感受什麼時候開始拖放?我們長按桌面一個應用圖標或者控件的時候拖放就開始了,包括在all app view中長按應用圖標,下面就是我截取的拖放開始的代碼調用堆棧

java代碼:
  1. at com.android.launcher2.DragController.startDrag
  2. at com.android.launcher2.Workspace.startDrag
  3. at com.android.launcher2.Launcher.onLongClick
  4. at android.view.View.performLongClick
  5. at android.widget.TextView.performLongClick
  6. at android.view.View$CheckForLongPress.run
  7. at android.os.Handler.handleCallback
  8. at android.os.Handler.dispatchMessage
  9. at android.os.Looper.loop
複製代碼

        桌面應用圖標由Launcher.onLongClick負責監聽處理,插入斷點debug進入onLongclick函數

java代碼:
  1. if (!(v instanceof CellLayout)) {
  2. v = (View) v.getParent();
  3. }

  4. //獲取桌面CellLayout上一個被拖動的對象
  5. CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();

  6. if (mWorkspace.allowLongPress()) {
  7. if (cellInfo.cell == null) {

  8. } else {
  9. if (!(cellInfo.cell instanceof Folder)) {
  10. //調用Workspace.startDrag處理拖動
  11. mWorkspace.startDrag(cellInfo);
  12. }
  13. }

  14. }
複製代碼

       我上面只寫出關鍵代碼,首先是獲取被拖動的對象v.getTag(),Tag什麼時候被設置進去的了

java代碼:
  1. public boolean onInterceptTouchEvent(MotionEvent ev) {

  2. if (action == MotionEvent.ACTION_DOWN) {
  3. boolean found = false;

  4. for (int i = count - 1; i >= 0; i--) {
  5. final View child = getChildAt(i);
  6. if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
  7. child.getHitRect(frame);

  8. //判斷區域是否在這個子控件的區間,如果有把child信息賦給mCellInfo


  9. if (frame.contains(x, y)) {
  10. final LayoutParams lp = (LayoutParams) child.getLayoutParams();
  11. cellInfo.cell = child;
  12. cellInfo.cellX = lp.cellX;
  13. cellInfo.cellY = lp.cellY;
  14. cellInfo.spanX = lp.cellHSpan;
  15. cellInfo.spanY = lp.cellVSpan;
  16. cellInfo.valid = true;
  17. found = true;
  18. mDirtyTag = false;
  19. break;
  20. }
  21. }

  22. }

  23. mLastDownOnOccupiedCell = found;
  24. if (!found) {
  25. //沒有child view 說明沒有點擊桌面圖標項
  26. cellInfo.cell = null;
  27. }

  28. setTag(cellInfo);
  29. }
複製代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章