Android realizes audio bar graph effect (imitation audio animation without monitoring audio input)

Audio bar graph

The following figure shows the audio bar chart this time:

Because it's just @ r_ 419_ 1356 @ usage, we don't really monitor the audio input, just randomly simulate some numbers.

If you want to implement a static audio bar graph as shown in the figure above, I believe you can quickly find an idea, that is, draw rectangles one by one, and offset each rectangle a little distance. The following code shows a method of calculating coordinates.

In the above code, we create these small rectangles through a loop, where currentheight is the height of each small rectangle. These static small rectangles are drawn through the continuous offset of abscissa. Next, we simulate the audio through the random change of the height of the rectangle. Here, we directly use math Random () method to randomly change these heights and assign them to currentheight. The code is as follows.

In this way, the static effect can be realized, but how to realize the dynamic effect? In fact, it is also very simple. Just call the invalidate () method in the OnDraw () method to notify the view to redraw. However, there is no need to notify view to redraw every time a new rectangle is drawn, which will affect the effect because the refresh speed is too fast. Therefore, we can use the following code for delayed redrawing of view:

In this way, notify view to redraw every 300ms to get a better visual effect. Finally, add a gradient effect to make the view more realistic. The code is as follows:

From this example, we can know that when creating @ r_ 419_ 1356 @ you need to start with a basic effect step by step, slowly add functions and draw more complex effects. No matter how complex @ r_ 419_ 1356 @ must be iterated slowly, so don't feel @ R_ 419_ 1356 @ how hard is it. A journey of a thousand miles begins with a single step. As long as you start doing it, you will gradually become more and more proficient.

code

Here is the complete code for this:

Full code of layout file:

The above is what Xiaobian introduced to you. Android realizes the audio bar graph effect (imitation audio animation without monitoring audio input). 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
分享
二维码
< <上一篇
下一篇>>