修改android N 的Launcher3桌面显示的行数列数

修改android N 的Launcher3桌面显示的行数列数

在InvariantDeviceProfile中会根据屏幕的不同去动态适配应用图标和字体和行列数。

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);

    InvariantDeviceProfile closestProfile = closestProfiles.get(0);
    numRows = closestProfile.numRows;
    numColumns = closestProfile.numColumns;
    numHotseatIcons = closestProfile.numHotseatIcons;
    defaultLayoutId = closestProfile.defaultLayoutId;
    numFolderRows = closestProfile.numFolderRows;
    numFolderColumns = closestProfile.numFolderColumns;
    minAllAppsPredictionColumns = closestProfile.minAllAppsPredictionColumns;

    iconSize = interpolatedDeviceProfileOut.iconSize;
    iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
    iconTextSize = interpolatedDeviceProfileOut.iconTextSize;
    hotseatIconSize = interpolatedDeviceProfileOut.hotseatIconSize;
    fillResIconDpi = getLauncherIconDensity(iconBitmapSize);

    // If the partner customization apk contains any grid overrides, apply them
    // Supported overrides: numRows, numColumns, iconSize
    applyPartnerDeviceProfileOverrides(context, dm);

    Point realSize = new Point();
    display.getRealSize(realSize);
    // The real size never changes. smallSide and largeSide will remain the
    // same in any orientation.
    int smallSide = Math.min(realSize.x, realSize.y);
    int largeSide = Math.max(realSize.x, realSize.y);

    landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
            largeSide, smallSide, true /* isLandscape */);
    portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
            smallSide, largeSide, false /* isLandscape */);

    // We need to ensure that there is enough extra space in the wallpaper
    // for the intended parallax effects
    if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
        defaultWallpaperSize = new Point(
                (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
                largeSide);
    } else {
        defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
    }
}

在这里行数列数是根据closestProfile去获取的

numRows = closestProfile.numRows;
numColumns = closestProfile.numColumns;

看看closestProfile

ArrayList<InvariantDeviceProfile> closestProfiles = findClosestDeviceProfiles(
        minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context));

closestProfiles是根据屏幕宽高去找最接近的配置文件的。在这里看到有一个是默认的DeviceProfiles—getPredefinedDeviceProfiles 代码如下

ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles(Context context) {
    ArrayList<InvariantDeviceProfile> profiles = new ArrayList<>();
    try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
        final int depth = parser.getDepth();
        int type;

        while (((type = parser.next()) != XmlPullParser.END_TAG ||
                parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
            if ((type == XmlPullParser.START_TAG) && "profile".equals(parser.getName())) {
                TypedArray a = context.obtainStyledAttributes(
                        Xml.asAttributeSet(parser), R.styleable.InvariantDeviceProfile);
                int numRows = a.getInt(R.styleable.InvariantDeviceProfile_numRows, 0);
                int numColumns = a.getInt(R.styleable.InvariantDeviceProfile_numColumns, 0);
                float iconSize = a.getFloat(R.styleable.InvariantDeviceProfile_iconSize, 0);
                profiles.add(new InvariantDeviceProfile(
                        a.getString(R.styleable.InvariantDeviceProfile_name),
                        a.getFloat(R.styleable.InvariantDeviceProfile_minWidthDps, 0),
                        a.getFloat(R.styleable.InvariantDeviceProfile_minHeightDps, 0),
                        numRows,
                        numColumns,
                        a.getInt(R.styleable.InvariantDeviceProfile_numFolderRows, numRows),
                        a.getInt(R.styleable.InvariantDeviceProfile_numFolderColumns, numColumns),
                        a.getInt(R.styleable.InvariantDeviceProfile_minAllAppsPredictionColumns, numColumns),
                        iconSize,
                        a.getFloat(R.styleable.InvariantDeviceProfile_iconTextSize, 0),
                        a.getInt(R.styleable.InvariantDeviceProfile_numHotseatIcons, numColumns),
                        a.getFloat(R.styleable.InvariantDeviceProfile_hotseatIconSize, iconSize),
                        a.getResourceId(R.styleable.InvariantDeviceProfile_defaultLayoutId, 0)));
                a.recycle();
            }
        }
    } catch (IOException|XmlPullParserException e) {
        throw new RuntimeException(e);
    }
    return profiles;
}

在这里获取默认的DeviceProfiles list。查看R.xml.device_profiles 发现里面定义了很多手机类型
我们要更改行数列数的话可以在这里添加自己设备的profile,宽和高最好跟自己的设备接近,这样匹配到的就会是自己定义的peofile,也就能控制做界面应用的行数和列数了,还有其他的参数

<profile
launcher:name="Super Short Stubby"
launcher:minWidthDps="255"
launcher:minHeightDps="300"
launcher:numRows="2"
launcher:numColumns="3"
launcher:numFolderRows="2"
launcher:numFolderColumns="3"
launcher:minAllAppsPredictionColumns="3"
launcher:iconSize="48"
launcher:iconTextSize="13.0"
launcher:numHotseatIcons="3"
launcher:hotseatIconSize="48"
launcher:defaultLayoutId="@xml/default_workspace_3x3"
/>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值