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?
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.
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With