上一节我们研究了Launcher的整体结构,这一节我们看看整个Laucher的入口点,同时Laucher在加载了它的布局文件Laucher.xml时都干了些什么。

我们在源代码中可以找到LauncherApplication, 它继承了Application类,当整个Launcher启动时,它就是整个程序的入口。我们先来看它们在AndroidManifest.xml中是怎么配置的。

    <applicationandroid:name="com.android.launcher2.LauncherApplication"android:label="@string/application_name"android:icon="@drawable/ic_launcher_home"android:hardwareAccelerated="@bool/config_hardwareAccelerated"android:largeHeap="@bool/config_largeHeap">    

首先通过android:name指定了整个Launcher的Application也就是入口是在com.android.launcher2.LauncherApplication这个路径下,android:lable指定了桌面的名字是叫Launcher,如果要改名字就改values文件夹的string.xml中的相应属性就可以了。android:icon指定了Laucher的图标,这个图标可以在应用程序管理器中看见,如下图所示,是个可爱机器人住在一个小房子里面,如果需要更改Laucher的图片,重新设置这个属性就可以了。

      

android:hardwareAccelerated="@bool/config_hardwareAccelerated" 指定了整个应用程序是启用硬件加速的,这样整个应用程序的运行速度会更快。

android:largeHeap="@bool/config_largeHeap" 指定了应用程序使用了大的堆内存,能在一定程度上避免,对内存out of memory错误的出现。我们可以在values文件夹的config.xml中看到对是否启用硬件加速和大内存的配置。如下所示:

    <bool name="config_hardwareAccelerated">true</bool><bool name="config_largeHeap">false</bool>

在Application中onCreate()方法通过:sIsScreenLarge= screenSize== Configuration.SCREENLAYOUT_SIZE_LARGE|| screenSize== Configuration.SCREENLAYOUT_SIZE_XLARGE;和sScreenDensity= getResources().getDisplayMetrics().density;来判断是否是大屏幕,同时得到它的屏幕密度。同时通过mIconCache = new IconCache(this); 来设置了应用程序的图标的cache,然后申明了LauncherModel,mModel = new LauncherModel(this, mIconCache); LauncherModel主要用于加载桌面的图标、插件和文件夹,同时LaucherModel是一个广播接收器,在程序包发生改变、区域、或者配置文件发生改变时,都会发送广播给LaucherModel,LaucherModel会根据不同的广播来做相应加载操作,此部分会在后面做详细介绍。

在LauncherApplication完成初始化工作之后,我们就来到了Launcher.java的onCreate()方法,同样是启动桌面时的一系列初始化工作。

首先需要注意的是在加载launcher布局文件时的一个TraceView的调试方法,它能够对在他们之间的方法进行图形化的性能分析,并且能够具体到method 代码如下:

       if (PROFILE_STARTUP) {android.os.Debug.startMethodTracing(Environment.getDataDirectory() + "/data/com.android.launcher/launcher");}if (PROFILE_STARTUP) {android.os.Debug.stopMethodTracing();}

我指定的生成性能分析的路径是:/data/data/com.android.launcher/launcher,启动launcher后我们会发现在指定的目录下生成了launcher.trace文件,如下图所示:

把launcher.trace文件通过DDMS pull到电脑上 , 在SDK的tools目录里,执行traceview工具来打开launcher.trace .如下图所示:

可以看到setContentView使用了448.623ms,占整个跟踪代码时间的62%,所以说在加载布局文件时,肯定经过了一系列的加载运算,我们接着分析。

当加载launcher布局文件的过程时,最为关键的时对整个workspace的加载,workspace是一个自定义组件,它的继承关系如下所示,可以看到Workspace实际上也是一个ViewGroup,可以加入其他控件。

当ViewGroup组件进行加载的时候首先会读取本控件对应的XML文件,然后Framework层会执行它的onMeasure()方法,根据它所包含的子控件大小来计算出整个控件要在屏幕上占的大小。Workspace重写了ViewGroup的onMeasure方法(在PagedView中),在workspace中是对5个子CellLayout进行测量,的方法如下, 具体含义请看注释:

  1. @Override
  2. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  3. if (!mIsDataReady) {
  4. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  5. return;
  6. }
  7. //得到宽度的模式(在配置文件中对应的是match_parent 或者 wrap_content)和其大小
  8. final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  9. final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  10. //宽度必须是match_parent,否则会抛出异常。
  11. if (widthMode != MeasureSpec.EXACTLY) {
  12. throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
  13. }
  14. /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
  15. * of the All apps view on XLarge displays to not take up more space then it needs. Width
  16. * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
  17. * each page to have the same width.
  18. */
  19. //高度允许是wrap_content,因为在大屏幕的情况下,会占了多余的位置
  20. final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  21. int heightSize = MeasureSpec.getSize(heightMeasureSpec);
  22. int maxChildHeight = 0;
  23. //得到在竖值方向上和水平方向上的Padding
  24. final int verticalPadding = mPaddingTop + mPaddingBottom;
  25. final int horizontalPadding = mPaddingLeft + mPaddingRight;
  26. // The children are given the same width and height as the workspace
  27. // unless they were set to WRAP_CONTENT
  28. if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize  + " mPaddingTop="+mPaddingTop + " mPaddingBottom="+mPaddingBottom);
  29. final int childCount = getChildCount();
  30. //对workspace的子View进行遍历,从而对它的几个子view进行测量。
  31. for (int i = 0; i < childCount; i++) {
  32. // disallowing padding in paged view (just pass 0)
  33. final View child = getPageAt(i);
  34. final LayoutParams lp = (LayoutParams) child.getLayoutParams();
  35. int childWidthMode;
  36. if (lp.width == LayoutParams.WRAP_CONTENT) {
  37. childWidthMode = MeasureSpec.AT_MOST;
  38. } else {
  39. childWidthMode = MeasureSpec.EXACTLY;
  40. }
  41. int childHeightMode;
  42. if (lp.height == LayoutParams.WRAP_CONTENT) {
  43. childHeightMode = MeasureSpec.AT_MOST;
  44. } else {
  45. childHeightMode = MeasureSpec.EXACTLY;
  46. }
  47. final int childWidthMeasureSpec =
  48. MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
  49. final int childHeightMeasureSpec =
  50. MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
  51. //对子View的大小进行设置,传入width和height参数
  52. child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
  53. maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
  54. if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
  55. + child.getMeasuredHeight());
  56. }
  57. if (heightMode == MeasureSpec.AT_MOST) {
  58. heightSize = maxChildHeight + verticalPadding;
  59. }
  60. //存储测量后的宽度和高度
  61. setMeasuredDimension(widthSize, heightSize);
  62. // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
  63. // We also wait until we set the measured dimensions before flushing the cache as well, to
  64. // ensure that the cache is filled with good values.
  65. invalidateCachedOffsets();
  66. updateScrollingIndicatorPosition();
  67. if (childCount > 0) {
  68. mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
  69. } else {
  70. mMaxScrollX = 0;
  71. }
  72. }

测量完毕之后就可以对子控件进行布局了,这时候Framework层会调用PagedView中重写的onLayout方法。

  1. @Override
  2. protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  3. if (!mIsDataReady) {
  4. return;
  5. }
  6. if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
  7. //竖值方向的Padding
  8. final int verticalPadding = mPaddingTop + mPaddingBottom;
  9. final int childCount = getChildCount();
  10. int childLeft = 0;
  11. if (childCount > 0) {
  12. if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
  13. + getChildWidth(0));
  14. childLeft = getRelativeChildOffset(0);
  15. //偏移量为0
  16. if (DEBUG) Log.d(TAG, "childLeft:"+childLeft);
  17. // Calculate the variable page spacing if necessary
  18. // 如果mPageSpacing小于0的话,就重新计算mPageSpacing,并且给它赋值。
  19. if (mPageSpacing < 0) {
  20. setPageSpacing(((right - left) - getChildAt(0).getMeasuredWidth()) / 2);
  21. }
  22. }
  23. for (int i = 0; i < childCount; i++) {
  24. final View child = getPageAt(i);
  25. if (child.getVisibility() != View.GONE) {
  26. final int childWidth = getScaledMeasuredWidth(child);
  27. final int childHeight = child.getMeasuredHeight();
  28. int childTop = mPaddingTop;
  29. if (mCenterPagesVertically) {
  30. childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
  31. }
  32. if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
  33. //把5个CellLayout布局到相应的位置,layout的4个参数分别是 左、上、右、下。
  34. child.layout(childLeft, childTop,
  35. childLeft + child.getMeasuredWidth(), childTop + childHeight);
  36. childLeft += childWidth + mPageSpacing;
  37. }
  38. }
  39. //第一次布局完毕之后,就根据当前页偏移量(当前页距离Workspace最左边的距离)滚动到默认的页面去,第一次布局时
  40. //默认的当前页是3,则它的便宜量就是两个CellLayout的宽度。
  41. if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
  42. setHorizontalScrollBarEnabled(false);
  43. int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
  44. //滚动到指定的位置
  45. scrollTo(newX, 0);
  46. mScroller.setFinalX(newX);
  47. if (DEBUG) Log.d(TAG, "newX is "+newX);
  48. setHorizontalScrollBarEnabled(true);
  49. mFirstLayout = false;
  50. }
  51. if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
  52. mFirstLayout = false;
  53. }
  54. }

转载于:https://blog.51cto.com/zuiniuwang/773412

Android4.0 Launcher 源码分析系列(二)相关推荐

  1. Android 4.0 Launcher源码分析系列(二)

    原文:http://mobile.51cto.com/hot-314700.htm 上一节我们研究了Launcher的整体结构,这一节我们看看整个Laucher的入口点,同时Laucher在加载了它的 ...

  2. 菜鸟读jQuery 2.0.3 源码分析系列(1)

    原文链接在这里,作为一个菜鸟,我就一边读一边写 jQuery 2.0.3 源码分析系列 前面看着差不多了,看到下面一条(我是真菜鸟),推荐木有入门或者刚刚JS入门摸不着边的看看,大大们手下留情,想一起 ...

  3. vue源码分析系列二:$mount()和new Watcher()的执行过程

    续vue源码分析系列一:new Vue的初始化过程 在initMixin()里面调用了$mount() if (vm.$options.el) {vm.$mount(vm.$options.el);/ ...

  4. android 8 ril,Android 8.0 RIL源码分析(二)

    非URC消息处理 之前分析到在at_send_command_full_nolock调用后会先发送消息给modem,然后阻塞当前线程等待modem返回消息. 因此也是在readerLoop的消息处理中 ...

  5. android 蓝牙扫描流程,Android 9.0 Bluetooth源码分析(二)蓝牙扫描流程

    1 UI 蓝牙开始扫描位于setting的 /packages/apps/Settings/src/com/android/settings/bluetooth/BluetoothPairingDet ...

  6. Jedis 1.0.0 版 源码分析系列3:JedisPool.java

    2019独角兽企业重金招聘Python工程师标准>>> package redis.clients.jedis;import redis.clients.util.FixedReso ...

  7. Hhadoop-2.7.0中HDFS写文件源码分析(二):客户端实现(1)

    一.综述 HDFS写文件是整个Hadoop中最为复杂的流程之一,它涉及到HDFS中NameNode.DataNode.DFSClient等众多角色的分工与合作. 首先上一段代码,客户端是如何写文件的: ...

  8. idea 线程内存_Java线程池系列之-Java线程池底层源码分析系列(二)

    课程简介: 课程目标:通过本课程学习,深入理解Java线程池,提升自身技术能力与价值. 适用人群:具有Java多线程基础的人群,希望深入理解线程池底层原理的人群. 课程概述:多线程的异步执行方式,虽然 ...

  9. android4.0.3源码之硬件gps简单移植

    [转]我和菜鸟一起学android4.0.3源码之硬件gps简单移植 2013-7-5阅读94 评论0 关于android定位方式 android 定位一般有四种方法,这四种方式分别是GPS定位.WI ...

最新文章

  1. 利用ArcGIS Python批量拼接裁剪遥感影像(arcpy batch processing)
  2. 问题:出现在哪个地方?关于map的搜索问题
  3. python import 问题
  4. stylus之方法(Functions)
  5. RDLC报表---自定义数据集
  6. 移动文件读/写指针----lseek
  7. mysql改utf8mb4后速度慢_更改MySQL数据库的编码为utf8mb4
  8. 如何用电脑快速制作gif动态图片
  9. 后缀数组模板 (详细注释)
  10. 基金使用计划 数学建模 matlab,基金使用计划模型
  11. 区块链马拉松|Blockathon(2018)上海站开放报名(HiBlock)
  12. linux系统修改etc,Linux系统中修改/etc/profile文件的方法
  13. Shiro权限管理实现(详解)
  14. 在VS2010中ActiveX控件注册方法,使用regsvr32命令
  15. 随手记:小程序相关知识点
  16. C语言程序设计——计算梯形面积
  17. 想拥有一个自由时间的职业_如何以自由职业者的身份管理时间
  18. 腾讯云数据库产品介绍第四章-
  19. Towards 3D Human Pose Estimation in the Wild: a Weakly-supervised Approach论文翻译
  20. (转)SQL Server数据库复制错误的原因及解决方法

热门文章

  1. Linux系统命令sort详解
  2. Django路由介绍
  3. 快速迭代的测试人员的思考
  4. ASP.NET MVC4 路由的配置 十种方法
  5. HDU - 1875 畅通工程再续
  6. 纯代码实现wordpress文章隐藏内容评论可见
  7. C# 中常用数据类型与控件类型的命名规则
  8. Codeforces Round #200 (Div. 1)A. Rational Resistance 数学
  9. 邮件系列(二)-发送邮件
  10. Spring Boot中使用RabbitMQ