前一篇大致說了怎麼做出仿 Mac Dock 的效果,有朋友問那個梯形外框是怎麼來的。答案其實很樸素:一張背景圖而已。前提是你得先理解 .9(九宮格)圖的含義,而這件事親手做一遍比看文章有效得多。
延伸閱讀:Android 開發:為 launcher 添加一個仿 Mac 的 Dock(附源碼)
public class DockView extends LinearLayout
implements DropTarget, DragSource, DragController.DragListener,
OnClickListener, View.OnLongClickListener {
public DockView(Context context) {
super(context);
}
public DockView(Context context, AttributeSet attrs) {
super(context, attrs);
mDockBgId = R.drawable.shortcut_selector;
}
public void init() {
// 初始化操作
}
// 決定接受哪些類型的圖示:Widget、資料夾等一律退回
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;
}
return true;
}
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) {
// 長按觸發拖動
return true;
}
public void onDropCompleted(View target, boolean success) {
// 拖動釋放完成
}
}
DropTarget、DragSource、DragController.DragListener 這三個介面都是 Launcher 自訂的,你必須在 Launcher 啟動時把它們掛到 DragLayer 上。這一步細節不少,多讀原始碼會比死記更有效果。
自訂類別放在 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="https://schemas.android.com/apk/res/android"
xmlns:launcher="https://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" 這一項,數值依你的 Dock 高度調整即可——它把工作區底部讓出一塊,形成不可放置的禁區。