Using a gradientDrawable with more than three colors set

前端 未结 5 924
孤独总比滥情好
孤独总比滥情好 2020-12-29 06:40

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



        
相关标签:
5条回答
  • 2020-12-29 07:15

    I think the below are possible solutions.

    • You can create multiple shapes with gradients and form a bigger shape.
    • You can create your own GradientDrawable by extending the GradientDrawable Class refer to the below doc.

    • Gradient Drawable Documentation

    0 讨论(0)
  • 2020-12-29 07:18

    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.

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

    0 讨论(0)
  • 2020-12-29 07:21

    It is not possible to do into a xml file, but you can apply +3 color gradient in yout java code with GradientDrawable class:

    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);
    
    0 讨论(0)
  • 2020-12-29 07:23

    In Kotlin you can do it like this:

    replace color1,2,..n with your color values

                     //Create Gradient 
                    val gradientDrawable = GradientDrawable(
                        GradientDrawable.Orientation.TOP_BOTTOM,
                        intArrayOf(color1,color1 ,color1, colorn)
                    );
                    gradientDrawable.cornerRadius = 0f;
    
    0 讨论(0)
  • 2020-12-29 07:25

    Using GradientDrawble we can achieve this

    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);
    
    0 讨论(0)
提交回复
热议问题