Detailed explanation of custom gradient color effect by Android programming

This article describes the example of Android programming to achieve custom gradient color effect. Share with you for your reference, as follows:

Have you hated the solid background? Well, Android provides programmers with an interface to customize gradient colors. Let's dazzle our interface.

XML defines the gradient color

First, you write an XML in the drawable directory. The code is as follows

The shape node is configured in the form of graphics, mainly including square, circle, etc. the upper code is square. The gradient node is mainly configured with the start color, end color and the color, coordinates and gradient effect of the middle point (0, 90180 gradient from left to right, 270 gradient from top to bottom) By default, the padding node from left to right is mainly configured with the spacing from top to bottom, left and right, and the corners node is configured with the radius of the round foot

Then, you can use it freely in code or XML layout.

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

Code definition gradient color

Achieve gradient effect on Android platform. In android.graphics, we can find classes related to gradient, such as linear gradient, radial gradient and angular gradient sweepgradient. Their base class is android.graphics.shader. In order to show the effect, use a simple example to illustrate.

1、 LinearGradient linear gradient

The Android platform provides two overloaded methods to instantiate this class. The difference is that the first method in the parameters can use the color array and position to achieve a more delicate transition effect. For example, if 20 colors are stored in the color sampling int [] colors array, 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.

Examples are as follows:

Parameter 1 is the coordinate x position of the initial point of the gradient, parameter 2 is the y-axis position, parameters 3 and 4 correspond to the gradient end point, and the last parameter is the tiling mode, which is set as mirror here

Android development network has just mentioned that gradient is based on shader class, so we set this gradient through paint's setshader method. The code is as follows:

2、 RadialGradient mirror gradient

With the above foundation, let's learn about the radial gradient. The only difference from the above parameters is that the third parameter of radial gradient is radius, and the others are the same as linear gradient.

3、 Sweepgradient angle gradient

For some 3D stereo effect 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 through the loaded colors.

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, android123 recommends the following overload method, which is generally null.

Or create a drawable directly:

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android layout skills, summary of Android view skills, summary of activity operation skills of Android programming, summary of Android operation JSON format data skills, summary of Android resource operation skills, and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>