zoukankan      html  css  js  c++  java
  • android launcher 仿 mac dock

    引用:http://blog.csdn.net/hmg25/article/details/6289438

    http://blog.csdn.net/hmg25/article/category/803127

    之前在网上看到有篇文章: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个地方)

    1. 1.         
    2.   
    3. dockbar=(DockBar)dragLayer.findViewById(R.id.dockbar);  
    4.   
    5. dockbar.setLauncher(this);   
    6.   
    7. dockbar.setDragController(dragController);        
    8.   
    9. 2.  
    10.   
    11. dragController.setDragScoller(workspace);  
    12.   
    13. dragController.setDragListener(deleteZone);  
    14.   
    15. dragController.setDockDragListener(dockbar); // hmg25 add for dock  
    16.   
    17.    
    18.   
    19. setDockDragListener为自定义函数,添加在DragController的startDrag中,具体见源码  
    20.   
    21. if(mDockListener!=null){     
    22.   
    23.       mDockListener.onDragStart(source, dragInfo, dragAction);   
    24.   
    25.         }  
    26.   
    27.    
    28.   
    29.   
    30.   
    31. 3.  
    32.   
    33. // The order here is bottom to top.  
    34.   
    35. dragController.addDropTarget(workspace);  
    36.   
    37. dragController.addDropTarget(dockbar);    //hmg25 add for dock  
    38.   
    39. dragController.addDropTarget(deleteZone);  
    40.   
    41.    

      

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

     

    [xhtml] view plaincopy
    1. <!--hmg add for dock {  -->   
    2.   
    3.  <com.android.launcher2.DockBar  
    4.   
    5.         android:id="@+id/dockbar"  
    6.   
    7.         android:layout_width="fill_parent"  
    8.   
    9.         android:layout_height="@dimen/button_bar_height"  
    10.   
    11.         android:layout_gravity="bottom|center_horizontal"  
    12.   
    13.         android:background="@drawable/dock_bg"  
    14.   
    15.         launcher:direction="horizontal">  
    16.   
    17.         <HorizontalScrollView android:id="@+id/dock_scroll_view"   
    18.   
    19.         android:scrollbars="none"   
    20.   
    21.         android:fadingEdge="none"   
    22.   
    23.         android:saveEnabled="false"   
    24.   
    25.         android:layout_width="fill_parent"   
    26.   
    27.         android:layout_height="fill_parent">  
    28.   
    29.         <LinearLayout android:orientation="horizontal"   
    30.   
    31.         android:id="@+id/dock_item_holder"        
    32.   
    33.         android:saveEnabled="false"   
    34.   
    35.         android:layout_width="fill_parent"   
    36.   
    37.         android:layout_height="fill_parent">  
    38.   
    39.          <com.android.launcher2.HandleView   //默认将allapp按钮添加进去  
    40.   
    41.             android:id="@+id/all_apps_button"  
    42.   
    43.             android:layout_centerHorizontal="true"  
    44.   
    45.             android:src="@drawable/all_apps_button"  
    46.   
    47.             launcher:direction="horizontal"          
    48.   
    49.             android:layout_width="fill_parent"  
    50.   
    51.             android:layout_height="fill_parent"  
    52.   
    53.             android:focusable="true"  
    54.   
    55.             android:clickable="true"  
    56.   
    57.             />  
    58.   
    59.         </LinearLayout>  
    60.   
    61.     </HorizontalScrollView>  
    62.   
    63. </com.android.launcher2.DockBar>  
    64.   
    65.  <!--hmg add for dock  } -->    

      

    三、创建自定义的类:

     

    1. public class DockBar extends LinearLayout implements DropTarget, DragSource,  
    2.   
    3. DragController.DragListener,View.OnLongClickListener {  
    4.   
    5.    
    6.   
    7. @Override  
    8.   
    9.     public boolean acceptDrop(DragSource source, int x, int y, int xOffset,int yOffset,DragView dragView, Object dragInfo) {                   
    10.   
    11.      //接受什么类型的图标          
    12.   
    13.       Log.i("hmg", "DockBar->acceptDrop");  
    14.   
    15.      final ItemInfo item = (ItemInfo) dragInfo;         
    16.   
    17.      if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET                  
    18.   
    19.      || item.itemType == LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER                  
    20.   
    21.      || item.itemType == LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER                   
    22.   
    23.      || item.itemType == LauncherSettings.Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME                  
    24.   
    25.      || item.itemType == LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH                  
    26.   
    27.      || item.itemType == LauncherSettings.Favorites.ITEM_TYPE_WIDGET_CLOCK) {           
    28.   
    29.      return false;          
    30.   
    31.      }    
    32.   
    33.      return true;   
    34.   
    35.      }  
    36.   
    37.    
    38.   
    39.    
    40.   
    41.    
    42.   
    43. //拖拽释放时响应下边函数  
    44.   
    45. @Override  
    46.   
    47.     public void onDrop(DragSource source, int x, int y, int xOffset,  
    48.   
    49.          int yOffset, DragView dragView, Object dragInfo) {  
    50.   
    51.    
    52.   
    53.      int position=0;  
    54.   
    55.      position=getLocation(x); //根据释放时的坐标,获取插入位置  
    56.   
    57.      addItemAt((ItemInfo)dragInfo, position);      
    58.   
    59.     }  
    60.   
    61. /* 
    62.  
    63.      * 传入x坐标,判断新图标的位置,此处仅判断竖屏 
    64.  
    65.      */  
    66.   
    67.     public int getLocation(int x){     
    68.   
    69.             for(int i=0;i<mItemHolder.getChildCount();i++){     
    70.   
    71.                 View iv = mItemHolder.getChildAt(i);             
    72.   
    73.                 int[] position = new int[2];  
    74.   
    75.                 //获取坐标,如果要适应横屏可以稍作修改,比较Y值  
    76.   
    77.                 iv.getLocationOnScreen(position);  
    78.   
    79.                 //判断释放时新增的图标在原图标的之前还是之后  
    80.   
    81.                  if(x<=(position[0]+(iv.getWidth()/2))){                          
    82.   
    83.                          return i;                        
    84.   
    85.                  }                      
    86.   
    87.              }      
    88.   
    89.              return mItemHolder.getChildCount();     
    90.   
    91.          }    
    92.   
    93.    
    94.   
    95.    
    96.   
    97. private void addItemAt(ItemInfo itemInfo, int position)  
    98.   
    99.     {  
    100.   
    101.      View view=null;  
    102.   
    103.      switch (itemInfo.itemType) {  
    104.   
    105.      case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:  
    106.   
    107.      case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:       
    108.   
    109.          ShortcutInfo shortcutInfo;  
    110.   
    111.      // 拖拽图标来自于app list  
    112.   
    113.      if(itemInfo.container ==NO_ID&& itemInfo instanceof ApplicationInfo)          
    114.   
    115.      {  
    116.   
    117.          //与来自桌面的图标包含信息不一样,具体看源码  
    118.   
    119.          shortcutInfo= new ShortcutInfo((ApplicationInfo)itemInfo);  
    120.   
    121.      }  
    122.   
    123.      else  
    124.   
    125.      shortcutInfo = (ShortcutInfo)itemInfo; //拖拽图标来自桌面  
    126.   
    127.      //调用Launcher中的CreateDockShortcut生成一个imageView  
    128.   
    129.      view = mLauncher. CreateDockShortcut (shortcutInfo);  
    130.   
    131.       view.setOnLongClickListener(this);    
    132.   
    133.        break;  
    134.   
    135.      case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:  
    136.   
    137.          break;  
    138.   
    139.      default:  
    140.   
    141.          throw new IllegalStateException("Unknown item type: "  
    142.   
    143.              + itemInfo.itemType);  
    144.   
    145.      }     
    146.   
    147.      mItemHolder.addView(view, position);       
    148.   
    149. }  
    150.   
    151.    
    152.   
    153. 之所以将新建view用Launcher. CreateDockShortcut是想直接使用Launcher中的单击事件。  
    154.   
    155. View CreateDockShortcut (ShortcutInfo shortcutInfo)  
    156.   
    157.     {   
    158.   
    159.      Context context=getApplicationContext();  
    160.   
    161.      ImageView imageView =new ImageView(context);      
    162.   
    163.      imageView.setImageBitmap(shortcutInfo.mIcon);  
    164.   
    165.      imageView.setOnClickListener(this);  
    166.   
    167.      imageView.setFocusable(true);  
    168.   
    169.      imageView.setTag(shortcutInfo);  
    170.   
    171.      imageView.setMinimumWidth(100);  
    172.   
    173.      return imageView;  
    174.   
    175.     }  
    176.   
    177.    
    178.   
    179.  在dock上长按,拖拽交换位置或者拖拽出去  
    180.   
    181. @Override  
    182.   
    183.     public boolean onLongClick(View v) {  
    184.   
    185.      // TODO Auto-generated method stub  
    186.   
    187.       if (mLauncher.isAllAppsVisible())  
    188.   
    189.          mLauncher.closeAllApps(false);  
    190.   
    191.          mSelectedView = v;          
    192.   
    193. //开始拖拽  
    194.   
    195.      mDragController.startDrag(v, this, v.getTag(),  
    196.   
    197.          DragController.DRAG_ACTION_MOVE);  
    198.   
    199.       removeSelectedItem();  
    200.   
    201.          return true;  
    202.   
    203.     }  
    204.   
    205. private void removeSelectedItem()  
    206.   
    207.     {  
    208.   
    209.       if (mSelectedView == null)  
    210.   
    211.         return;  
    212.   
    213.       mItemHolder.removeView(mSelectedView);  
    214.   
    215.     }  

     

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

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

  • 相关阅读:
    redhat7最小化安装后的简单配置
    weblogic线程优化
    weblogic运行自动挂掉,优化jvm解决
    weblogic抛出Platform MBeanServer异常
    rehat7/contos7 HA安装配置
    docker stats 命令源码分析
    cAdvisor源码分析
    转: django class base view 简单说明
    转:多用户、多工作负载、5000节点集群:Kubernetes 1.6都有哪些看点?
    第一篇随笔
  • 原文地址:https://www.cnblogs.com/sode/p/2661732.html
Copyright © 2011-2022 走看看