Android 9.0 修改默认自适应图标形状(Adaptive icon)

一、我们可以在Launcher3中找到默认图标形状的配置,可以看到默认配置的几个图标形状,这个地方并不能修改默认图标的形状,因为icon_shape_system_default对应的值为空。

    <!-- Values for icon shape overrides. These should correspond to entries defined
     in icon_shape_override_paths_names -->
    <string-array translatable="false" name="icon_shape_override_paths_values">
        <item></item>
        <item>M50,0L100,0 100,100 0,100 0,0z</item>
        <item>M50,0 C10,0 0,10 0,50 0,90 10,100 50,100 90,100 100,90 100,50 100,10 90,0 50,0 Z</item>
        <item>M50 0A50 50,0,1,1,50 100A50 50,0,1,1,50 0</item>
        <item>M50,0A50,50,0,0 1 100,50 L100,85 A15,15,0,0 1 85,100 L50,100 A50,50,0,0 1 50,0z</item>
    </string-array>

    <string-array translatable="false" name="icon_shape_override_paths_names">
        <!-- Option to not change the icon shape on home screen. [CHAR LIMIT=50] -->
        <item>@string/icon_shape_system_default</item>
        <item>@string/icon_shape_square</item>
        <item>@string/icon_shape_squircle</item>
        <item>@string/icon_shape_circle</item>
        <item>@string/icon_shape_teardrop</item>
    </string-array>

二、从绘制Adaptive icon的地方入手,找到AdaptiveIconDrawable.java,路径如下:

./base/graphics/java/android/graphics/drawable/AdaptiveIconDrawable.java

查看其构造方法:

/**
     * The one constructor to rule them all. This is called by all public
     * constructors to set the state and initialize local properties.
     */
    AdaptiveIconDrawable(@Nullable LayerState state, @Nullable Resources res) {
        mLayerState = createConstantState(state, res);

        if (sMask == null) {
            sMask = PathParser.createPathFromPathData(
                Resources.getSystem().getString(R.string.config_icon_mask));
        }
        mMask = PathParser.createPathFromPathData(
            Resources.getSystem().getString(R.string.config_icon_mask));
        mMaskMatrix = new Matrix();
        mCanvas = new Canvas();
        mTransparentRegion = new Region();
    }

可以看到用到了config_icon_mask 字段,该字段是在framework中配置的,大概意思就是通过config_icon_mask来获取对应的icon shape.

三、查看android/frameworks/base/core/res/res/values/config.xml 中的config_icon_mask值

<!-- Specifies the path that is used by AdaptiveIconDrawable class to crop launcher icons. -->
    <string name="config_icon_mask" translatable="false">"M50,0L92,0C96.42,0 100,4.58 100 8L100,92C100, 96.42 96.42 100 92 100L8 100C4.58, 100 0 96.42 0 92L0 8 C 0 4.42 4.42 0 8 0L50 0Z"</string>

可以知道该值就是默认的adaptive icon的形状,我们把其值改为我们想要的值即可,值可以参考,launcher3中的配置,比如我们想改为正方形,就改为M50,0L100,0 100,100 0,100 0,0z 即可。

支持,android系统里面所有使用了adaptive icon 特性功能的,都会遵循这个规则。

发布了10 篇原创文章 · 获赞 0 · 访问量 2197

猜你喜欢

转载自blog.csdn.net/h1217256980/article/details/103201231
今日推荐