Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a gradientDrawable with more than three colors set

Tags:

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

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?

like image 543
android developer Avatar asked Dec 24 '12 11:12

android developer


People also ask

How do you color a gradient in XML?

To create a gradient color we need to create a . xml file in the drawable folder. So go to app -> res -> drawable and right-click on drawable -> New -> Drawable Resource File and create gradient_drawable. xml file.

What is gradient drawable?

Designers are trying out different styles and combinations which go best with the Android App. One of the key components of Android being used these days is called GradientDrawable. A GradientDrawable is drawable with a color gradient for buttons, backgrounds, etc.

How to use gradients in Android with gradientdrawable?

One of the key components of Android being used these days is called GradientDrawable. Here #00FFFF is the color code for Aqua color. For this you need to use gradients. 1. Using a drawable resource in XML Here we have given a <gradient /> tag and added the three color codes - startColor, centerColor and endColor.

What is a gradient in art?

The colors produced by a gradient vary continuously with the position, producing smooth color transitions. A color gradient is also known as a color ramp or a color progression. In assigning colors to a set of values, a gradient is a continuous colormap, a type of color scheme.

How to create gradient using a drawable resource in XML?

Using a drawable resource in XML Here we have given a <gradient /> tag and added the three color codes - startColor, centerColor and endColor. We have also given the angle as 0 which denotes LEFT-RIGHT orientation. Note: Angles can only be multiples of 45. Also, max 3 colors can be specified in XML - startColor, centerColor and endColor.

What is the color code for Aqua in Android for gradients?

One of the key components of Android being used these days is called GradientDrawable. Here #00FFFF is the color code for Aqua color. For this you need to use gradients. 1. Using a drawable resource in XML


1 Answers

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.

like image 91
Bhaskar Avatar answered Oct 05 '22 17:10

Bhaskar