Android4.4-Launcher源码分析系列之Launcher介绍

来源:互联网 发布:关于网络拍卖司法解释 编辑:程序博客网 时间:2024/05/22 15:00

一.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类的整个流程会让大家懵逼的.

欢迎留言




1 0