【專家專欄】Android 4.0 Launcher源碼分析系列(一)
原創(chuàng)從今天起傻蛋打算做一個系列文章,對最新的Android 4.0 系統(tǒng)中的Launcher,也就是Android 4.0原生的桌面程序,進行一個深入淺出的分析,從而引領(lǐng)Android系統(tǒng)的編程愛好者對 Launcher的設(shè)計思想,實現(xiàn)方式來做一個研究,從而能夠通過這個實例最掌握到目前世界領(lǐng)先的設(shè)計方法,同時在程序中加入我們的一些新的實現(xiàn)。眾所周知,對一些優(yōu)秀源代碼的分析,是提高編程水平的一條便捷的方式,希望本系列文章能夠給大家?guī)硪欢ǖ膯l(fā),同時歡迎大家和作者一起討論,作者的微博是:http://weibo.com/zuiniuwang/
先從整體上對Launcher布局作一個分析,讓我們通過查看Launcher.xml 和使用hierarchyviewer布局查看工具兩者結(jié)合的方法來對Launcher的整體結(jié)構(gòu)有個了解。通過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關(guān)鍵字包含了另外一個布局文件workspace_divider.xml ,而這個workspace_divider.xml包含了一ImageView,其實dock_divider就是dock區(qū)域上面的那條直線。
再下面是paged_view_indicator,同樣它包含了scroll_indicator.xml,其中包含了一個ImageView,顯示的是一個.9的png文件。實際上就是當Launcher滾動翻頁的時候,那個淡藍色的頁面指示條。
然后桌面的核心容器WorkSpace,如下圖所示,當然你看到的只是Workspace的一部分,其實是一個workspace_screen,通過 Launcher.xml可以看到,整個workspace由5個workspace_screen組成,每個workspace_screen其實就是對應(yīng)桌面一頁。而每個workspace_screen包含了一個CellLayout,這是一個自定義控件,繼承自ViewGroup,所以它算是一個用來布局的控件,在這里主要用來承載我們每頁的桌面圖標、widget和文件夾。
通過查看如下的布局結(jié)構(gòu)(由于圖太大只截取了一部分)可以看到,Workspace包含了序號從0到4的5個CellLayout。
接下來是一個Hotseat,其實就是這塊dock區(qū)域了。如圖所示:
從如下的布局圖我們可以看到,這個Hotseat其實還是包含了一個CellLayout,用來承載4個圖標和中間啟動所有程序的按鈕。
再下來就是那個qsb_bar,就是屏幕最頂端的Google搜索框。這個搜索框是獨立于圖標界面的,所以當我們對桌面進行翻頁的時候,這個搜索框會巍然不動滴固定在最頂端,如下所示:
緊接著是3個初始化時被隱藏的界面。
apps_customize_pane,點擊dock中顯示所有應(yīng)用程序的按鈕后才會從隱藏狀態(tài)轉(zhuǎn)換為顯示狀態(tài),如下圖所示,顯示了所有應(yīng)用程序和所有插件的界面。
通過查看apps_customize_pane.xml ,我們可以看到apps_customize_pane主要由兩部分組成:tabs_container 和tabcontent。tabs部分,用來讓我們選擇是添加應(yīng)用程序還是widget,如下圖所示:
tabcontent,選擇了相應(yīng)的tab之后,下面的部分就會相應(yīng)的顯示應(yīng)用程序或是widget了,如下圖所示:
workspace_cling 和 folder_cling 是剛刷完機后,進入桌面時,顯示的使用向?qū)Ы缑?,介紹怎么使用workspace和folder,跳過以后就再也不會出現(xiàn)了,這里就不截圖了。
【51CTO.com獨家特稿,非經(jīng)授權(quán)謝絕轉(zhuǎn)載,合作媒體轉(zhuǎn)載請注明原文作者及出處!】