Android 9.0 Launcher源码分析(三)——Launcher的布局与多设备适配

转载请注明原地址:https://www.jianshu.com/p/484c1c5a1795

本文对Launcher的布局做一个整体性的描述。我们先看一下布局文件launcher.xml

<com.android.launcher3.LauncherRootView
    xmlns:android="<http://schemas.android.com/apk/res/android>"
    xmlns:launcher="<http://schemas.android.com/apk/res-auto>"
    android:id="@+id/launcher"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <com.android.launcher3.dragndrop.DragLayer
        android:id="@+id/drag_layer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:importantForAccessibility="no">

        <!-- The workspace contains 5 screens of cells -->
        <!-- DO NOT CHANGE THE ID -->
        <com.android.launcher3.Workspace
            android:id="@+id/workspace"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:theme="@style/HomeScreenElementTheme"
            launcher:pageIndicator="@+id/page_indicator" />

        <include
            android:id="@+id/overview_panel_container"
            layout="@layout/overview_panel"
            android:visibility="gone" />

        <!-- Keep these behind the workspace so that they are not visible when
         we go into AllApps -->
        <com.android.launcher3.pageindicators.WorkspacePageIndicator
            android:id="@+id/page_indicator"
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:layout_gravity="bottom|center_horizontal"
            android:theme="@style/HomeScreenElementTheme" />

        <include
            android:id="@+id/drop_target_bar"
            layout="@layout/drop_target_bar" />

        <include android:id="@+id/scrim_view"
            layout="@layout/scrim_view" />

        <include
            android:id="@+id/apps_view"
            layout="@layout/all_apps"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="invisible" />

        <!-- DO NOT CHANGE THE ID -->
        <include
            android:id="@+id/hotseat"
            layout="@layout/hotseat"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </com.android.launcher3.dragndrop.DragLayer>

</com.android.launcher3.LauncherRootView>

在常规状态下,几个主要的区域如下图所示。Workspace(@+id/workspace)是显示图标和widget的主体;PageIndicator(@+id/page_indicator,代码中是一个具体的对象WorkspacePageIndicator)是页面指示器,用于指示滑动和表明当前页面;HotSeat(@+id/hotseat)是常驻底部的图标栏;DragLayer是包在这些View的外层的一个ViewGroup,Launcher的拖拽操作需要依赖其内部实现,以后再详细说明。

然后还有常规情况下隐藏起来的一些重要区域,首先是@+id/overview_panel_container。我们知道在Android 9.0上,Recent界面不像以前一样是进入到一个新的页面,而是与Launcher的内容结合在一起。这个ViewGroup就是显示Recent内容的控件了。

然后还有@+id/drop_target_bar,当拖动图标或widget的内容时,出现在Launcher上方用于执行特定操作的控件。

Untitled.png

最后是@+id/apps_view,这就是应用抽屉了,容纳了所有应用图标。

另外还有容纳所有Widget的页面,Launcher设置页面,这里不详说。

那Launcher一页一页的视图结构是如何做到的呢?简单画了一个示意图,真正呈现到用户面前的就是这样一个结构。用户可见的部分为红框(DragLayer区域),Workspace真正的大小比可见区域要大很多。桌面上的每一页是一个CellLayout(也是一个自定义的ViewGroup),当有多页时,就一个一个横向排布在Workspace中。当触摸滑动桌面时,通过scrollTo来改变workspace内部子View的位置,就可以使用户看到不同页的内容了。

对于图标的排布,CellLayout还不是真正容纳图标的ViewGroup,每个CellLayout会包含一个ShortcutAndWidgetContainer,这才是真正容纳图标和Widget的ViewGroup。

介绍完布局结构,接下来看看一些布局数据的初始化过程。从上面已经可以看出,Launcher有一个相对复杂的视图结构,那么如何让这个视图在各种不同分辨率下都能良好的适配呢?继续分析源码。

在前文的Launcher启动流程(点此跳转)中提到过LauncherAppState这个类,在它初始化时有这样一句mInvariantDeviceProfile = new InvariantDeviceProfile(mContext),这里就已经开始有针对不同设备的处理逻辑了。我们看InvariantDeviceProfile的构造函数,重点在于findClosestDeviceProfiles这一句。

public InvariantDeviceProfile(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);

        Point smallestSize = new Point();
        Point largestSize = new Point();
        display.getCurrentSizeRange(smallestSize, largestSize);

        // This guarantees that width < height
        minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
        minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);

        ArrayList<InvariantDeviceProfile> closestProfiles = findClosestDeviceProfiles(
                minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context));
        InvariantDeviceProfile interpolatedDeviceProfileOut =
                invDistWeightedInterpolate(minWidthDps,  minHeightDps, closestProfiles);
                ...
    }

        /**
     * Returns the closest device profiles ordered by closeness to the specified width and height
     */
    // Package private visibility for testing.
    ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles(
            final float width, final float height, ArrayList<InvariantDeviceProfile> points) {

        // Sort the profiles by their closeness to the dimensions
        ArrayList<InvariantDeviceProfile> pointsByNearness = points;
        Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() {
            public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) {
                return Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
                        dist(width, height, b.minWidthDps, b.minHeightDps));
            }
        });

        return pointsByNearness;
    }

执行这个函数时,传入了根据当前设备DisplayMetrics计算得到的一个宽高值,另外还传入了一个ArrayList。

这里岔开一下,我们知道不同的设备屏幕宽高不同,那么桌面上放多少行多少列也应该要动态可调。我们当然可以在代码里写一个动态计算的规则,让其自动去适配不同的宽高,但对于定制ROM的厂家来说,同样的宽高下可能你想要5行,我只想要4行,所以这个计算规则如果要调整就得修改代码了。那么Launcher是如何去做这件事的呢?它定义了一个device_profile.xml,里面的内容我列出了其中两个。可以看到,Launcher的处理方式是在xml中由开发者自行定义对于一个设备的所有layout配置信息,包括多少行、多少列、图标大小、文件夹行列、HotSeat列数、默认图标排布的配置文件等。这样既不用改到代码,又可以直观且灵活地进行配置。

        <profile
        launcher:name="Nexus 10"
        launcher:minWidthDps="727"
        launcher:minHeightDps="1207"
        launcher:numRows="5"
        launcher:numColumns="6"
        launcher:numFolderRows="4"
        launcher:numFolderColumns="5"
        launcher:iconSize="76"
        launcher:iconTextSize="14.4"
        launcher:numHotseatIcons="7"
        launcher:defaultLayoutId="@xml/default_workspace_5x6"
        />

    <profile
        launcher:name="20-inch Tablet"
        launcher:minWidthDps="1527"
        launcher:minHeightDps="2527"
        launcher:numRows="7"
        launcher:numColumns="7"
        launcher:numFolderRows="6"
        launcher:numFolderColumns="6"
        launcher:iconSize="100"
        launcher:iconTextSize="20.0"
        launcher:numHotseatIcons="7"
        launcher:defaultLayoutId="@xml/default_workspace_5x6"
        />

了解了上面的信息,我们回到findClosestDeviceProfiles,传入的ArrayList就是解析这个xml之后传入的一个对象数组。然后根据宽高找到最接近的一个设备,然后Launcher就知道了在这个设备上的想要的各个基本数据了,将其储存在成员变量中。后面使用这些数据的地方就比较分散了,比如某个ViewGroup onLayout时、文件夹初始化时等,这里就不一一列出了。大家只要知道这些宽高、行列、padding等数据的来源都是此时决定好了的就可以了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266