gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:41:30 25 4
gpt4 key购买 nike

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com