Add a Mac-like dock for launcher (with source code)

Source: Internet
Author: User

By he minggui (http://blog.csdn.net/hmg25) reprint please indicate the source

I saw an article on the Internet: the Launcher's Dock detail Article http://news.wangmeng.cn/detailNews/2716-the-article-details-launcher-dock which implements a Mac-like dock. I think it's quite interesting, so I just copied one.

You can dynamically add shortcuts. By default, the AllApp button is included and the icons are displayed in the center.

The icons on the DockBar can exchange positions and drag the icons out.

 

 

After the release is dragged:

The source code that comes with the article is slightly modified based on the launcher2 that comes with android2.2 and used for eclipse debugging.

1. First, initialize your layout in the setupViews function of Launcher (three additional places are required)

1. </P> <p> dockbar((dockbardragdraglayer.findviewbyid(r.id.doc kbar); </P> <p> dockbar. setlauncher (this); </P> <p> dockbar. setdragcontroller (dragcontroller); </P> <p> 2. </P> <p> dragcontroller. setdragscoller (workspace); </P> <p> dragcontroller. setdraglistener (deletezone); </P> <p> dragcontroller. setdockdraglistener (dockbar); // hm25 add for Dock </P> <p> setdockdraglistener is a user-defined function and is added to startdrag of dragcontroller. For details, see Source Code </P> <p > If (mdocklistener! = NULL) {</P> <p> mdocklistener. ondragstart (source, draginfo, dragaction); </P> <p >}</P> <p> 3. </P> <p> // The order here is bottom to top. </P> <p> dragcontroller. adddroptarget (workspace); </P> <p> dragcontroller. adddroptarget (dockbar); // hm25 add for Dock </P> <p> dragcontroller. adddroptarget (deletezone); </P> <p>

2. Add in launcher. xml of layout-port

<! -- Hmg add for dock {--> </p> <com. android. launcher2.DockBar </p> <p> android: id = "@ + id/dockbar" </p> <p> android: layout_width = "fill_parent" </p> <p> android: layout_height = "@ dimen/button_bar_height" </p> <p> android: layout_gravity = "bottom | center_horizontal" </p> <p> android: background = "@ drawable/dock_bg" </p> <p> launcher: direction = "horizontal"> </p> <HorizontalScrollView android: id = "@ + id/dock_scroll_view" </P> <p> android: scrollbars = "none" </p> <p> android: fadingEdge = "none" </p> <p> android: saveEnabled = "false" </p> <p> android: layout_width = "fill_parent" </p> <p> android: layout_height = "fill_parent"> </p> <LinearLayout android: orientation = "horizontal" </p> <p> android: id = "@ + id/dock_item_holder" </p> <p> android: saveEnabled = "false" </p> <p> android: layout_width = "fill_parent" </p> <p> android: layout_height =" Fill_parent "> </p> <com. android. launcher2.HandleView // by default, the allapp button is added. </p> <p> android: id = "@ + id/all_assist_button" </p> <p> android: layout_centerHorizontal = "true" </p> <p> android: src = "@ drawable/all_assist_button" </p> <p> launcher: direction = "horizontal" </p> <p> android: layout_width = "fill_parent" </p> <p> android: layout_height = "fill_parent" </p> <p> android: focusable = "true" </p> <p> android: clickable = "true" </p> <P>/> </p> </LinearLayout> </p> </HorizontalScrollView> </p> </com. android. launcher2.DockBar> </p> <! -- Hmg add for dock} --> <br/>

3. Create a custom class:

Public class DockBar extends LinearLayout implements DropTarget, DragSource, </p> <p> DragController. dragListener, View. onLongClickListener {</p> <p> @ Override </p> <p> public boolean acceptDrop (DragSource source, int x, int y, int xOffset, int yOffset, DragView dragView, object dragInfo) {</p> <p> // What type of icons are accepted </p> <p> Log. I ("hmg", "DockBar-> acceptDrop"); </p> <p> final ItemInfo item = (ItemInfo) dragInfo; </p> <P> if (item. itemType = LauncherSettings. favorites. ITEM_TYPE_APPWIDGET </p> <p> | item. itemType = LauncherSettings. favorites. ITEM_TYPE_LIVE_FOLDER </p> <p> | item. itemType = LauncherSettings. favorites. ITEM_TYPE_USER_FOLDER </p> <p> | item. itemType = LauncherSettings. favorites. ITEM_TYPE_WIDGET_PHOTO_FRAME </p> <p> | item. itemType = LauncherSettings. favorites. ITEM_TYPE_WIDGET_SEARCH </p> <p> | Item. itemType = LauncherSettings. favorites. ITEM_TYPE_WIDGET_CLOCK) {</p> <p> return false; </p> <p >}</p> <p> return true; </p> <p >}</p> <p> // returns the following function when you drag and drop the release. </p> <p> @ Override </p> <p> public void onDrop (DragSource source, int x, int y, int xOffset, </p> <p> int yOffset, DragView dragView, Object dragInfo) {</p> <p> int position = 0; </p> <p> position = getLocation (x); // obtain the insertion position based on the released coordinates. </p> <p> addItemAt (ItemInfo) d RagInfo, position); </p> <p >}</p> <p>/* </p> <p> * input x coordinates to determine the position of the new graph subject, only the portrait screen is determined here </p> <p> */</p> <p> public int getLocation (int x) {</p> <p> for (int I = 0; I <mItemHolder. getChildCount (); I ++) {</p> <p> View iv = mItemHolder. getChildAt (I); </p> <p> int [] position = new int [2]; </p> <p> // obtain coordinates, if you want to adapt to the landscape screen, you can make a slight modification to compare the value of Y </p> <p> iv. getLocationOnScreen (position ); </p> <p> // determines whether the newly added icon is before or after the source image. </p> <p> if (x <= (position [0] + (iv. getWidth ()/2) {</p> <p> return I; </p> <p >}</p> <p> return mItemHolder. getChildCount (); </p> <p >}</p> <p> private void addItemAt (ItemInfo itemInfo, int position) </p> <p >{</p> <p> View view = null; </p> <p> switch (itemInfo. itemType) {</p> <p> case LauncherSettings. favorites. ITEM_TYPE_APPLICATION: </p> <p> case LauncherSettings. favorites. ITEM_TYPE_SHORTCUT: </p> <p> ShortcutInfo shortcutInfo; </p> <p> // drag The icon is from the app list </p> <p> if (itemInfo. container = NO_ID & itemInfo instanceof ApplicationInfo) </p> <p >{</p> <p> // different from the icon containing information from the desktop, for details, see the source code </p> <p> shortcutInfo = new ShortcutInfo (ApplicationInfo) itemInfo ); </p> <p >}</p> <p> else </p> <p> shortcutInfo = (ShortcutInfo) itemInfo; // drag the icon from the desktop </p> <p> // call CreateDockShortcut in Launcher to generate an imageView </p> <p> view = mLauncher. createDockShortcut (shortcutInfo); </p> <p> view. SetOnLongClickListener (this); </p> <p> break; </p> <p> case LauncherSettings. favorites. ITEM_TYPE_USER_FOLDER: </p> <p> break; </p> <p> default: </p> <p> throw new IllegalStateException ("Unknown item type: "</p> <p> + itemInfo. itemType); </p> <p >}</p> <p> mItemHolder. addView (view, position); </p> <p >}</p> <p> Launcher is used to create a new view. createDockShortcut is used to directly use the Click Event in Launcher. </P> <p> View CreateDockShortcut (ShortcutInfo shortcutInfo) </p> <p >{</p> <p> Context context = getApplicationContext (); </p> <p> ImageView imageView = new ImageView (context); </p> <p> imageView. setImageBitmap (shortcutInfo. mIcon); </p> <p> imageView. setOnClickListener (this); </p> <p> imageView. setFocusable (true); </p> <p> imageView. setTag (shortcutInfo); </p> <p> imageView. setMinimumWidth (100); </p> <p> return imageView; </p> <p >}</p> <p> long press on the dock, drag the switch location or drag it out. </p> <p> @ Override </p> <p> public boolean onLongClick (View v) {</p> <p> // TODO Auto-generated method stub </p> <p> if (mLauncher. isallpolicvisible () </p> <p> mLauncher. closeAllApps (false); </p> <p> mSelectedView = v; </p> <p> // start dragging </p> <p> mDragController. startDrag (v, this, v. getTag (), </p> <p> DragController. DRAG_ACTION_MOVE); </p> <p> removeSelectedItem (); </p> <p> return true; </p> <p >}</p> <p> private void removeSelectedItem () </p> <p >{</p> <p> if (mSelectedView = null) </p> <p> return; </p> <p> mItemHolder. removeView (mSelectedView); </p> <p >}</p> <p>

 

The Code has been modified many times. For details, refer to the code. I have marked all the changes ~~ Thank you for your advice ~~

Source Code address: http://download.csdn.net/source/3142047

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.