Android is a simple way to implement a color gradient ProgressBar

Today, I watched a tutorial and saw a progress bar with color gradient. I thought it was a little interesting, so I recorded it. The following is a rendering

ProgressBar with color gradient

Seeing the rendering may give people a feeling that high-end technology is used. In fact, this is not so profound. We can simply change the style of ProgressBar. Let's talk about the implementation method.

First, let's briefly analyze:

1. The above style only realizes color gradient, but its rotation and presentation method is still a circular ProgressBar.

2. This ProgressBar implements color gradient. We need gradient. This is also relatively simple. As long as we configure the start, middle and end colors, we can realize it. After understanding the above two points, we will start writing code.

First, we implement the above layout. The background is gray. A ProgressBar is centered and a textview is located below the progressbar. The code is as follows:

All other codes above are well understood. Only ProgressBar has an indeterminate sign that needs to be explained:

The general ProgressBar is used to display the loading progress. If we have reached the current specific progress, this property should be set to true and set the correct progress. If we do not know the correct progress, it should be set to false.

After setting the layout, the next step is to set the gradient style of ProgressBar. Here we need to customize a drawable.

The custom drawable code is as follows:

The above code is explained below:

The rotate of the outer layer indicates that this is a rotation animation, and the specifies the start angle and end angle, and the rotation center is the center of the circle

The shape of the inner layer defines the shape as a ring, which has three attributes:

<1> Innerradiusratio is the ratio of the outer ring radius to the inner diameter. For example, if the outer ring radius is 30 and the inner ring radius is 10, the ratio is 3

<2> Thicknessratio is the ratio of the radius of the outer ring to the thickness of the ring

<3> If uselevel is true, it can be used in levellistdrawable

The next gradient defines the gradient effect, specifies the color of the beginning and end, and also specifies the gradient method as scanning gradient

In the last step, we set the style we defined above through a ProgressBar attribute:

After the above steps, we have realized a simple gradient ProgressBar. Is it super simple? I hope it can help people in need.

Source address: https://github.com/codekongs/Android-Learning/tree/master/AndroidLoading

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>