2

我做的:

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

但是当我将它设置为视图中的背景时,我什么也看不到。

4

1 回答 1

6

尝试将此drawable设置为您的视图背景

GradientDrawable rainbow = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
            new int[] {Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED});
于 2015-10-07T11:14:32.583 回答