Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize gradient color effect in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to customize the gradient color effect in Android". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to customize the gradient color effect in Android".

Xml defines a gradient color

First, you write a xml in the drawable directory with the following code

Shape nodes are configured in the form of graphics, mainly including squares, circles, etc., and the above code is square

The gradient node mainly configures the color of the start point, the color of the end point and the color, coordinates and gradient effects of the middle point (0pc90180 gradient from left to right, 270 gradient from top to bottom) from left to right by default

Padding nodes are mainly configured with upper and lower left and right spacing.

Corners node configures the radius of the surrounding feet

Then, you can feel free to use it in your code or in your xml layout.

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

Code defines gradient color

Realize the gradual change effect under the Android platform. In android.graphics, we can find classes about the word Gradient, such as LinearGradient linear gradient, RadialGradient radial gradient and angle gradient SweepGradient. Their base class is android.graphics.Shader. To show the effect, use a simple example to illustrate.

First, LinearGradient linear gradient

In the android platform provides two overloaded ways to instantiate this class respectively, their difference is that the first method in the parameters can use a color array, and position to achieve a more delicate transition effect, such as color sampling int [] colors array to store 20 colors, then the gradient will be dealt with 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, Shader.TileMode tile) LinearGradient (float x0, float y0, float x1, float y1, int color0, int color1, Shader.TileMode tile)

Examples of use are as follows:

Paint p=new Paint (); LinearGradient lg=new LinearGradient (0pc0pr 100pr 100pr. REDrect Color.blue) Shader. TileMode. Mirror)

The first parameter is the starting point coordinate x position of the gradient, the second parameter is the y-axis position, the third and fourth resolution correspond to the gradient end point, and the last parameter is the tiling mode, which is set as the mirror image.

Just now the Android development network has mentioned that Gradient is based on the Shader class, so we set this gradient through the setShader method of Paint. The code is as follows:

P.setShader (lg); canvas.drawCicle (0rec 0j 200jp); / / Parameter 3 is the radius of the circle drawn, and the type is float.

2. RadialGradient Mirror gradient

With the above foundation, let's look at the next radial gradient. The only difference from the above parameters is that the third parameter of the radial gradient is the radius, and the other parameters are the same as the linear gradient.

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

3. Gradual change of SweepGradient angle

For some 3D stereo effects, you can try to use an angular gradient to complete a cone, which is relatively simpler than above, with the first two parameters as the center, and then render the gradient evenly through the loaded colors.

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

The description on the last parameter, 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 using the following overloading method, which is generally NULL.

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

Or create a drawable directly:

Public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); requestWindowFeature (Window.FEATURE_NO_TITLE); / / set no title getWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, / / full-screen WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView (R.layout.login) / / login interface GradientDrawable grad = new GradientDrawable (/ / gradient Orientation.TOP_BOTTOM, new int [] {Color.BLACK, Color.WHITE}); getWindow () .setBackgroundDrawable (grad) / / set gradient color} Thank you for your reading, the above is the content of "how to customize gradient color effect in Android". After the study of this article, I believe you have a deeper understanding of how to customize gradient color effect in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report