使用带有超过三种颜色设置的gradientDrawable [英] Using a gradientDrawable with more than three colors set

查看:123
本文介绍了使用带有超过三种颜色设置的gradientDrawable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我阅读的内容,您可以使用 gradientDrawable 并为其设置了三种颜色,例如:

According to what I've read, you can use a gradientDrawable and have three colors set for it, for example:

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

但是,如果我想要三种以上的颜色,不仅如此,我还希望能够设置每种颜色的放置位置(以重量/百分比计)?

But what if I want more than three colors, and not only that, I want to be able to set where to put each (in weight/percentage)?

是否可以使用API​​,还是应该制作自己的自定义可绘制对象?如果我需要制作自己的自定义可绘制对象,应该怎么做?

Is it possible using the API or should I make my own customized drawable? If I need to make my own customized drawable, how should I do it?

推荐答案

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

put this code in your onCreate() method:

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

,并使用此可绘制对象作为背景.

and use this drawable as a background.

您还可以通过创建图层在xml中添加三种以上的颜色.但是在XML中,它非常复杂.

You can add more than three colors in xml also by creating layers. But in XML it is quite complicated.

这篇关于使用带有超过三种颜色设置的gradientDrawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆