SoFunction
Updated on 2025-03-02

Android programming to implement custom gradient color effects detailed explanation

This article describes the Android programming to implement custom gradient color effects. Share it for your reference, as follows:

Have you already disliked the solid color background? OK, Android provides programmers with interfaces to customize gradient colors to show off our interface.

xml defines gradient color

First, you write an xml in the drawable directory, the code is as follows

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:andro
  android:shape="rectangle" >
  <gradient
    android:angle="270"
    android:endColor="#000000"
    android:startColor="#ffffff" />
  <corners
    android:bottomLeftRadius="5dip"
    android:bottomRightRadius="5dip"
    android:topLeftRadius="5dip"
    android:topRightRadius="5dip" />
</shape>

shape The node is configured in the form of a graph, mainly including square, circle, etc. The above code is square,
gradient The node mainly configures the starting point color, end point color and the color, coordinates, and gradient effects of the middle point (0, 90, 180 gradient from left to right, 270 gradient from top to bottom) from left to right by default.
padding The nodes are mainly configured with up, down, left and right spacing
corners The radius of the surrounding footsteps is configured

Then you can use it in your code or in your XML layout as you like.

With such a simple configuration, as long as you know the rgb value of the color, you can become a color expert.

Code defines gradient colors

Gradient effect is achieved under the Android platform. In this we can find classes related to the word Gradient, such as LinearGradient linear gradient, RadialGradient radial gradient and angle gradient SweepGradient, their base class is. To show the effect, use a simple example to illustrate.

1. LinearGradient linear gradient

There are two overloading methods for instantiating the class in the Android platform. The difference is that the first method among the parameters can use color arrays and positions to achieve a more delicate transition effect. For example, the color sampling int[] colors array stores 20 colors, and the gradient will be processed one by one. The parameters of the second method are only the initial color color0 and the final color color1.

LinearGradient(float x0, float y0, float x1, float y1, int[] colors, float[] positions,  tile)
LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1,  tile)

Examples of use are as follows:

Paint p=new Paint();
LinearGradient lg=new LinearGradient(0,0,100,100,,,);

Parameter 1 is the x position of the initial point coordinate of the gradient, parameter 2 is the y-axis position, parameters 3 and 4 are resolved to correspond to the end point of the gradient, and the final parameter is tiled, which is set to mirror here.

The Android Development Network has just mentioned that Gradient is based on the Shader class, so we set this gradient through Paint's setShader method. The code is as follows:

(lg);
(0,0,200,p); // Parameter 3 is the radius of the circle, and the type is float type.

2. RadialGradient mirror gradient

With the above foundation, let’s take a look at radial gradients together. The only difference from the above parameters is that the third parameter of radial gradient is the radius, and the others are the same as linear gradient.

RadialGradient(float x, float y, float radius, int[] colors, float[] positions,  tile)
RadialGradient(float x, float y, float radius, int color0, int color1,  tile)

3. SweepGradient angle gradient

For some 3D stereo gradients, you can try to use angle gradients to complete a cone, which is relatively simpler than the above. The first two parameters are the center point, and then the gradient rendering is averaged by the loaded colors.

SweepGradient(float cx, float cy, int[] colors, float[] positions)

For the last parameter, the description on the SDK is:

May be NULL. The relative position of each corresponding color in the colors array, beginning with 0 and ending with 1.0. If the values are not monotonic, the drawing may produce unexpected results. If positions is NULL, then the colors are automatically spaced evenly.

Therefore, Android 123 recommends using the following overload method, which is generally NULL.

SweepGradient(float cx, float cy, int color0, int color1)

Or create a drawable directly:

public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE); //Set no title    getWindow().setFlags(.FLAG_FULLSCREEN , //full screen           .FLAG_FULLSCREEN);
    setContentView();//Login interface    GradientDrawable grad = new GradientDrawable(//Gradial color      Orientation.TOP_BOTTOM,
      new int[]{, }
    );
    getWindow().setBackgroundDrawable(grad);//Set gradient color}

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android layout layout tips summary》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Summary of Android data skills for operating json format》、《Android resource operation skills summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.