Launcher 是 Android手機開啓後第一個運行的 應用程序,也叫Home,或者叫做手機桌面。
本文介紹的是4.1源碼的launcher2 app. Android41\packages\apps\Launcher2
首先找到主Activity,打開AndroidManifest.xml 入口是 com.android.launcher2.Launcher 這個類
Launcher 主界面包含 wallpaper牆紙,work_screen屏幕, 最底部的hotseat, 以及all apps.
onCreate方法裏面 主要初始化一些對象,包括拖拽對象,hotSeat, 牆紙大小設置,
打開主佈局文件launcher.xml
外層是一個Framelayout疊層 com.android.launcher2.DragLayer
com.android.launcher2.Workspace.java 爲主要Home Screen
手機裏面包含了多個屏的滑動,一共有5個
<com.android.launcher2.Workspace
android:id="@+id/workspace"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/workspace_left_padding"
android:paddingRight="@dimen/workspace_right_padding"
android:paddingTop="@dimen/workspace_top_padding"
android:paddingBottom="@dimen/workspace_bottom_padding"
launcher:defaultScreen="2"
launcher:cellCountX="@integer/cell_count_x"
launcher:cellCountY="@integer/cell_count_y"
launcher:pageSpacing="@dimen/workspace_page_spacing"
launcher:scrollIndicatorPaddingLeft="@dimen/qsb_bar_height"
launcher:scrollIndicatorPaddingRight="@dimen/button_bar_height">
<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>
每一個屏叫做 com.android.launcher2.CellLayout.java
<com.android.launcher2.CellLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/cell_layout_left_padding"
android:paddingRight="@dimen/cell_layout_right_padding"
android:paddingTop="@dimen/cell_layout_top_padding"
android:paddingBottom="@dimen/cell_layout_bottom_padding"
android:hapticFeedbackEnabled="false"
launcher:cellWidth="@dimen/workspace_cell_width"
launcher:cellHeight="@dimen/workspace_cell_height"
launcher:widthGap="@dimen/workspace_width_gap"
launcher:heightGap="@dimen/workspace_height_gap"
launcher:maxGap="@dimen/workspace_max_gap" />
Workspace.java 是繼承ViewGroup
CellLayout 也是繼承ViewGroup
Workspace.java 對象在Launcher裏面做了一些初始化。
首先在setupViews方法裏面 獲取了對象引用。還包含了shortcut添加與刪除操作。
在Workspace.java中onTouchEvent方法中,監聽了屏幕的滑動操作,比如長按,拖動app圖標。
拖動用DragController.java類,處理拖動計算座標。
暫時說到這,如有問題,請指出謝謝。