Android uses surfaceview to customize the lottery turntable
Customize the lottery turntable with surfaceview
If you don't say much, go to the renderings first
Full code address welcome start
Implementation idea and process
1. First of all, understand the basic usage of surfaceview. It is different from general views. The dual cache mechanism is adopted to draw views in sub threads without losing fluency due to the time-consuming drawing. This is also the reason why surfaceview is used to customize the lottery turntable. After all, the disk blocks of the turntable are drawn, The pictures, words and rotation of the awards are drawn by drawing, which is a time-consuming drawing process.
2. Use the general template style of surfaceview
Member variables commonly used
Initialization normally on
Add callback to surfaceview to implement three of these methods
Define an endless loop in the sub thread to draw continuously
3. After understanding the basic usage of surfaceview, the next step is to implement the lottery turntable
First, measure the range of the entire view and set it to a square
Initialize the brush rectangle range when creating the surfaceview. See the code
The next step is to draw in the open sub thread
The focus is on the draw () method. The following is the implementation of the draw method:
Note: avoid memory leakage caused by mcanvas
Painting background:
Parameter interpretation:
Draw inner ring
Draw the middle eight blocks
Draw text
The text is drawn into a ring shape, and the text is drawn according to the path
Draw the prize icon picture in the disc block
The general drawing is basically completed, and the key point is to make the turntable rotate by changing the starting rotation angle.
Define a method to start the turntable
Stop turning
The specific implementation involves some mathematical knowledge, which may not be described clearly, but it is better to go to the code. It will be clearer to look at the code directly, and the comments in the code are very detailed to prevent you from forgetting when looking back in the future. Welcome to the GitHub address to view the complete code. You can modify it according to your own needs. By the way, learn about the custom view and learn the usage of surfaceview.
Address: complete code address welcome to start. If you find any problems, please give more advice and learn from each other.
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.