将 css 渐变转换为 android 渐变 (xml)

Convert css gradient in to android gradient (xml)

我想将 css 渐变转换为 android xml 渐变(形状)文件

例如

background-image: linear-gradient(to right, #ff8177 0%, #ff867a 0%, #ff8c7f 21%, #f99185 52%, #cf556c 78%, #b12a5b 100%);

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="0"
        android:endColor="#b12a5b"
        android:startColor="#ff867a"
        android:type="linear" />
</shape>

我对css了解不多。我只知道 android 颜色只有三个参数 android:endColor, android:startColor, android:centerColor 如何定义这个 % 和 css 中呈现的各种颜色

是否有任何在线工具可以让我通过提供 css 输入来生成 xml 文件。

我以前用它在背景中绘制渐变


        View    layout  =   findViewById(R.id.mainLayout);

        GradientDrawable    gradientDrawable    =   new GradientDrawable(
                GradientDrawable.Orientation.TOP_BOTTOM,
                new int[]   {0xff45c0cc,0xff0054a6}
        );
        gradientDrawable.setCornerRadius(0f);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            layout.setBackground(gradientDrawable);
        }

我从给定的 css 中了解到的是#f99185-52% 表示 52% 的透明度,在我给定的代码示例中 int[] 有一个十六进制代码,你可以做的是使用 6 位 GRBcode并附加相当于 here 的 52% 不透明度,并附加给定的 RGBcode

例如 #f99185 52% 将变为 #f9918585 因为 52% 在十六进制中是 85

我建议查看这个相关问题:Using a gradientDrawable with more than three colors set

使用 LinearGradient 对象,您可以使用多种颜色和百分比停止定义渐变。

例如(借用我链接的问题):

LinearGradient linearGradient = new LinearGradient(0, 0, width, height,
        new int[] { 
            0xFF1e5799, 
            0xFF207cca, 
            0xFF2989d8, 
            0xFF207cca }, //substitute the correct colors for these
        new float[] {
            0, 0.40f, 0.60f, 1 },
        Shader.TileMode.REPEAT);

我链接的问题和 official documentation 包含有关如何使用它的更多详细信息。