Android draw a Tai Chi diagram example code
Let's practice today. Let's draw a Tai Chi chart~
The final effect is as follows:
Final effect
Generally speaking, I talk about the principle first. I'll do the opposite. Let's talk about implementation first.
1. Inheritance implementation initialization method
Inherit view and implement the basic constructor:
In the init () method, initialize the brush.
Get the height and width in the onsizechanged () method for later drawing and calculation.
Create two paths in which the next calculation is performed.
Then we will go to the key OnDraw () method, which will be demonstrated in several steps.
1. Move layout to the middle
PS: for the sake of brevity, the following code is added layer by layer in OnDraw (), and then the outgoing parentheses of OnDraw () are not written.
2. Draw yellow background
Step two png
3. Draw a white round background, that is, the white fish part of the Tai Chi diagram.
4. Draw the black round background, that is, the black fish part of the Tai Chi diagram, which is the same size and position as the white fish, but covers the white fish. Here, you need to draw it with some Boolean operations.
Step four png
5. Perform Boolean calculation on black fish (path1) and remove the unnecessary part. Here is to eliminate the right half of the circle. Here, you need to use path The op () method.
Step 5 png
6. At this time, we have removed the other half of the unnecessary black, but the black fish should have a round head, so we will splice a head for it.
Step 6 png
7. Here, we can see that you only need to draw the head of a white fish. Then, as in step 5, you can use a Boolean operation to remove the excess black.
Step 7 png
8. So far, the background of the Eight Diagrams has been drawn. You only need to draw the fish's eyes.
Step 8 png
Complete, and finally complete the code. The code is a little messy, but it's just practice, ha ha. As for the Boolean operation, write it in my custom view notes later.
Whitelaning It's very easy to be different but very difficult to be better
The above is the example code of realizing Taiji for Android. If you are interested, you can refer to it. Thank you for your support for this site!