歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發:Launcher之Dock細節篇

Android開發:Launcher之Dock細節篇

日期:2017/3/1 11:17:10   编辑:Linux編程

在前面一篇文章中,大致介紹了怎麼仿Mac Dock效果,有的朋友問起那個梯形怎麼實現的,其實這個很簡單,就是一張背景圖片,不過你要先理解.9圖片代表的含義,這裡有一片文章有介紹,不過大家最好是親身體驗下,這樣的話理解更深入。

相關閱讀 : Android開發:為launcher添加一個仿Mac的Dock(附源碼) http://www.linuxidc.com/Linux/2011-09/44161.htm

這個圖片就是我們項目中用到的圖片:


這個就是我顯示在Launcher主界面的自定義類:

public class DockView extends LinearLayout implements DropTarget,DragSource, DragController.DragListener,OnClickListener, View.OnLongClickListener {
	/**
	 * @author jezz
	 *
	 */

	public DockView(Context context){
		super(context);
	}

	public DockView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mDockBgId = R.drawable.shortcut_selector;
	}
	
	public void init(){
               //初始化操作
	}
	

	public boolean acceptDrop(DragSource source, int x, int y, int xOffset,int yOffset, Object dragInfo) {
                //接受什麼類型的圖標
		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;
		}
	}

	public void onDragEnter(DragSource source, int x, int y, int xOffset,int yOffset, Object dragInfo) {
                //拖入進入區域
		setBackgroundResource(R.drawable.dock_press_bg);
	}

	public void onDragExit(DragSource source, int x, int y, int xOffset,int yOffset, Object dragInfo) {
                //拖動離開區域
		setBackgroundResource(R.drawable.dock_bg);
	}

	public void onDragOver(DragSource source, int x, int y, int xOffset,int yOffset, Object dragInfo) {
	}

	public void onDrop(DragSource source, int x, int y, int xOffset,int yOffset, Object dragInfo) {
		//拖動釋放
	}
	
	public void onDragEnd() {
	}

	public void onDragStart(View v, DragSource source, Object info,int dragAction) {
                 //開始拖動
	}
    	
	public void onClick(View v) {
                 //單擊打開app
		ImageView view=(ImageView)v;
		DockInfo dockInfo=(DockInfo)view.getTag();
		try {
			Intent i = Intent.getIntent(dockInfo.info.intent.toUri(0));
			mLauncher.startActivitySafely(i);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}

	public boolean onLongClick(View v) {
          //長按實現拖動
	}
    	
    	public void onDropCompleted(View target, boolean success) {
          //拖動釋放事件
    	}
}

這個三個接口DropTarget,DragSource, DragController.DragListener是launcher自己定義的,你必須在Launcher啟動的時候,將你的這幾個接口實現在DragLayer.java裡面,要注意的細節很多,你們多研究下代碼就知道了。

自定義的類放在layout-land下面,有個launcher.xml寫入到文件的最下面:

<com.android.launcher.DockView
	android:id="@+id/dock_view"
	android:background="@drawable/dock_bg"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_gravity="bottom|center_horizontal"
     />

其中這個@drawable/dock_bg就是.9(九宮格圖片),每次添加Icon的時候都會水平自由拉伸。

還有一個注意的地方就是我們下面Dock區域是能讓任何快捷方式和Widget放到這個區域的,所以我們要設置一個禁區,不過系統已經為我們寫好了,我們只需要設置一下就可以了,在同樣在layout-land目錄下的workspace_screen.xml文件裡,內容如下:

<com.android.launcher.CellLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    launcher:cellWidth="106dip"
    launcher:cellHeight="73dip"
    launcher:longAxisStartPadding="0dip"
    launcher:longAxisEndPadding="55dip"
    launcher:shortAxisStartPadding="0dip"
    launcher:shortAxisEndPadding="70dip"
    launcher:shortAxisCells="6"
    launcher:longAxisCells="8" />

我們只需要該launcher:shortAxisEndPadding="70dip"這個就可以,這個數值根據你的需求來即可了。

最後來一張我們真機上的截圖:

Copyright © Linux教程網 All Rights Reserved