【问题标题】:Changing the background color of a vector app icon更改矢量应用程序图标的背景颜色
【发布时间】:2018-10-27 21:11:38
【问题描述】:

我正在尝试在 android 上创建一个小助手以获得乐趣,我想使用与 google 助手相同的图标,但背景为深色,但它不起作用: 我尝试使用具有形状(圆形或正方形)的图层列表,但它并不完全适合圆形图标。 我也试过了:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/midnightblue"/>
    <item android:drawable="@drawable/ic_assistant"/>
</layer-list>

但正如您在屏幕截图中看到的那样,我得到了相同的结果。你有什么想法吗?

【问题讨论】:

    标签: java android vector


    【解决方案1】:

    尝试将启动器图标背景设置为白色。比如res/mipmap-anydpi-v26目录:

    ic_launcher.xml

    <?xml version="1.0" encoding="utf-8"?>
    <adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
        <background android:drawable="@drawable/ic_launcher_background" />
        <foreground android:drawable="@drawable/ic_launcher" />
    </adaptive-icon>
    

    ic_launcher_round.xml

    <?xml version="1.0" encoding="utf-8"?>
    <adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
        <background android:drawable="@drawable/ic_launcher_background" />
        <foreground android:drawable="@drawable/ic_launcher_round" />
    </adaptive-icon>
    

    res/drawable目录下:

    ic_launcher_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="108dp"
        android:height="108dp"
        android:viewportHeight="108"
        android:viewportWidth="108">
        <path
            android:fillColor="#FFFFFF"
            android:pathData="M0,0h108v108h-108z" />
    </vector>
    

    毕竟,我们可以缩放内部图标以强制它包含在圆圈中。

    ic_assistant.xml

    <?xml version="1.0" encoding="utf-8"?>
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="48dp"
        android:height="48dp"
        android:viewportWidth="512"
        android:viewportHeight="512">
    
        <group
            android:scaleX="0.5"
            android:scaleY="0.5"
            android:translateX="128"
            android:translateY="128">
    
            <path
                android:fillColor="#4285F4"
                android:pathData="M156.268,167.705m-156.268,0a156.268,156.268 0,1 1,312.536 0a156.268,156.268 0,1 1,-312.536 0" />
            <path
                android:fillColor="#34A853"
                android:pathData="M512,182.95c0,17.544 -14.224,31.762 -31.762,31.762s-31.762,-14.218 -31.762,-31.762c0,-17.543 14.224,-31.762 31.762,-31.762S512,165.407 512,182.95z" />
            <path
                android:fillColor="#EA4335"
                android:pathData="M454.829,260.449c0,35.081 -28.438,63.522 -63.523,63.522c-35.088,0 -63.524,-28.441 -63.524,-63.522c0,-35.083 28.437,-63.524 63.524,-63.524C426.392,196.925 454.829,225.367 454.829,260.449z" />
            <path
                android:fillColor="#FBBC05"
                android:pathData="M467.533,424.339c0,42.1 -34.124,76.225 -76.228,76.225c-42.104,0 -76.229,-34.125 -76.229,-76.225c0,-42.098 34.124,-76.227 76.229,-76.227C433.409,348.112 467.533,382.241 467.533,424.339z" />
    
        </group>
    
    </vector>
    

    【讨论】:

    • 这是我为 ic_launcher_round.xml 所做的:hastebin.com/uxelinejoc.xml 但我没有得到我想要的:zupimages.net/up/18/43/cp1f.png
    • 如果ic_assistant 代码是矢量可绘制的,可以给我看吗?
    • 我已经更新了答案。如果需要,您可以更改组标签中的比例和平移。最初我将它的比例设置为 0.5。
    • 不客气。如果你想在一个圆圈中精确地包含一个方形图标,你应该将它缩放大约 0.7 (sqrt(2) / 2)
    猜你喜欢
    • 2021-03-31
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    相关资源
    最近更新 更多