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

来源:互联网 发布:股票预测软件 编辑:程序博客网 时间:2024/06/03 00:15
  看了上面代码知道,当开始点击桌面时,celllayout就会根据点击区域去查找在该区域是否有child存在,若有把它设置为tag.cell,没有,tag.cell设置为null,后面在开始拖放时launcher.onlongclick中对tag进行处理,

  这个理顺了,再深入到workspace.startDrag函数,workspace.startDrag调用DragController.startDrag去处理拖放

  mDragController.startDrag(child, this, child.getTag(), DragController.DRAG_ACTION_MOVE);
  再分析一下上面调用的几个参数

  child = tag.cell
  this = workspace

  child.getTag()是什么呢?在什么时候被设置?再仔细回顾原来launcher加载过程代码,在launcher.createShortcut中它被设置了:注意下面我代码中的注释

java代码:
  1. View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
  2. TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
  3. favorite.setCompoundDrawablesWithIntrinsicBounds(null,new FastBitmapDrawable(info.getIcon(mIconCache)),null, null);

  4. favorite.setText(info.title);
  5. //设置favorite(一个桌面Shortcut类型的图标)的tag
  6. favorite.setTag(info);
  7. favorite.setOnClickListener(this);
  8. return favorite;
  9. }
复制代码

       继续深入解读DragController.startDrag函数

java代码:

  1. public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {

  2. //设置拖放源view
  3. mOriginator = v;
  4. //获取view的bitmap
  5. Bitmap b = getViewBitmap(v);
  6. if (b == null) {

  7. // out of memory?
  8. return;
  9. }
  10. //获取源view在整个屏幕的坐标
  11. int[] loc = mCoordinatesTemp;
  12. v.getLocationOnScreen(loc);


  13. int screenX = loc[0];
  14. int screenY = loc[1];


  15. //该函数功能解读请继续往下看
  16. startDrag(b, screenX, screenY, 0, 0, b.getWidth(), b.getHeight(),
  17. source, dragInfo, dragAction);
  18. b.recycle();
  19. //设置原来view不可见
  20. if (dragAction == DRAG_ACTION_MOVE) {
  21. v.setVisibility(View.GONE);
  22. }

  23. }
复制代码

java代码:
  1. public void startDrag(Bitmap b, int screenX, int screenY,int textureLeft, int textureTop, int textureWidth, int textureHeight,DragSource source, Object dragInfo, int dragAction) {

  2. //隐藏软键盘
  3. if (mInputMethodManager == null) {
  4. mInputMethodManager = (InputMethodManager)
  5. mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
  6. }

  7. mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
  8. //mListener = deletezone,在blog laucher ui框架中有说明该函数,主要就是现实deletezone

  9. if (mListener != null) {
  10. mListener.onDragStart(source, dragInfo, dragAction);
  11. }

  12. //记住手指点击位置与屏幕左上角位置偏差
  13. int registrationX = ((int)mMotionDownX) - screenX;
  14. int registrationY = ((int)mMotionDownY) - screenY;
  15. mTouchOffsetX = mMotionDownX - screenX;

原创粉丝点击