Android launcher source code parsing 03: Mac-like dock

Source: Internet
Author: User

I saw an article on the Internet: the launcher's Dock detail Article http://www.linuxidc.com/Linux/2011-09/44160.htm 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,

 // hmg add for dock {    dockbar = (DockBar) dragLayer.findViewById(R.id.dockbar);    dockbar.setLauncher(this);    dockbar.setDragController(dragController);    // hmg add for dock }

2,

 dragController.setDragScoller(workspace);        dragController.setDragListener(deleteZone);        dragController.setDockDragListener(dockbar); // hmg25 add for dock        dragController.setScrollView(dragLayer);        dragController.setMoveTarget(workspace);

Setdockdraglistener is a user-defined function and is added to startdrag of dragcontroller. For details, see the source code.

//hmg25 add for dock {         if(mDockListener!=null){               mDockListener.onDragStart(source, dragInfo, dragAction);         }        //hmg25 add for dock } 

3,

// The order here is bottom to top.        dragController.addDropTarget(workspace);        dragController.addDropTarget(dockbar); // hmg25 add for dock        dragController.addDropTarget(deleteZone);

2. Add in launcher. xml of layout-Port

 <!-- hmg25 add for dock {  -->  <com.xuxm.demo.launcher.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.xuxm.demo.launcher.HandleView            android:id="@+id/all_apps_button"            android:layout_centerHorizontal="true"            android:src="@drawable/hotseat_all_apps"               launcher:direction="horizontal"                    android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:focusable="true"            android:clickable="true"            />        </LinearLayout>    </HorizontalScrollView></com.xuxm.demo.launcher.DockBar> <!-- hmg25 add for dock  } --> 

3. Create a custom class:

Package COM. xuxm. demo. launcher; import android. content. context; import android. graphics. rect; import android. util. attributeset; import android. util. log; import android. view. gravity; import android. view. view; import android. widget. framelayout; import android. widget. linearlayout; public class dockbar extends linearlayout implements droptarget, dragsource, dragcontroller. draglistener, view. onlongclicklistener {private launcher mlauncher; private dragcontroller mdragcontroller; private workspace mworkspace; private view mdragview; private linearlayout mitemholder; private view mscrollview; private view mselectedview; Public dockbar (context) {super (context); // todo auto-generated constructor stub} public dockbar (context, attributeset attrs) {super (context, attrs);} @ override protected void onfinishinflate () {// todo auto-generated method stub log. I ("HMG", "dockbar-> onfinishinflate"); mitemholder = javask_item_holder); mscrollview = findviewbyid(r.id.doc k_scroll_view); super. onfinishinflate () ;}@ override protected void onmeasure (INT widthmeasurespec, int heightmeasurespec) {// todo auto-generated method stub super. onmeasure (widthmeasurespec, heightmeasurespec); // framelayout is displayed in the center. layoutparams Lp = (framelayout. layoutparams) mitemholder. getlayoutparams (); LP. gravity = gravity. center; mitemholder. setlayoutparams (LP); postinvalidate (); requestlayout () ;}@ override public Boolean onlongclick (view v) {// todo auto-generated method stub if (mlauncher. isallpolicvisible () 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);} @ overridepublic void ondragstart (dragsource source, object info, int dragaction) {// todo auto-generated method stublog. I ("HMG", "dockbar-> ondragstart") ;}@ overridepublic void ondragend () {// todo auto-generated method stub} @ overridepublic void setdragcontroller (dragcontroller dragger) {// todo auto-generated method stubmdragcontroller = dragger;} void setlauncher (launcher) {mlauncher = launcher;} void setworkspace (workspace) {mworkspace = workspace ;} @ overridepublic void ondropcompleted (view target, Boolean success) {// todo auto-generated method stub}/** response when dragging and releasing */@ override public void ondrop (dragsource source, int X, int y, int xoffset, int yoffset, dragview, object draginfo) {int position = 0; position = getlocation (x); // Based on the coordinates of the release, get the insert position additemat (iteminfo) draginfo, position) ;}@ overridepublic void ondragenter (dragsource source, int X, int y, int xoffset, int yoffset, dragview, object draginfo) {// todo auto-generated method stublog. I ("HMG", "dockbar-> ondragenter" cmdk_bg_press);} @ overridepublic void ondragover (dragsource source, int X, int y, int xoffset, int yoffset, dragview, object draginfo) {// todo auto-generated method stub} private void additemat (iteminfo, int position) {view = NULL; Switch (iteminfo. itemtype) {Case launchersettings. favorites. item_type_application: Case launchersettings. favorites. item_type_shortcut: shortcutinfo; // drag the icon from the app listif (iteminfo. container = no_id & iteminfo instanceof applicationinfo) {// different from the information contained in the icons from the desktop. For details, see the source code shortcutinfo = new shortcutinfo (applicationinfo) iteminfo );} elseshortcutinfo = (shortcutinfo) iteminfo; // drag the icon from the desktop // call createsmallgatecut In the launcher to generate an imageviewview = 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);}/** input X coordinates to determine the position of the new graph. Here, only the vertical screen */Public int getlocation (int x) is determined) {for (INT I = 0; I <mitemholder. getchildcount (); I ++) {view IV = mitemholder. getchildat (I); int [] position = new int [2]; // obtain the coordinates. To adapt to the landscape, You Can slightly modify the position and compare the value of Y to IV. getlocationonscreen (position); // determines if (x <= (position [0] + (IV. getwidth ()/2) {return I;} return mitemholder. getchildcount () ;}@ overridepublic void ondragexit (dragsource source, int X, int y, int xoffset, int yoffset, dragview, object draginfo) {// todo auto-generated method stubsetbackgroundresource(r.drawable.doc k_bg); mitemholder. removeview (mdragview); log. I ("HMG", "dockbar-> ondragexit") ;}@ overridepublic Boolean acceptdrop (dragsource source, int X, int y, int xoffset, int yoffset, dragview, object draginfo) {// todo auto-generated method stub // What type of icon log is accepted. 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;} @ overridepublic rect estimatedroplocation (dragsource source, int X, int y, int xoffset, int yoffset, dragview, object draginfo, rect recycle) {// todo auto-generated method stubreturn NULL ;}}

References:

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

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.