Best practices for Android graffiti

Source: Internet
Author: User

Best practices for Android graffiti

In Android, there are two ways to implement gesture painting. One is to draw directly on the View, but to use SurfaceView.

There are still some differences between the two. Introduction.

View: A display View with a built-in canvas that provides graphic rendering functions, touch-screen events, and key-event functions. You must update the screen in the main UI thread. Slow speed. SurfaceView: Expanded view Class Based on view. It is more suitable for the development of 2D games. It is a subclass of view and uses the double easing mechanism. Refreshing the interface is faster than refreshing the view because the screen is updated in the new thread. Therefore, the latter should be preferred for graffiti.

Before starting the code, you must simply implement the functions.

1. Ability to customize the color of the paint brush

2. You can customize the paint brush width.

3. Ability to draw various common shapes

4. Agree to roll back the canvas, that is, return to the previous step.

5. Support the eraser Function

6. Save the finished painting.

Below we will gradually implement these five features.

1. Define the color and width of the paint brush. This is the simplest. You only need to call the setColor (int color) and setStrokeWidth (float width) Methods of painting. To use SurfaceView to draw a picture, you must note that the Canvas instance is obtained through SurfaceHolder. In this case, you can draw a picture through the Canvas instance and call unlockCanvasAndPost (canvas) to submit the changes after the painting ends.

2. Supports free curves, straight lines, rectangles, circles, solid rectangles, and solid circles for convenient extension.

Here, we first abstract a base class Action. Every draw is an action instance, and our canvas is a list of actions. In this way, the rollback function is well supported.

3. roll back the canvas. If the action list on the canvas is not 0, it indicates that the canvas now supports rollback. You only need to remove the last action from the list and draw it again.

4. eraser. I have a coincidence here. The canvas background is white. Therefore, the implementation of the eraser is also an action. The shape is a free curve and the color is white, which creates an erased effect. In fact, it is only covered by the white curve.

According to the third implementation, the eraser also supports rollback.

5. Save the canvas. The canvas is full of your various images. The last step is to save them. However, the screenshot of View and SurfaceView is different. View is static and passive, and SurfaceView is active and dynamic, assume that the View method can only obtain a black screen.

In this case, we can plot the saved action list again. The Code is as follows:


Related Article

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.