Android Studio中图标颜色如何渐变

5

我希望我的图标具有渐变颜色。我找到了如何制作渐变背景的方法,但这对我的情况没有帮助。

这是我的图标代码:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:strokeColor="#000"
        android:fillColor="#000"
        android:pathData="M12,12m-3.2,0a3.2,3.2 0,1 1,6.4 0a3.2,3.2 0,1 1,-6.4 0"/>
    <path
        android:fillColor="#FF000000"
        android:pathData="M9,2L7.17,4L4,4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2h-3.17L15,2L9,2zM12,17c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5z"/>
</vector>

到目前为止,我已经做了以下工作来获得渐变文本:

mPicGallery = (Button) getView().findViewById(R.id.get_picture_gallery);
Shader textShader_8 = new LinearGradient(200, 20, 50, 10, new int[]{Color.parseColor("#01579b"),Color.parseColor("#006064")}, new float[]{0, 1}, Shader.TileMode.CLAMP);
mPicGallery.getPaint().setShader(textShader_8);

这很不规范,但我没有找到更好的方法。

所以我的问题是:如何用渐变色制作图标。

以下是我想要的示例:

Example of what I mean


你尝试过使用可绘制对象创建渐变并将其应用于路径填充颜色吗? - Estevex
1个回答

11

尝试在VectorDrawable中按以下方式执行

<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">

<path
    android:fillType="evenOdd"
    android:pathData="M12,12m-3.2,0a3.2,3.2 0,1 1,6.4 0a3.2,3.2 0,1 1,-6.4 0">
    <aapt:attr name="android:fillColor">
        <gradient
            android:gradientRadius="22"
            android:type="radial">
            <item
                android:color="#000000"
                android:offset="0.0" />
            <item
                android:color="#FFFFFFFF"
                android:offset="1.0" />
        </gradient>
    </aapt:attr>
</path>
<path
    android:fillColor="#FF000000"
    android:pathData="M9,2L7.17,4L4,4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2h-3.17L15,2L9,2zM12,17c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5z" />

您可以在文档中找到更多详细信息。

https://developer.android.com/reference/android/graphics/drawable/GradientDrawable

如果您向谷歌大叔询问,可能会找到更多教程 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接