Android: How to make a drawable with a multicolor gradient?

冷暖自知 提交于 2019-12-12 10:09:49

问题


I made this:

  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.


回答1:


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


来源:https://stackoverflow.com/questions/32990598/android-how-to-make-a-drawable-with-a-multicolor-gradient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!