Android Doodle Best Practices

Source: Internet
Author: User

There are two ways to implement gesture drawing in Android, one is to draw directly on the view and use Surfaceview. There are some differences between the two, a brief introduction. View: Display view, built-in canvas, provides graphical drawing functions, touch-screen events, key event functions, etc., must be updated within the UI main thread, slower. Surfaceview: Views based on the view of the expansion of the class, more suitable for the development of 2D Games, is the subclass of the view, using a double-slow mechanism to update the screen in a new thread so the refresh interface faster than the view. So, to achieve the function of graffiti first choose the latter.

Before you start code, simply sort out the functionality you want to implement.

1. You can customize the color of the brush

2, you can customize the thickness of the brush

3, can achieve a variety of common shape drawing

4, allow the canvas fallback, is to go back to the previous step

5, to support the eraser function

6, have finished painting, to support the preservation

Now we are going to implement these five function points gradually.

First, about the color and thickness of the custom brush, this is the simplest, only need to call the paint setcolor (int color) and setstrokewidth (float width) two methods. It is important to note that using the Surfaceview drawing requires that you obtain a canvas instance through Surfaceholder, where you can draw through the canvas instance, drawing the end call Unlockcanvasandpost (canvas) to commit the change.

Second, support free curve, straight line, rectangle, round, solid rectangle, solid circle, it is convenient to expand. Here we first abstract a base class action, each time the drawing is an action instance, our artboard is a list of actions, so that can support the fallback function well.

Third, the fallback of the canvas. If the size of the action list on the canvas is not 0, the canvas currently supports fallback, just remove the last action in the list, and redraw is OK.

Four, Eraser. Here I took a coincidence, the background of the canvas is white, so the implementation of the eraser is also an action, the shape of the free curve, the color is white, so that creates a kind of erased effect, in fact, only by the white curve to cover up. In accordance with the 3rd implementation, the Eraser also supports fallback.

Five, save the artboard. The canvas is full of your various graphics, the last step is to save, but the view and Surfaceview interception is different, the view is static passive, Surfaceview is active dynamic, if you use the View method can only get a black screen. A good way to do this is to redraw the list of actions we saved. The code is as follows


Code Address: Https://github.com/JackCho/AndroidDoodle

If you find it helpful, you are welcome to subscribe to my public account Android Dry Share (id:android_share) . Below is the QR code, to provide you with timely high-quality Android dry.

Technical Exchange QQ Group: 318588906, welcome everyone to add a group, together to explore the next Android and Java technology, together to expand our dry sharing community.

Android Doodle Best Practices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.