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

著名手机厂商Android开发工程师、最牛网站长傻蛋曾为51CTO撰稿《Android 4.0的图形硬件加速及绘制技巧》受到读者的广泛欢迎。傻蛋同学将在新的一年里在51CTO开设专家专栏,本文为傻蛋正在研究的一个方向,与网友共同探讨Android 4.0原生的桌面程序——Launcher。

从今天起傻蛋打算做一个系列文章,对最新的Android4.0系统中的Launcher,也就是Android4.0原生的桌面程序,进行一个深入浅出的分析,从而引领Android系统的编程爱好者对Launcher的设计思想,实现方式来做一个研究,从而能够通过这个实例最掌握到目前世界领先的设计方法,同时在程序中加入我们的一些新的实现。众所周知,对一些优秀源代码的分析,是提高编程水平的一条便捷的方式,希望本系列文章能够给大家带来一定的启发,同时欢迎大家和作者一起讨论,作者的微博是:http://weibo.com/zuiniuwang/

先从整体上对Launcher布局作一个分析,让我们通过查看Launcher.xml和使用hierarchyviewer布局查看工具两者结合的方法来对Launcher的整体结构有个了解。通过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. android:id="@+id/drag_layer"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <!--Keepthesebehindtheworkspacesothattheyarenotvisiblewhen
  8. wegointoAllApps-->
  9. <include
  10. android:id="@+id/dock_divider"
  11. layout="@layout/workspace_divider"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_marginBottom="@dimen/button_bar_height"
  15. android:layout_gravity="bottom"/>
  16. <include
  17. android:id="@+id/paged_view_indicator"
  18. layout="@layout/scroll_indicator"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_gravity="bottom"
  22. android:layout_marginBottom="@dimen/button_bar_height"/>
  23. <!--Theworkspacecontains5screensofcells-->
  24. <com.android.launcher2.Workspace
  25. android:id="@+id/workspace"
  26. android:layout_width="match_parent"
  27. android:layout_height="match_parent"
  28. android:paddingTop="@dimen/qsb_bar_height_inset"
  29. android:paddingBottom="@dimen/button_bar_height"
  30. launcher:defaultScreen="2"
  31. launcher:cellCountX="4"
  32. launcher:cellCountY="4"
  33. launcher:pageSpacing="@dimen/workspace_page_spacing"
  34. launcher:scrollIndicatorPaddingLeft="@dimen/workspace_divider_padding_left"
  35. launcher:scrollIndicatorPaddingRight="@dimen/workspace_divider_padding_right">
  36. <includeandroid:id="@+id/cell1"layout="@layout/workspace_screen"/>
  37. <includeandroid:id="@+id/cell2"layout="@layout/workspace_screen"/>
  38. <includeandroid:id="@+id/cell3"layout="@layout/workspace_screen"/>
  39. <includeandroid:id="@+id/cell4"layout="@layout/workspace_screen"/>
  40. <includeandroid:id="@+id/cell5"layout="@layout/workspace_screen"/>
  41. </com.android.launcher2.Workspace>
  42. <includelayout="@layout/hotseat"
  43. android:id="@+id/hotseat"
  44. android:layout_width="match_parent"
  45. android:layout_height="@dimen/button_bar_height_plus_padding"
  46. android:layout_gravity="bottom"/>
  47. <include
  48. android:id="@+id/qsb_bar"
  49. layout="@layout/qsb_bar"/>
  50. <includelayout="@layout/apps_customize_pane"
  51. android:id="@+id/apps_customize_pane"
  52. android:layout_width="match_parent"
  53. android:layout_height="match_parent"
  54. android:visibility="invisible"/>
  55. <includelayout="@layout/workspace_cling"
  56. android:id="@+id/workspace_cling"
  57. android:layout_width="match_parent"
  58. android:layout_height="match_parent"
  59. android:visibility="gone"/>
  60. <includelayout="@layout/folder_cling"
  61. android:id="@+id/folder_cling"
  62. android:layout_width="match_parent"
  63. android:layout_height="match_parent"
  64. android:visibility="gone"/>
  65. </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包含了序号从0到4的5个CellLayout。

接下来是一个Hotseat,其实就是这块dock区域了。如图所示:

点击查看大图

从如下的布局图我们可以看到,这个Hotseat其实还是包含了一个CellLayout,用来承载4个图标和中间启动所有程序的按钮。

再下来就是那个qsb_bar,就是屏幕最顶端的Google搜索框。这个搜索框是独立于图标界面的,所以当我们对桌面进行翻页的时候,这个搜索框会巍然不动滴固定在最顶端,如下所示:

紧接着是3个初始化时被隐藏的界面。

apps_customize_pane,点击dock中显示所有应用程序的按钮后才会从隐藏状态转换为显示状态,如下图所示,显示了所有应用程序和所有插件的界面。

点击查看大图

通过查看apps_customize_pane.xml,我们可以看到apps_customize_pane主要由两部分组成:tabs_container和tabcontent。tabs部分,用来让我们选择是添加应用程序还是widget,如下图所示:

点击查看大图

tabcontent,选择了相应的tab之后,下面的部分就会相应的显示应用程序或是widget了,如下图所示:

点击查看大图

workspace_cling和folder_cling是刚刷完机后,进入桌面时,显示的使用向导界面,介绍怎么使用workspace和folder,跳过以后就再也不会出现了,这里就不截图了。

【51CTO.com独家特稿,非经授权谢绝转载,合作媒体转载请注明原文作者及出处!】

http://mobile.51cto.com/hot-312129.htm

更多相关文章

  1. Android(安卓)Material Design:CoordinatorLayout与NestedScrollV
  2. Android(安卓)多媒体 -- 四种播放视频的方法
  3. android布局相关
  4. Android界面——控件和布局
  5. 小视频源码,android 判断布局是否为RTL布局
  6. android DrawerLayout 点击穿透、点击自身消失等问题解决
  7. Android(安卓)布局添加阴影背景ShadowDrawable
  8. Android常用的输入框
  9. Android(安卓)单选之史上最简单的ListView实现单选效果

随机推荐

  1. Android进程内存上限
  2. Android开发之ConstraintLayout布局
  3. android:theme="@android:style/Theme.Li
  4. Android系统自带主题的使用及自定义主题
  5. Android系统在超级终端下必会的命令大全(a
  6. Android圆角图片
  7. Android(安卓)XML解析
  8. Android(安卓)之EditText自动弹出/不弹出
  9. Android控件属性大全
  10. android相对布局的案例