Android 4.0 Launcher源碼分析系列(一)

從今天起傻蛋打算做一個系列文章,對最新的Android 4.0 系統中的Launcher,也就是Android 4.0原生的桌面程序,進行一個深入淺出的分析,從而引領Android系統的編程愛好者對 Launcher的設計思想,實現方式來做一個研究,從而能夠通過這個實例最掌握到目前世界領先的設計方法,同時在程序中加入我們的一些新的實現。衆所周知,對一些優秀源代碼的分析,是提高編程水平的一條便捷的方式,希望本系列文章能夠給大家帶來一定的啓發,同時歡迎大家和作者一起討論,作者的微博是:http://weibo.com/zuiniuwang/

先從整體上對Launcher佈局作一個分析,讓我們通過查看Launcher.xml 和使用hierarchyviewer佈局查看工具兩者結合的方法來對Launcher的整體結構有個瞭解。通過hierarchyviewer來對整個桌面做個截圖,如下:

通過hierarchyviewer來對整個桌面做個截圖

放大後如下所示: 可以看到整個桌面包含的元素,最上面是Google的搜索框,下面是一個始終插件,然後是圖標,再有就是一個分隔線,最後是dock。請注意,桌面程序其實並不包含桌面壁紙,桌面壁紙其實是由 WallpaperManagerService來提供,整個桌面其實是疊加在整個桌面壁紙上的另外一個層。

放大後所示

點擊查看大圖

整個Launcher.xml佈局文件如下:

  1. <com.android.launcher2.DragLayer 
  2.     xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher" 
  4.  
  5.     android:id="@+id/drag_layer" 
  6.     android:layout_width="match_parent" 
  7.     android:layout_height="match_parent"> 
  8.  
  9.     <!-- Keep these behind the workspace so that they are not visible when 
  10.          we go into AllApps --> 
  11.     <include 
  12.         android:id="@+id/dock_divider" 
  13.         layout="@layout/workspace_divider" 
  14.         android:layout_width="match_parent" 
  15.         android:layout_height="wrap_content" 
  16.         android:layout_marginBottom="@dimen/button_bar_height" 
  17.         android:layout_gravity="bottom"  /> 
  18.     <include 
  19.         android:id="@+id/paged_view_indicator" 
  20.         layout="@layout/scroll_indicator" 
  21.         android:layout_width="wrap_content" 
  22.         android:layout_height="wrap_content" 
  23.         android:layout_gravity="bottom" 
  24.         android:layout_marginBottom="@dimen/button_bar_height"  /> 
  25.  
  26.     <!-- The workspace contains 5 screens of cells --> 
  27.     <com.android.launcher2.Workspace 
  28.         android:id="@+id/workspace" 
  29.         android:layout_width="match_parent" 
  30.         android:layout_height="match_parent" 
  31.         android:paddingTop="@dimen/qsb_bar_height_inset" 
  32.         android:paddingBottom="@dimen/button_bar_height" 
  33.         launcher:defaultScreen="2" 
  34.         launcher:cellCountX="4" 
  35.         launcher:cellCountY="4" 
  36.         launcher:pageSpacing="@dimen/workspace_page_spacing" 
  37.         launcher:scrollIndicatorPaddingLeft="@dimen/workspace_divider_padding_left" 
  38.         launcher:scrollIndicatorPaddingRight="@dimen/workspace_divider_padding_right"> 
  39.  
  40.         <include android:id="@+id/cell1" layout="@layout/workspace_screen"  /> 
  41.         <include android:id="@+id/cell2" layout="@layout/workspace_screen"  /> 
  42.         <include android:id="@+id/cell3" layout="@layout/workspace_screen"  /> 
  43.         <include android:id="@+id/cell4" layout="@layout/workspace_screen"  /> 
  44.         <include android:id="@+id/cell5" layout="@layout/workspace_screen"  /> 
  45.     </com.android.launcher2.Workspace> 
  46.  
  47.     <include layout="@layout/hotseat" 
  48.         android:id="@+id/hotseat" 
  49.         android:layout_width="match_parent" 
  50.         android:layout_height="@dimen/button_bar_height_plus_padding" 
  51.         android:layout_gravity="bottom"  /> 
  52.  
  53.     <include 
  54.         android:id="@+id/qsb_bar" 
  55.         layout="@layout/qsb_bar"  /> 
  56.  
  57.     <include layout="@layout/apps_customize_pane" 
  58.         android:id="@+id/apps_customize_pane" 
  59.         android:layout_width="match_parent" 
  60.         android:layout_height="match_parent" 
  61.         android:visibility="invisible"  /> 
  62.  
  63.     <include layout="@layout/workspace_cling" 
  64.         android:id="@+id/workspace_cling" 
  65.         android:layout_width="match_parent" 
  66.         android:layout_height="match_parent" 
  67.         android:visibility="gone"  /> 
  68.  
  69.     <include layout="@layout/folder_cling" 
  70.         android:id="@+id/folder_cling" 
  71.         android:layout_width="match_parent" 
  72.         android:layout_height="match_parent" 
  73.         android:visibility="gone"  /> 
  74. </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

點擊查看大圖

通過查看如下的佈局結構(由於圖太大隻截取了一部分)可以看到,Workspace包含了序號從0到4的5個CellLayout。

查看佈局結構

接下來是一個Hotseat,其實就是這塊dock區域了。如圖所示:

dock區域

點擊查看大圖

從如下的佈局圖我們可以看到,這個Hotseat其實還是包含了一個CellLayout,用來承載4個圖標和中間啓動所有程序的按鈕。

用來承載4個圖標和中間啓動所有程序的按鈕

再下來就是那個qsb_bar,就是屏幕最頂端的Google搜索框。這個搜索框是獨立於圖標界面的,所以當我們對桌面進行翻頁的時候,這個搜索框會巍然不動滴固定在最頂端,如下所示:

最頂端的Google搜索框

緊接着是3個初始化時被隱藏的界面。

apps_customize_pane,點擊dock中顯示所有應用程序的按鈕後纔會從隱藏狀態轉換爲顯示狀態,如下圖所示,顯示了所有應用程序和所有插件的界面。

所有應用程序和所有插件的界面

點擊查看大圖

通過查看apps_customize_pane.xml ,我們可以看到apps_customize_pane主要由兩部分組成:tabs_container 和tabcontent。tabs部分,用來讓我們選擇是添加應用程序還是widget,如下圖所示:

用來讓我們選擇是添加應用程序還是widget

點擊查看大圖

tabcontent,選擇了相應的tab之後,下面的部分就會相應的顯示應用程序或是widget了,如下圖所示:

面的部分就會相應的顯示應用程序或是widget

點擊查看大圖

workspace_cling  和 folder_cling 是剛刷完機後,進入桌面時,顯示的使用嚮導界面,介紹怎麼使用workspace和folder,跳過以後就再也不會出現了,這裏就不截圖了。

發佈了41 篇原創文章 · 獲贊 18 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章