现在的位置: 首页 > 综合 > 正文

launcher2中图标拖拽的简单分析

2013年10月02日 ⁄ 综合 ⁄ 共 2269字 ⁄ 字号 评论关闭

相关的文件:

DragController:拖动控制接口。
DragLayer
:整个launcher的父节点,继承FrameLayout,实现接口DrayController

DragSource
:拖动源接口,定义了void onDropCompleted(View target, boolean success)

DropTarget
:拖动目标(要将部件拖拽进入,必须继承DropTarget接口),定义很多拖动过程需要的方法:onDroponDragEnteronDragOveronDragExitacceptDrop

 

流程分析:

Laucher. onLongClick(View v) ->Workspace.startDrag(CellLayout.CellInfo cellInfo) -> CellLayout. onDragChild(View v) –> DragController. startDrag(View v, DragSource source, Object dragInfo, int dragAction) dragAction分为DRAG_ACTION_MOVEDRAG_ACTION_COPY,前者在移动时会通过view.setVisibility(View.GONE)隐藏之前存在的那个cell. startDrag中还有一个有用的函数是getViewBitmap(View v),它将传入的view绘制成一张bitmap.需要注意的是在startDrag中调用getViewBitmap返回的那张图片是包含icon和下边文字两部分的。

   接着进入DragController.startDrag(Bitmap b, int screenX, int screenY,int textureLeft, int textureTop, int textureWidth, int textureHeight, DragSource source, Object dragInfo, int dragAction)函数,

 

这个是launcher.javasetupViews()dragController.setDragListener(deleteZone)相关联的,即跳转进入deleteZone. onDragStart(DragSource source, Object info, int dragAction)这个函数。之后通过new DragViewdragView.show生成显示拖动时浮起,放大的那个图标。图标跟随鼠标的移动是响应dragController.onTouchEvent(MotionEvent ev)case MotionEvent.ACTION_MOVE:里的mDragView.move((int)ev.getRawX(), (int)ev.getRawY()),同时还会响应onDragOver(拖拽悬浮于某处上方),onDragEnter(拖拽进入),onDragExit(拖拽离开)函数。要响应的上述函数,必须继承实现interface DropTarget,并且在launcher.java中的setupViews()里通过dragController.addDropTarget注册一下,默认注册了

所以workspacedeleteZone可以接收拖拽进入其空间的物件,如果你要实现一个类似Mac系统,在桌面的底部加一个icon的列表功能的dock,如http://news.wangmeng.cn/detailNews/2601 那么你就要为你的对应类注册一下。

 

 当你的拖拽释放的时候,即MotionEvent.ACTION_UP,响应dragController.drop(screenX, screenY)函数,

   

drop中先执行对应的acceptDrop判断是否可接收对应类型,如果可以接收则响应对应的onDrop函数,如果你是在workspace区域释放的话,你就响应workspace. onDrop,如果你是在deleteZone区域释放则响应deleteZone. onDrop

(待续……)

抱歉!评论已关闭.