Android custom view implementation with digital progress bar example code
Step 1: effect display
Figure 1. Blue progress bar
Figure 2. Red progress bar
Figure 3. Multiple progress bars with different colors
Figure 4. Multiple progress bars with different colors
Step 2: customize the progressbar to realize the progress bar with numbers
0. Project structure
As shown in the above figure, the library project is the specific implementation of the custom progress bar numberprogressbar with numbers, and the demo project is an example project. The library project is referenced in an engineering dependent manner, and then the custom progress bar numberprogressbar with numbers is used for display
As shown in the figure above: the structure diagram of a custom library project with a digital progress bar
As shown in the above figure: the structure diagram of the demo project
1. Draw step analysis
As shown in the above figures. This progress bar can be divided into the following three parts:
Reached area: a progress bar (rectangle) representing the text before the current progress value
Text area: indicates the current progress value text
Unreached area: the progress bar (rectangle) after the current progress value text
According to the above analysis, we only need to draw the progress bar with numbers in the following three steps:
1. Draw the reach area (the progress bar of the text before the current progress value)
2. Draw text area (current progress value text)
3. Draw the unreached area (the progress bar after the current progress value text).
2. Custom properties
We found that the color, font size, the maximum value of the progress bar, the height of the rectangle representing the progress bar and other properties of the above three parts can be changed to show different interface effects.
Therefore, we make these attributes custom attributes. In this way, we can use XML to define its properties like those components officially provided by Android.
1. Define your own attribute configuration file: attr.xml
Define an attrs.xml file under the RES / values file. The RES / values / attrs.xml definition code is as follows:
2. Define theme configuration file: styles.xml
Define a styles.xml file under the RES / values file, which defines some basic theme options for users to choose. RES / values / styles.xml definition code is as follows:
3. Customize the view to realize the progress bar with numbers
As shown in the above code:
In the construction method of the custom numberprogressbar control, get all the set custom property values. If not set, the default custom property values will be used.
Then override the onmeasure (int widthmeasurespec, int hightmeasurespec) method to determine the size of the custom numberprogressbar control.
Then rewrite the OnDraw () method to draw a custom progress bar with numbers.
Step 3: add the custom progress bar with numbers to the layout file
Define an activity in the RES / layout directory_ Main.xml file, RES / layout / activity_ The main.xml definition code is as follows:
Step 4: write the activity to load the layout file and display the customized progress bar with numbers
The main activity code is as follows:
The renderings shown are:
After reading the introduction, readers can check the complete project code at the following address
The original address of the project on the GitHub of daimajia
https://github.com/daimajia/NumberProgressBar
Here is another example of numberprogressbar, as shown in the following figure
The above content is the example code of Android custom view with digital progress bar introduced by Xiaobian. I hope it will be helpful to you!