为launcher添加一个仿Mac的dock(附源码)

By 何明桂(http://blog.csdn.net/hmg25)转载请注明出处

之前在网上看到有篇文章:Launcher之Dock细节篇http://news.wangmeng.cn/detailNews/2716-the-article-details-launcher-dock它实现了一个仿Mac的dock。感觉蛮有意思的,所以就照着仿制了一个。

可以动态的添加快捷方式,默认包含AllApp按钮,图标居中显示。

DockBar上的图标可以相互交换位置,并且将图标拖拽出去。

拖拽释放后:

文章后边附带的源码是基于android2.2自带的launcher2稍作修改而成,使用eclipse调试。

一、首先要在Launcher的setupViews函数里面初始化自己的layout(需增加3个地方)

[java] view plain copy
  1. 1.
  2. dockbar=(DockBar)dragLayer.findViewById(R.id.dockbar);
  3. dockbar.setLauncher(this);
  4. dockbar.setDragController(dragController);
  5. 2.
  6. dragController.setDragScoller(workspace);
  7. dragController.setDragListener(deleteZone);
  8. dragController.setDockDragListener(dockbar);//hmg25addfordock
  9. setDockDragListener为自定义函数,添加在DragController的startDrag中,具体见源码
  10. if(mDockListener!=null){
  11. mDockListener.onDragStart(source,dragInfo,dragAction);
  12. }
  13. 3.
  14. //Theorderhereisbottomtotop.
  15. dragController.addDropTarget(workspace);
  16. dragController.addDropTarget(dockbar);//hmg25addfordock
  17. dragController.addDropTarget(deleteZone);

二、在layout-port的launcher.xml中增加

[xhtml] view plain copy
  1. <!--hmgaddfordock{-->
  2. <com.android.launcher2.DockBar
  3. android:id="@+id/dockbar"
  4. android:layout_width="fill_parent"
  5. android:layout_height="@dimen/button_bar_height"
  6. android:layout_gravity="bottom|center_horizontal"
  7. android:background="@drawable/dock_bg"
  8. launcher:direction="horizontal">
  9. <HorizontalScrollViewandroid:id="@+id/dock_scroll_view"
  10. android:scrollbars="none"
  11. android:fadingEdge="none"
  12. android:saveEnabled="false"
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent">
  15. <LinearLayoutandroid:orientation="horizontal"
  16. android:id="@+id/dock_item_holder"
  17. android:saveEnabled="false"
  18. android:layout_width="fill_parent"
  19. android:layout_height="fill_parent">
  20. <com.android.launcher2.HandleView//默认将allapp按钮添加进去
  21. android:id="@+id/all_apps_button"
  22. android:layout_centerHorizontal="true"
  23. android:src="@drawable/all_apps_button"
  24. launcher:direction="horizontal"
  25. android:layout_width="fill_parent"
  26. android:layout_height="fill_parent"
  27. android:focusable="true"
  28. android:clickable="true"
  29. />
  30. </LinearLayout>
  31. </HorizontalScrollView>
  32. </com.android.launcher2.DockBar>
  33. <!--hmgaddfordock}-->

三、创建自定义的类:

[java] view plain copy
  1. publicclassDockBarextendsLinearLayoutimplementsDropTarget,DragSource,
  2. DragController.DragListener,View.OnLongClickListener{
  3. @Override
  4. publicbooleanacceptDrop(DragSourcesource,intx,inty,intxOffset,intyOffset,DragViewdragView,ObjectdragInfo){
  5. //接受什么类型的图标
  6. Log.i("hmg","DockBar->acceptDrop");
  7. finalItemInfoitem=(ItemInfo)dragInfo;
  8. if(item.itemType==LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET
  9. ||item.itemType==LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER
  10. ||item.itemType==LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER
  11. ||item.itemType==LauncherSettings.Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME
  12. ||item.itemType==LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH
  13. ||item.itemType==LauncherSettings.Favorites.ITEM_TYPE_WIDGET_CLOCK){
  14. returnfalse;
  15. }
  16. returntrue;
  17. }
  18. //拖拽释放时响应下边函数
  19. @Override
  20. publicvoidonDrop(DragSourcesource,intx,inty,intxOffset,
  21. intyOffset,DragViewdragView,ObjectdragInfo){
  22. intposition=0;
  23. position=getLocation(x);//根据释放时的坐标,获取插入位置
  24. addItemAt((ItemInfo)dragInfo,position);
  25. }
  26. /*
  27. *传入x坐标,判断新图标的位置,此处仅判断竖屏
  28. */
  29. publicintgetLocation(intx){
  30. for(inti=0;i<mItemHolder.getChildCount();i++){
  31. Viewiv=mItemHolder.getChildAt(i);
  32. int[]position=newint[2];
  33. //获取坐标,如果要适应横屏可以稍作修改,比较Y值
  34. iv.getLocationOnScreen(position);
  35. //判断释放时新增的图标在原图标的之前还是之后
  36. if(x<=(position[0]+(iv.getWidth()/2))){
  37. returni;
  38. }
  39. }
  40. returnmItemHolder.getChildCount();
  41. }
  42. privatevoidaddItemAt(ItemInfoitemInfo,intposition)
  43. {
  44. Viewview=null;
  45. switch(itemInfo.itemType){
  46. caseLauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
  47. caseLauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
  48. ShortcutInfoshortcutInfo;
  49. //拖拽图标来自于applist
  50. if(itemInfo.container==NO_ID&&itemInfoinstanceofApplicationInfo)
  51. {
  52. //与来自桌面的图标包含信息不一样,具体看源码
  53. shortcutInfo=newShortcutInfo((ApplicationInfo)itemInfo);
  54. }
  55. else
  56. shortcutInfo=(ShortcutInfo)itemInfo;//拖拽图标来自桌面
  57. //调用Launcher中的CreateDockShortcut生成一个imageView
  58. view=mLauncher.CreateDockShortcut(shortcutInfo);
  59. view.setOnLongClickListener(this);
  60. break;
  61. caseLauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
  62. break;
  63. default:
  64. thrownewIllegalStateException("Unknownitemtype:"
  65. +itemInfo.itemType);
  66. }
  67. mItemHolder.addView(view,position);
  68. }
  69. 之所以将新建view用Launcher.CreateDockShortcut是想直接使用Launcher中的单击事件。
  70. ViewCreateDockShortcut(ShortcutInfoshortcutInfo)
  71. {
  72. Contextcontext=getApplicationContext();
  73. ImageViewimageView=newImageView(context);
  74. imageView.setImageBitmap(shortcutInfo.mIcon);
  75. imageView.setOnClickListener(this);
  76. imageView.setFocusable(true);
  77. imageView.setTag(shortcutInfo);
  78. imageView.setMinimumWidth(100);
  79. returnimageView;
  80. }
  81. 在dock上长按,拖拽交换位置或者拖拽出去
  82. @Override
  83. publicbooleanonLongClick(Viewv){
  84. //TODOAuto-generatedmethodstub
  85. if(mLauncher.isAllAppsVisible())
  86. mLauncher.closeAllApps(false);
  87. mSelectedView=v;
  88. //开始拖拽
  89. mDragController.startDrag(v,this,v.getTag(),
  90. DragController.DRAG_ACTION_MOVE);
  91. removeSelectedItem();
  92. returntrue;
  93. }
  94. privatevoidremoveSelectedItem()
  95. {
  96. if(mSelectedView==null)
  97. return;
  98. mItemHolder.removeView(mSelectedView);
  99. }

代码修改了不少地方,具体看代码,修改的地方我都标注啦 ~~欢迎大家指教,相互交流~~

源码地址:http://download.csdn.net/source/3142047

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值