在图层列表中更改形状渐变开始颜色

时间:2018-08-11 20:27:38

标签: android android-drawable shape

我使用选择器列表选择按钮背景。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <size
                android:width="@dimen/layoutwidthMaincopbutton"
                android:height="@dimen/HeightMaincopbutton" />
            <solid android:color="@color/black"/>
        </shape>
    </item>
    <item
        android:id="@+id/gradientDrawble"
        android:left="4dp"
        android:right="1dp"
        android:top="0dp"
        android:bottom="5.5dp"
        >
        <shape
            android:id="@+id/gradientDrawbles"
            android:shape="ring"
            android:thickness="@dimen/thicknessMaincopbutton"
            android:innerRadius="@dimen/innerRadiusMaincopbutton"
            android:useLevel="false">
            <solid android:color="@color/GradientEnd" />

            <gradient
                android:id="@+id/gradientDrawbleg"
                android:type="radial"
                android:gradientRadius="30%p"
                android:startColor="@color/GradientCenter"
                android:endColor="@color/GradientEnd"
                android:centerX="0.2"
                android:centerY="0.2"
                />
        </shape>
    </item>

我的shapecopmainbutton.xlm如下。

StateListDrawable stateListDrawable=(StateListDrawable) ContextCompat.getDrawable(context,R
                    .drawable.copbuttonmaineffect);
            LayerDrawable layerDrawable = (LayerDrawable) stateListDrawable.getCurrent();
            int colors[] = { R.color.GradientStartl, R.color.GradientEndl };
            GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.
                    gradientDrawble);
            gradientDrawable.setColors(colors);

我想更改环形渐变属性的开始和结束颜色。

这是我到目前为止在代码中所拥有的:

已编辑。

{{1}}

我在这里很困惑,因为我不知道如何设置“环”的开始/停止颜色渐变属性。

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以使用GradientDrawable的setColors函数添加开始和结束颜色。

    LayerDrawable layerDrawable = (LayerDrawable) ContextCompat.getDrawable(this, 
        .drawable.shapecopmainbutton);
    //you can choose any color of the below. I have chosen these for purpose of example.
    //first parameter is the start color and second parameter is the end color.
    int colors[] = { 0xff255779, 0xffa6c0cd };
    GradientDrawable gradientDrawable = (GradientDrawable) 
         layerDrawable.findDrawableByLayerId(R.id.gradientDrawable);
    gradientDrawable.setColors(colors);