使用具有超过三种颜色设置的渐变Drawable

20

据我所读,您可以使用gradientDrawable,并为其设置三种颜色,例如:

<gradient startColor="#00FF00" centerColor="#FFFF00" endColor="#FFFFFF"/>

但如果我想要超过三种颜色,而且不仅如此,我还想能够设置每种颜色的位置(按权重/百分比)怎么办?

使用API是否有可能实现这一点,还是我需要自己制作定制的drawable?如果需要制作自己的定制drawable,应该如何做?


我认为你可以在这里找到答案:https://dev59.com/xW855IYBdhLWcg3wYTH_。 - Androidz
5个回答

26

将此代码放入您的onCreate()方法中:

ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
    @Override
    public Shader resize(int width, int height) {
        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);
         return linearGradient;
    }
};
PaintDrawable paint = new PaintDrawable();
paint.setShape(new RectShape());
paint.setShaderFactory(shaderFactory);

使用这个可绘制对象作为背景。

你也可以通过创建图层在XML中添加三种以上的颜色,但是在XML中操作比较复杂。


比如你需要设置ActionBar的背景: getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); getActionBar().setBackgroundDrawable(paint); 或者如果你有一个按钮: //在xml文件中定义一个id为button的按钮 Button button = (Button)findViewById(R.id.button); button.setBackgroundDrawable((Drawable)paint); - Bhaskar
你可以在画图软件中绘制渐变色,并将其作为背景图像使用。 - Bhaskar
1
请尝试展示一个样例。当我测试你的代码时,我得到了一个对角线渐变。 - android developer
2
这个答案非常好。如果想要从上到下的渐变,请将渐变的创建替换为以下内容:“LinearGradient(0, 0, 0, height,…”(因此,将第二个渐变点替换为0|height而不是width|height) - sunadorer
@sunadorer,现在看起来运作良好。谢谢。我甚至可以设置整个彩虹颜色:new int[] {0xFFff0000,0xFFFFA500,0xFFFFFF00,0xFF00FF00,0xFF0000FF,0xFF000099,0xFF8F00FF},new float[] {0,0.16f,0.33f,0.5f,0.66f,0.83f,1},Shader.TileMode.REPEAT); - android developer
显示剩余9条评论

8

不可能直接将其转换为xml文件,但您可以使用GradientDrawable类在Java代码中应用+3种颜色渐变:

GradientDrawable gradientDrawable = new GradientDrawable(
                Orientation.TOP_BOTTOM,
                new int[]{ContextCompat.getColor(this, R.color.color1),
                        ContextCompat.getColor(this, R.color.color2),
                        ContextCompat.getColor(this, R.color.color3),
                        ContextCompat.getColor(this, R.color.color4)});

        findViewById(R.id.background).setBackground(gradientDrawable);

这也是一个不错的解决方案,而且很简短。 - android developer
1
但是如何设置颜色的位置? - Aaron Lee
数组颜色位置对应于渐变中的颜色位置。请注意您设置的方向。 - Pelanes

3
使用GradientDrawable,我们可以实现这个功能。
GradientDrawable gradientInsta = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                new int[] {
                        Color.parseColor("#F9ED32"),
                        Color.parseColor("#F6C83F"),
                        Color.parseColor("#F2735F"), 
                        Color.parseColor("#EF3E73"),
                        Color.parseColor("#EE2A7B"),
                        Color.parseColor("#D22A8A"),
                        Color.parseColor("#8B2AB1"),
                        Color.parseColor("#1C2AEF"),
                        Color.parseColor("#002AFF"),
                        ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)
        });
findViewById(R.id.insta).setBackground(gradientInsta);

1
我认为这是我的浏览器错误,将“转换为引用字符串,现在它应该可以正常工作了。 - sai Pavan Kumar

1

我认为以下是可能的解决方案。

  • 您可以创建具有渐变的多个形状并形成更大的形状。
  • 您可以通过扩展GradientDrawable类来创建自己的GradientDrawable,请参阅下面的文档。

  • Gradient Drawable文档


你能展示一下如何完成第二个吗? - android developer

0

Kotlin中可以这样做:

将color1、2、..n替换为您的颜色值

                 //Create Gradient 
                val gradientDrawable = GradientDrawable(
                    GradientDrawable.Orientation.TOP_BOTTOM,
                    intArrayOf(color1,color1 ,color1, colorn)
                );
                gradientDrawable.cornerRadius = 0f;

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