Android:如何制作具有多色渐变的可绘制对象? [英] Android: How to make a drawable with a multicolor gradient?

查看:121
本文介绍了Android:如何制作具有多色渐变的可绘制对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做到了:

  ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            LinearGradient linearGradient = new LinearGradient(0, 0, width, height,
                    new int[]{
                            0xF44336,
                            0xFFB74D,
                            0xFFE082,
                            0xAED581,
                            0x4CAF50,
                            0xAED581,
                            0xFFE082,
                            0xFFB74D,
                            0xF44336},
                    new float[]{
                            0, 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.75f, 0.875f, 1.0f},
                    Shader.TileMode.REPEAT);
            return linearGradient;
        }
    };
    PaintDrawable paint = new PaintDrawable();
    paint.setShape(new RectShape());
    paint.setShaderFactory(shaderFactory);

但是当我在视图中将其设置为背景时,我什么都看不到.

But I can't see anything when I set it as background in a view.

推荐答案

尝试将此可绘制对象设置为您的视图背景

try setting this drawable to your view background

GradientDrawable rainbow = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
            new int[] {Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED});

这篇关于Android:如何制作具有多色渐变的可绘制对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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