Detailed explanation of rounded horizontal progress bar example of Android custom progress bar

1. This article will introduce you to the writing method of user-defined progress bar, which is relatively simple, but there are still some knowledge points to pay attention to:

Invalidate() method application of rectf method application of onmeasure method

2. Principle

Draw three layers of rounded rectangle, the bottom layer is black, the second layer is gray, and the top layer is the color of progress bar. The example diagram is as follows:

3. Renderings

There are many other methods to implement the fillet progress bar, such as filling the fillet picture in the progress control and stretching the picture to achieve the desired effect. Although it sounds very simple, it is still troublesome to implement.

4. Interpretation method

(1) Invalidate() method

Invalidate () is used to refresh the view. It must work in the UI thread. For example, when modifying the display of a view, you can call invalidate () to see the redrawn interface. The call to invalidate () is to pop the old view from the main UI thread queue. This method is generally used in custom controls.

(2) Application of rectf method

Rectf is a method for drawing rectangles.

Rectf (left, top, right, bottom). The meaning of the four parameters is the distance between the parent control and the upper left and lower right margins of the rectangle, which is illustrated in the following figure:

The drawroundrect method is used to draw rounded rectangles. Its parameters are as follows: parameter description

Rect: rectf object. Rx: fillet radius in the X direction. Ry: fillet radius in Y direction. Paint: the brush used when painting.

(3) Onmeasure method

Specifies the size of the custom control on the screen. The two parameters of the onmeasure method are the size passed in by the upper level control, and they are values mixed with mode and size. Measurespec. Is required Getmode (widthmeasurespec) gets the mode, measurespec GetSize (widthmeasurespec) gets the size.

Several modes of onmeasure are effective and at_ MOST,UNSPECIFIED。

[1]MeasureSpec. EXACTLY

MeasureSpec. Exactly is the exact size when we put the layout of the control_ Width or layout_ When height is specified as a specific value, such as andorid: layout_ Width = "50dip", or fill_ Parent is the exact size when the control size has been determined.

[2]MeasureSpec. AT_ MOST

MeasureSpec. AT_ Most is the maximum size when the layout of the control_ Width or layout_ Height is specified as wrap_ During content, the size of the control generally changes with the subspace or content of the control. At this time, the size of the control as long as it does not exceed the maximum size allowed by the parent control. Therefore, the mode at this time is at_ Most, size gives the maximum size allowed for the parent control.

[3]MeasureSpec. UNSPECIFIED

MeasureSpec. Unspecified is the mode passed in through the measure method when the parent control is an AdapterView.

5.activity_ main. XML file:

6.ProgressViewTest. Java file

MainActivity. Java file

The above is a detailed explanation of the rounded horizontal progress bar of Android custom progress bar introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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