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. Under 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. The speed is relatively slow. Surfaceview: View class that is expanded based on view view. More suitable for 2D game development; It is a subclass of view, using a double-slow mechanism. Update the screen in a new thread so the refresh interface is 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, can define the color of the brush

2, you can define the thickness of the brush

3, can achieve a variety of common shape drawing

4, agree to return to the canvas, is to go back to the previous step

5, to support the eraser function

6, have finished painting, to support the preservation

We will gradually implement these five function points.

First, about the color and thickness of the brush you define. The simplest thing to do is to call only the setcolor (int color) and setstrokewidth (float width) of the paint. It is essential that the use of Surfaceview drawing requires attention to obtain a canvas instance through Surfaceholder, when it is possible to paint through a canvas instance and draw the end call Unlockcanvasandpost (canvas) to commit the change.

Second, support free curve, straight line, rectangle, round, solid rectangle, solid circle, it is very convenient to expand.

This first abstracts out a base class action, and each time it is drawn is an action instance, and our artboard is a list of action. This will enable a very good fallback function.

Third, the fallback of the canvas. Assuming that the size of the action list on the canvas is not 0, the canvas is now backed back, just remove the last action in the list and draw again.

Four, Eraser. I took a coincidence here. 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, which creates a wipe effect. In fact it was only covered by the white curve.

In accordance with the implementation of the 3rd, 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, assuming that the method of using view can only get a black screen.

A good way to do this is to draw the list of actions we saved once again. Code such as the following


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

If you think it's helpful, please subscribe to my public account-- Android Dry Share (id:android_share) . The following is the QR code, to provide you with timely high-quality Android dry.

Technical Exchange QQ Group: 318588906, welcome everyone add Group. Explore Android and Java technologies together. Grow our dry-sharing community together.

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.