之前在网上看到有篇文章: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个地方)
view plain copy to clipboard print ?
- 1.
-
- dockbar=(DockBar)dragLayer.findViewById(R.id.dockbar);
-
- dockbar.setLauncher(this);
-
- dockbar.setDragController(dragController);
-
- 2.
-
- dragController.setDragScoller(workspace);
-
- dragController.setDragListener(deleteZone);
-
- dragController.setDockDragListener(dockbar);
-
-
-
- setDockDragListener为自定义函数,添加在DragController的startDrag中,具体见源码
-
- if(mDockListener!=null){
-
- mDockListener.onDragStart(source, dragInfo, dragAction);
-
- }
-
-
-
-
-
- 3.
-
-
-
- dragController.addDropTarget(workspace);
-
- dragController.addDropTarget(dockbar);
-
- dragController.addDropTarget(deleteZone);
-
-
二、在layout-port的launcher.xml中增加
view plain copy to clipboard print ?
-
-
- <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>
-
-
三、创建自定义的类:
view plain copy to clipboard print ?
- 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);
-
- }
-
-
-
-
-
-
-
- public int getLocation(int x){
-
- for(int i=0;i<mItemHolder.getChildCount();i++){
-
- View iv = mItemHolder.getChildAt(i);
-
- int[] position = new int[2];
-
-
-
- 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;
-
-
-
- if(itemInfo.container ==NO_ID&& itemInfo instanceof ApplicationInfo)
-
- {
-
-
-
- shortcutInfo= new ShortcutInfo((ApplicationInfo)itemInfo);
-
- }
-
- else
-
- shortcutInfo = (ShortcutInfo)itemInfo;
-
-
-
- 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) {
-
-
-
- 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