從今天起傻蛋打算做一個系列文章,對最新的Android4.0 系統中的Launcher,也就是Android4.0原生的桌面程序,進行一個深入淺出的分析,從而引領Android系統的編程愛好者對Launcher的設計思想,實現方式來做一個研究,從而能夠通過這個實例最掌握到目前世界領先的設計方法,同時在程序中加入我們的一些新的實現。衆所周知,對一些優秀源代碼的分析,是提高編程水平的一條便捷的方式,希望本系列文章能夠給大家帶來一定的啓發,同時歡迎大家和我一起討論,我的微博是:http://weibo.com/zuiniuwang/
先從整體上對Launcher佈局作一個分析,讓我們通過查看Launcher.xml 和使用hierarchyviewer佈局查看工具兩者結合的方法來對Launcher的整體結構有個瞭解。通過hierarchyviewer來對整個桌面做個截圖,如下:
放大後如下所示: 可以看到整個桌面包含的元素,最上面是google的搜索框,下面是一個始終插件,然後是圖標,再有就是一個分隔線,最後是dock。請注意,桌面程序其實並不包含桌面壁紙,桌面壁紙其實是由 WallpaperManagerService來提供,整個桌面其實是疊加在整個桌面壁紙上的另外一個層。
整個Launcher.xml佈局文件如下:
<com.android.launcher2.DragLayer
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
android:id="@+id/drag_layer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Keep these behind the workspace so that they are not visible when
we go into AllApps -->
<include
android:id="@+id/dock_divider"
layout="@layout/workspace_divider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/button_bar_height"
android:layout_gravity="bottom" />
<include
android:id="@+id/paged_view_indicator"
layout="@layout/scroll_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="@dimen/button_bar_height" />
<!-- The workspace contains 5 screens of cells -->
<com.android.launcher2.Workspace
android:id="@+id/workspace"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/qsb_bar_height_inset"
android:paddingBottom="@dimen/button_bar_height"
launcher:defaultScreen="2"
launcher:cellCountX="4"
launcher:cellCountY="4"
launcher:pageSpacing="@dimen/workspace_page_spacing"
launcher:scrollIndicatorPaddingLeft="@dimen/workspace_divider_padding_left"
launcher:scrollIndicatorPaddingRight="@dimen/workspace_divider_padding_right">
<include android:id="@+id/cell1" layout="@layout/workspace_screen" />
<include android:id="@+id/cell2" layout="@layout/workspace_screen" />
<include android:id="@+id/cell3" layout="@layout/workspace_screen" />
<include android:id="@+id/cell4" layout="@layout/workspace_screen" />
<include android:id="@+id/cell5" layout="@layout/workspace_screen" />
</com.android.launcher2.Workspace>
<include layout="@layout/hotseat"
android:id="@+id/hotseat"
android:layout_width="match_parent"
android:layout_height="@dimen/button_bar_height_plus_padding"
android:layout_gravity="bottom" />
<include
android:id="@+id/qsb_bar"
layout="@layout/qsb_bar" />
<include layout="@layout/apps_customize_pane"
android:id="@+id/apps_customize_pane"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible" />
<include layout="@layout/workspace_cling"
android:id="@+id/workspace_cling"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<include layout="@layout/folder_cling"
android:id="@+id/folder_cling"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</com.android.launcher2.DragLayer>
Launcher整個佈局的根是DragLayer,DragLayer繼承了FrameLayout,所以DragLayer本身可以看作是一個FrameLayout。下面是 dock_divider,它通過include關鍵字包含了另外一個佈局文件workspace_divider.xml ,而這個workspace_divider.xml包含了一ImageView,其實dock_divider就是dock區域上面的那條直線。
再下面是paged_view_indicator,同樣它包含了scroll_indicator.xml,其中包含了一個ImageView,顯示的是一個.9的png文件。實際上就是當Launcher滾動翻頁的時候,那個淡藍色的頁面指示條。
然後桌面的核心容器WorkSpace,如下圖所示,當然你看到的只是Workspace的一部分,其實是一個workspace_screen,通過Launcher.xml可以看到,整個workspace由5個workspace_screen組成,每個workspace_screen其實就是對應桌面一頁。而每個workspace_screen包含了一個CellLayout,這是一個自定義控件,繼承自ViewGroup,所以它算是一個用來佈局的控件,在這裏主要用來承載我們每頁的桌面圖標、widget和文件夾。
通過查看如下的佈局結構(由於圖太大隻截取了一部分)可以看到,Workspace包含了序號從0到4的5個CellLayout。
接下來是一個Hotseat,其實就是這塊dock區域了。如圖所示:
從如下的佈局圖我們可以看到,這個Hotseat其實還是包含了一個CellLayout,用來承載4個圖標和中間啓動所有程序的按鈕。
再下來就是那個qsb_bar,就是屏幕最頂端的google搜索框。這個搜索框是獨立於圖標界面的,所以當我們對桌面進行翻頁的時候,這個搜索框會巍然不動滴固定在最頂端,如下所示:
緊接着是3個初始化時被隱藏的界面。
apps_customize_pane,點擊dock中顯示所有應用程序的按鈕後纔會從隱藏狀態轉換爲顯示狀態,如下圖所示,顯示了所有應用程序和所有插件的界面。
通過查看apps_customize_pane.xml ,我們可以看到apps_customize_pane主要由兩部分組成:
tabs_container ,tabs部分,用來讓我們選擇是添加應用程序還是widget,如下圖所示:
tabcontent,選擇了相應的tab之後,下面的部分就會相應的顯示應用程序或是widget了,如下圖所示:
workspace_cling 和folder_cling 是剛刷完機後,進入桌面時,顯示的使用嚮導界面,介紹怎麼使用workspace和folder,跳過以後就再也不會出現了,這裏就不截圖了。