launcher添加一个仿Mac的dock

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

 

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

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

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

拖拽释放后:

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

 

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

 

1.      

dockbar=(DockBar)dragLayer.findViewById(R.id.dockbar);

dockbar.setLauncher(this);

dockbar.setDragController(dragController);     

2.

dragController.setDragScoller(workspace);

dragController.setDragListener(deleteZone);

dragController.setDockDragListener(dockbar); // hmg25 add for dock

 

setDockDragListener为自定义函数,添加在DragController的startDrag中,具体见源码

if(mDockListener!=null){  

      mDockListener.onDragStart(source, dragInfo, dragAction);

        }

 

 

3.

// The order here is bottom to top.

dragController.addDropTarget(workspace);

dragController.addDropTarget(dockbar);    //hmg25 add for dock

dragController.addDropTarget(deleteZone);

 

二、在layout-portlauncher.xml中增加

<!--hmg add for dock {  -->

 <com.android.launcher2.DockBar

        android:id="@+id/dockbar"

        android:layout_width="fill_parent"

        android:layout_height="@dimen/button_bar_height"

        android:layout_gravity="bottom|center_horizontal"

        android:background="@drawable/dock_bg"

        launcher:direction="horizontal">

        <HorizontalScrollView android:id="@+id/dock_scroll_view"

        android:scrollbars="none"

        android:fadingEdge="none"

        android:saveEnabled="false"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent">

        <LinearLayout android:orientation="horizontal"

        android:id="@+id/dock_item_holder"     

        android:saveEnabled="false"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent">

         <com.android.launcher2.HandleView   //默认将allapp按钮添加进去

            android:id="@+id/all_apps_button"

            android:layout_centerHorizontal="true"

            android:src="@drawable/all_apps_button"

            launcher:direction="horizontal"       

            android:layout_width="fill_parent"

            android:layout_height="fill_parent"

            android:focusable="true"

            android:clickable="true"

            />

        </LinearLayout>

    </HorizontalScrollView>

</com.android.launcher2.DockBar>

 <!--hmg add for dock  } --> 


三、创建自定义的类:

 

public class DockBar extends LinearLayout implements DropTarget, DragSource,

DragController.DragListener,View.OnLongClickListener {

 

@Override

    public boolean acceptDrop(DragSource source, int x, int y, int xOffset,int yOffset,DragView dragView, Object dragInfo) {                

     //接受什么类型的图标       

      Log.i("hmg", "DockBar->acceptDrop");

     final ItemInfo item = (ItemInfo) dragInfo;      

     if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET               

     || item.itemType == LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER               

     || item.itemType == LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER                

     || item.itemType == LauncherSettings.Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME               

     || item.itemType == LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH               

     || item.itemType == LauncherSettings.Favorites.ITEM_TYPE_WIDGET_CLOCK) {        

     return false;       

     } 

     return true;

     }

 

 

 

//拖拽释放时响应下边函数

@Override

    public void onDrop(DragSource source, int x, int y, int xOffset,

         int yOffset, DragView dragView, Object dragInfo) {

 

     int position=0;

     position=getLocation(x); //根据释放时的坐标,获取插入位置

     addItemAt((ItemInfo)dragInfo, position);   

    }

/*

     * 传入x坐标,判断新图标的位置,此处仅判断竖屏

     */

    public int getLocation(int x){  

            for(int i=0;i<mItemHolder.getChildCount();i++){  

                View iv = mItemHolder.getChildAt(i);          

                int[] position = new int[2];

                //获取坐标,如果要适应横屏可以稍作修改,比较Y值

                iv.getLocationOnScreen(position);

                //判断释放时新增的图标在原图标的之前还是之后

                 if(x<=(position[0]+(iv.getWidth()/2))){                       

                         return i;                     

                 }                   

             }   

             return mItemHolder.getChildCount();  

         } 

 

 

private void addItemAt(ItemInfo itemInfo, int position)

    {

     View view=null;

     switch (itemInfo.itemType) {

     case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:

     case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:    

         ShortcutInfo shortcutInfo;

     // 拖拽图标来自于app list

     if(itemInfo.container ==NO_ID&& itemInfo instanceof ApplicationInfo)       

     {

         //与来自桌面的图标包含信息不一样,具体看源码

         shortcutInfo= new ShortcutInfo((ApplicationInfo)itemInfo);

     }

     else

     shortcutInfo = (ShortcutInfo)itemInfo; //拖拽图标来自桌面

     //调用Launcher中的CreateDockShortcut生成一个imageView

     view = mLauncher. CreateDockShortcut (shortcutInfo);

      view.setOnLongClickListener(this); 

       break;

     case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:

         break;

     default:

         throw new IllegalStateException("Unknown item type: "

             + itemInfo.itemType);

     }  

     mItemHolder.addView(view, position);    

}

 

之所以将新建view用Launcher. CreateDockShortcut是想直接使用Launcher中的单击事件。

View CreateDockShortcut (ShortcutInfo shortcutInfo)

    {

     Context context=getApplicationContext();

     ImageView imageView =new ImageView(context);   

     imageView.setImageBitmap(shortcutInfo.mIcon);

     imageView.setOnClickListener(this);

     imageView.setFocusable(true);

     imageView.setTag(shortcutInfo);

     imageView.setMinimumWidth(100);

     return imageView;

    }

 

 在dock上长按,拖拽交换位置或者拖拽出去

@Override

    public boolean onLongClick(View v) {

     // TODO Auto-generated method stub

      if (mLauncher.isAllAppsVisible())

         mLauncher.closeAllApps(false);

         mSelectedView = v;       

//开始拖拽

     mDragController.startDrag(v, this, v.getTag(),

         DragController.DRAG_ACTION_MOVE);

      removeSelectedItem();

         return true;

    }

private void removeSelectedItem()

    {

      if (mSelectedView == null)

        return;

      mItemHolder.removeView(mSelectedView);

    }

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

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值