android - 在图层列表中更改形状渐变起始颜色

标签 android android-drawable shapes

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_enabled="false"
    android:drawable="@drawable/shapecopmainbutton" />
<item
    android:state_pressed="true"
    android:state_enabled="true"
    android:drawable="@drawable/shapecopmainbuttonpress" />
<item
    android:state_focused="true"
    android:state_enabled="true"
    android:drawable="@drawable/shapecopmainbutton" />
<item
    android:state_enabled="true"
    android:drawable="@drawable/shapecopmainbutton" />
</selector>

我的shapecopmainbutton.xlm如下。

<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>

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

这是我目前的代码:

已编辑。

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);

我在这里被难住了,因为我无法弄清楚如何设置“环”开始/停止颜色渐变属性。

谢谢。

最佳答案

您可以使用 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);

关于android - 在图层列表中更改形状渐变起始颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51803490/

相关文章:

android - 支持所有屏幕尺寸和安卓版本

java - 为什么我会收到空​​指针异常?

android - AsyncTasks 有问题吗?

android - 是否可以将计算机的处理能力共享给 Android 设备?

不同显示器和屏幕密度的 Android 图标

android - 如何在 android 中以编程方式更改低于 api 级别 23 的按钮的可绘制色调

java - 如何使用文本字段相应地调整矩形的大小?

c++ - C++ 中的 2D 形状匹配

android - 是什么导致此 PNG 渲染不佳?

javascript - 从一个元素到另一个元素绘制一条线