Android4.4-Launcher源碼分析系列之Launcher介紹

一.Launcher是什麼

Launcher是啓動器的意思,最直觀的就是手機上的桌面.其實它是個Activity.

    public class Launcher extends Activity implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
它負責管理桌面,包括顯示快捷方式,widget,拖動圖標,卸載app等.

二.分析Launcher佈局

對於分析陌生的代碼,最直觀的是先查看它的佈局. 我們打開模擬器,我用的是Genymation.

把eclipse的Launcher源碼導出apk, 右鍵項目Launcher點AndroidTools的Export Signed Application Package選項就可以導出了,然後Push到模擬器的system/priv-app目錄下即可.

然後點擊設置裏的所有app,找到Launcher,清除數據就可以看到Launcher4.4的原生桌面了.


Launcher裏的佈局很多是自定義的,無法通過eclipse上預覽出來,這時候就要用到一個工具了,hierarchyviewer.bat,在sdk\tools目錄下,雙擊運行.

然後雙擊黑色的部分,就會出現下圖所示

這裏面就是整個Launcher的佈局結構.

爲了能讓大家更直白的理解,我就再畫一張圖


我們從外到內抽絲剝繭.

DragLaye                               是最外層的佈局,它繼承自FrameLayout.

SearchDropTargetBar         是頂部的搜索框和刪除框,當你拖動圖標時就隱藏搜索框,顯示刪除框

Workspace                            繼承自ViewGroup,負責左右頁面的滑動

CellLayout                            繼承自ViewGroup,一個Workspace由多個CellLayout組成,每一個CellLayout負責裏面圖標(favorite)和widget的顯示

PageIndicator 繼承自LinearLayout,就是頁面滾動指示器,幾個小圓點

HotSet                                  繼承自FrameLayout,底部固定的不隨頁面滑動的幾個圖標


其實還有幾個隱藏佈局

overview_panel 長按屏幕時出現選擇壁紙、選擇widget和設置的佈局

first_run_cling 第一次運行時顯示的佈局

workspace_cling 第一次進Home顯示的佈局

folder_cling    第一次進文件夾存儲快捷方式顯示

apps_customize_pane        進入所有app界面時的佈局


Launcher的佈局是res/layout-port/launcher.xml,如果你的設備是橫屏,應該在res/layout-land/launcher.xml.

我們看下它的佈局吧

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:launcher="http://schemas.android.com/apk/res-auto/com.android.launcher3"
    android:id="@+id/launcher"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/workspace_bg" >

    <com.android.launcher3.DragLayer
        android:id="@+id/drag_layer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <!-- The workspace contains 5 screens of cells -->

        <com.android.launcher3.Workspace
            android:id="@+id/workspace"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            launcher:defaultScreen="@integer/config_workspaceDefaultScreen"
            launcher:pageIndicator="@id/page_indicator"
            launcher:pageSpacing="@dimen/workspace_page_spacing" >
        </com.android.launcher3.Workspace>
        <!-- 底部不隨頁面滑動的幾個快捷方式 -->

        <include
            android:id="@+id/hotseat"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/hotseat" />
       
        <!-- 長按屏幕時出現選擇壁紙、選擇widget和設置的佈局 -->
	    <include
            android:id="@+id/overview_panel"
            layout="@layout/overview_panel"
            android:visibility="gone" />

        <!-- 圓形指示器 -->
  	    <include
            android:id="@+id/page_indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            layout="@layout/page_indicator" />

        <!-- 搜索/刪除框 -->
 	   <include
            android:id="@+id/qsb_bar"
            layout="@layout/qsb_bar" />
      
       <com.android.launcher3.ScrimView
            android:id="@+id/cling_scrim"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone" />
       
       <!-- 第一次運行時顯示 -->
 	   <include
            android:id="@+id/first_run_cling"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/first_run_cling"
            android:visibility="gone" />
        
        <!-- 第一次進Home顯示 -->
        <include
            android:id="@+id/workspace_cling"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/workspace_cling"
            android:visibility="gone" />
        
        <!-- 第一次進文件夾存儲快捷方式顯示 -->
  	    <include
            android:id="@+id/folder_cling"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/folder_cling"
            android:visibility="gone" />
 
        <com.android.launcher3.DrawableStateProxyView
            android:id="@+id/voice_button_proxy"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_gravity="top|end"
            android:clickable="true"
            android:importantForAccessibility="no"
            android:onClick="onClickVoiceButton"
            launcher:sourceViewId="@+id/voice_button" />
        
        <!-- 所有應用界面 -->
 		  <include
            android:id="@+id/apps_customize_pane"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/apps_customize_pane"
            android:visibility="invisible" />
    </com.android.launcher3.DragLayer>

</FrameLayout>
可以與上面一張圖對應着看,這樣更容易理解.

我打算先把常用的類和接口先做個分析,不然一上來就分析Launcher類的整個流程會讓大家懵逼的.

歡迎留言




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章