Best practices for Android graffiti

Source: Internet
Author: User

Best practices for Android graffiti

In Android, there are two ways to implement gesture plotting. One is to draw directly on the View, but to use SurfaceView. There are some differences between the two. View: display View, built-in canvas, provides graphics rendering functions, touch screen events, button event functions, etc. You must update the screen in the main UI thread, the speed is slow. SurfaceView: Expanded view Class Based on view, which 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, let's simply look at the functions to be implemented.

1. You can customize the paint brush color.

2. You can customize the paint brush width.

3. Various common shapes can be drawn.

4. To allow the canvas to be rolled back, return to the previous step.

5. Support the eraser Function

6. Save the finished painting.

Next we will gradually implement these five features.

1. For the color and width of a custom Paint brush, the simplest method is to call the setColor (int color) and setStrokeWidth (float width) Methods of painting. It is important to note that SurfaceView is used to draw a Canvas instance through SurfaceHolder. In this case, you can draw a 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, so that the rollback function can be well supported.

3. roll back the canvas. If the action list on the canvas is not 0, it indicates that the canvas currently supports rollback. You only need to remove the last action from the list and redraw it. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + CjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20140903/2014090308253764.jpg" alt = "\">

4. eraser. Here I took a coincidence that the canvas background is white, so the implementation of the eraser is also an action, the shape is a free curve, the color is also white, in this way, a erased effect is created, but it is actually covered by a white curve. According to the implementation of the third point, the eraser also supports rollback.

5. Save the canvas. The canvas is full of your various images. The last step is to save them, but the screenshot of the View and SurfaceView is different. The View is static and passive, and the SurfaceView is active and dynamic, if you use the View method, you can only obtain one black screen. In this case, we can re-plot the saved action list. The Code is as follows:


Address: https://github.com/JackCho/AndroidDoodle

If it is helpful to you, you are welcome to subscribe to my public account --Android Dry Goods sharing (ID: android_share). The QR code below provides you with timely and high quality Android dry goods.

Technical Exchange QQ group: 318588906. You are welcome to join the group to discuss Android and Java technologies and expand our sharing community.

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.