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

原文:http://mobile.51cto.com/hot-314700.htm

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

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

  1. <application 
  2.     android:name="com.android.launcher2.LauncherApplication" 
  3.     android:label="@string/application_name" 
  4.     android:icon="@drawable/ic_launcher_home" 
  5.     android:hardwareAccelerated="@bool/config_hardwareAccelerated" 
  6.     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中看到对是否启用硬件加速和大内存的配置。如下所示:

  1. <bool name="config_hardwareAccelerated">true</bool> 
  2. <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 代码如下:

  1. if (PROFILE_STARTUP) {  
  2.      android.os.Debug.startMethodTracing(  
  3.              Environment.getDataDirectory() + "/data/com.android.launcher/launcher");  
  4.  }  
  5.  if (PROFILE_STARTUP) {  
  6.      android.os.Debug.stopMethodTracing();  
  7.  }  

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

启动launcher后我们会发现在指定的目录下生成了launcher.trace文件

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

把launcher.trace文件通过DDMS pull到电脑上

点击查看大图

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

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

转载于:https://www.cnblogs.com/deve/archive/2012/06/28/2568977.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/361399.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用JFace Viewer延迟获取模型元素

Eclipse JFace Viewers显示的模型元素有时需要花费大量时间来加载。 因此&#xff0c; 工作台提供了IDeferredWorkbenchAdapter类型以在后台获取此类模型元素。 不幸的是&#xff0c;似乎仅通过DeferredTreeContentManager派生的AbstractTreeViewer支持此机制。 因此&#xff…

架构设计

好的架构是进化出来的 &#xff0c;不是设计出来的&#xff1b; 不同时期和不同环境有着不同的最佳架构&#xff1b; 存在即合理&#xff0c;合适的才是最好的&#xff1b; 转载于:https://www.cnblogs.com/sky-jyq/p/11527254.html

(网页)js每隔5分钟执行一次ajax请求的实现方法(转)

转自脚本之家: 一个页面好像只能有一个 window.οnlοadfunction(){}&#xff0c;所以要有多个事件&#xff0c;这样写就好了 window.onloadfunction(){ //假设这里每个五分钟执行一次test函数 publicBusi(); personBusi(); } function publicBusi(){ setTimeout(perso…

Flash网站流量统计的方法

在我印象中&#xff0c;一般的免费流量统计代码是不允许嵌套在Flash中使用的&#xff0c;而且也不允许在内置IFrame中嵌套使用&#xff0c;网上检索了一下&#xff0c;找到了Flash网站的流量统计办法&#xff0c;其实就是使用内置IFrame的办法&#xff0c;使用的是51啦&#xf…

从浏览器端JavaScript代码进行服务器端日志记录

应用程序日志记录是我们在部署到应用程序服务器上的应用程序中都要做的事情&#xff0c;对吗&#xff1f; 对于大多数Java开发人员而言&#xff0c;使用Log4J或Logback之类的框架似乎都是理所当然的。 但是&#xff0c;我们编写的在那些讨厌的浏览器中运行的代码又如何呢&#…

Oracle连接字符串记录

<?xml version"1.0" encoding"utf-8"?> <!--有关如何配置 ASP.NET 应用程序的详细信息&#xff0c;请访问https://go.microsoft.com/fwlink/?LinkId301879--> <configuration><configSections><!-- For more information on…

(网页)JS编程中,有时需要在一个方法返回两个个或两个以上的数据

转自脚本之家: 1 使用数组的方式&#xff0c;如下&#xff1a; <html> <head><title>JS函数返回多个值</title> </head> <body><input type"button" onclick"getNames()" value"test" /><script …

(网页)AngularJS 参考手册

指令描述ng-app定义应用程序的根元素。ng-bind绑定 HTML 元素到应用程序数据ng-bind-html绑定 HTML 元素的 innerHTML 到应用程序数据&#xff0c;并移除 HTML 字符串中危险字符ng-bind-template规定要使用模板替换的文本内容ng-blur规定 blur 事件的行为ng-change规定在内容改…

Apache Cassandra和Java入门(第一部分)

在此页面上&#xff0c;您将学到足够的知识来开始使用NoSQL Apache Cassandra和Java&#xff0c;包括如何安装&#xff0c;尝试一些基本命令以及下一步做什么。 要求 要遵循本教程&#xff0c;您应该已经有一个正在运行的Cassandra实例&#xff0c;并且已经在这里进行了10分钟…

vagrant网站中box下载方法

假设需要下载Laravel/homestead这个包。 首先定位到地址&#xff1a;https://app.vagrantup.com/laravel/boxes/homestead/versions/8.0.0 然后直接在后面加上 “providers/virtualbox.box”就可以下载了 (注意virtualbox.box是指供应商的名字&#xff0c;不同供应商提供不同名…

响应式Web设计——最佳指南

2012年被称为智能手机年。根据最近一份调查显示&#xff0c;美国的智能手机覆盖率已达50%。现在确实是提升移动终端用户体验的大好时机。如果你正运营一个网站&#xff0c;那就必须有一个响应式的Web设计&#xff0c;以便可以从移动终端上很好地访问你的网站。如果你还没意识到…

Eclipse扩展的轻量级集成测试

最近&#xff0c;我为Eclipse扩展点评估引入了一个小助手。 辅助程序努力减少通用编程步骤的样板代码&#xff0c;同时增加开发指导和可读性。 这篇文章是希望的后续文章&#xff0c;展示了如何将实用程序与AssertJ定制断言结合使用&#xff0c;以编写针对Eclipse扩展的轻量级…

原生js实现简单的焦点图效果

用到一些封装好的运动函数&#xff0c;主要是定时器 效果为图片和图片的描述定时自动更换 <!DOCTYPE html> <html><head><meta charset"UTF-8"><title></title><style type"text/css">ul,li,p,h3 {padding: 0;…

二:熟悉 TCP/IP 协议

一篇文章带你熟悉 TCP/IP 协议&#xff08;网络协议篇二&#xff09; 同样的&#xff0c;本文篇幅也比较长&#xff0c;先来一张思维导图&#xff0c;带大家过一遍。 一图看完本文 一、 计算机网络体系结构分层 计算机网络体系结构分层计算机网络体系结构分层不难看出&…

NYOJ 451(组合数+全错位)

光棍节的快乐 时间限制&#xff1a;1000 ms | 内存限制&#xff1a;65535 KB难度&#xff1a;2描述光棍们&#xff0c;今天是光棍节。聪明的NS想到了一个活动来丰富这个光棍节。 规则如下&#xff1a; 每个光棍在一个纸条上写一个自己心仪女生的名字&#xff0c;然后把这些纸条…

在Completablefuture和Observable之间转换

Java 8中的CompletableFuture<T>是对T类型的值将来将可用的承诺的高级抽象。 Observable<T>非常相似&#xff0c;但是它承诺将来会出现任意数量的项&#xff0c;从0到无穷大。 异步结果的这两种表示与仅使用一项即可使用Observable而不是CompletableFuture情况非常…

Box 'laravel/homestead' could not be found.

vbox vagrant.box Homestead 都安裝好的情況下。在homestead目錄下&#xff0c;執行vagrant up出錯 Homestead\scripts\文件夹中&#xff0c;打开homestead.rb文件打开homestead.rb文件 把config.vm.box_version settings["version"] || "> 0.4.0"改为…

DOM操作案例之--全选与反选

全选与反选在表单类的项目中还是很常见的&#xff0c;电商项目中的购物车一定少不了这个功能。 下面我只就用一个简单的案例做个演示吧。 <div class"wrap"><table><thead><tr><th><input type"checkbox" id"j_cbA…

沈阳出差归来

两个礼拜的出差终于结束了&#xff0c;现场开发锦江之星晚上熬夜到2点...我们开发着并快乐着&#xff0c;至少有一点是出差最大的收获&#xff0c;如何在高强度的项管中把握每个人的内心活动&#xff0c;在最及时的时候给与最贴心的帮助&#xff0c;以至于能让开发人员得到满足…

带有Swagger的Spring Rest API –公开文档

创建API文档后&#xff0c;将其提供给涉众非常重要。 在理想情况下&#xff0c;此发布的文档将具有足够的灵活性以解决任何最新更改&#xff0c;并且易于分发&#xff08;就成本以及完成此操作所需的时间而言&#xff09;。 为了使这成为可能&#xff0c;我们将利用我在上一篇文…