Android - 动态设置可绘制对象的渐变

6

我有一个来自XML资源的可绘制对象,我想使用该可绘制对象,但动态设置渐变颜色。目前我有以下代码:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="3dip">
    </corners>
    <gradient
        android:angle="90"
        android:type="linear"
        android:startColor="#FFFFFFFF"
        android:centerColor="#FFFF0000"
        android:endColor="#FFFF0000">
    </gradient>
</shape>

我认为我可以在运行时获取Drawable并将其转换为GradientDrawable,然后使用方法来动态设置颜色,从而实现颜色的动态变化。但是,GradientDrawable没有这样的方法,只能在构造函数中设置颜色。我觉得这很奇怪,因为渐变的所有其他方面都可以设置。是否有比覆盖onDraw()并手动绘制渐变更容易的方法?我正在尝试使用的一些类文档非常不好。

4个回答

1

资源主要是静态的,通常不允许修改。某些资源类型允许您“克隆”可变副本。GradientDrawable仅允许您在构造函数中设置颜色(正如您发现的那样),因此如果您想在运行时动态控制颜色,或者更好地从资源中选择固定数量的背景之一,则需要在内部创建这些颜色。 如前所述,使用setBackgroundDrawable()在运行时安装您的背景即可。 没有必要发表评论,只需完成任务!


请注意,自API级别16起,GradientDrawable现在支持setColors方法。详情请参考:http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#setColors(int[]) - Scott Leslie

1
创建一个类名为GradientDrawable的类,代码如下:
public class RoundedDrawable extends GradientDrawable {

        public RoundedDrawable(int shape, int solidColor, int strokeWidth,
     int strokeColor, float[] fourRadii) {

            this.mutate();
            this.setShape(shape);
            this.setColor(solidColor);
            this.setStroke(strokeWidth, strokeColor);
            this.setCornerRadii(fourRadii);
        }
    }

现在像这样在你的Activity中使用它:

public class AAActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_transaction_layout);

    RoundedDrawable customBg;

    RelativeLayout relList = (RelativeLayout) findViewById(R.id.relList);
    float radii[]={5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f};
    customBg = new RoundedDrawable(GradientDrawable.RECTANGLE,Color.parseColor("#FFFFFF"),
            2, Color.parseColor("#8C8C8C"),radii);
    relList.setBackgroundDrawable(customBg);

    LinearLayout linearItemsRow = (LinearLayout) findViewById(R.id.linearItemsRow);
    float[] rowRadii={5.0f, 5.0f, 5.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f};
    customBg = new RoundedDrawable(GradientDrawable.RECTANGLE,Color.parseColor("#CBCBCB"),
            0, 0, rowRadii);
    linearItemsRow.setBackgroundDrawable(customBg);


}

}

希望这能有所帮助。

0

你可以动态地将一个可绘制对象设置为视图的背景。

view.setBackgroundDrawable(R.drawable.your_drawable_id);


0

final class MyGradientDrawable extends GradientDrawable {
        MyGradientDrawable(int fromColor, int toColor) {
            super(Orientation.BOTTOM_TOP, new int[]{fromColor, toColor});

            setCornerRadius(0);
            setGradientType(LINEAR_GRADIENT);
            setGradientRadius(90);
        }
    }

使用方法

  final int firstColor = ContextCompat.getColor(requireContext(), R.color.my_first_color);
  final int secondColor = ContextCompat.getColor(requireContext(), R.color.my_second_color);
  final MyGradientDrawable myGradBg = new MyGradientDrawable(firstColor, secondColor);

  myView.setBackground(myGradBg)

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