Implement the android canvas application using double buffering technology

Source: Internet
Author: User

What is double buffer technology? The dual-buffer technology means that after the user operation interface is complete, a buffer zone will be provided to save the user operation results.

Why is dual-buffer technology used? For android game development, the UI is all re-painted every time, so it is said that there is a new image, and the old one is gone, so we need to use the dual-buffer technology to save the previous content.

How to implement dual buffering? Use a bitmap object to retain the previous canvas.

Package COM. example. phonegaptest; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmap. config; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. path; import android. util. attributeset; import android. view. motionevent; import android. view. view; public class drawview extends view {float prex; float prey; private Path; Public paint = NULL; Final int view_width = 320; Final int view_height = 480; bitmap cachebitmap = NULL; canvas cachecanvas = NULL; Public drawview (context, attributeset) {super (context, set); cachebitmap = bitmap. createbitmap (view_width, view_height, config. argb_8888); cachecanvas = new canvas (); Path = New Path (); cachecanvas. setbitmap (cachebitmap); paint = new paint (paint. dither_flag); paint. setcolor (color. red); paint. setstyle (paint. style. stroke); paint. setstrokewidth (1); paint. setantialias (true); paint. setdither (true) ;}@ overridepublic Boolean ontouchevent (motionevent event) {float x = event. getx (); float y = event. gety (); Switch (event. getaction () {Case motionevent. action_down: path. moveTo (x, y); prex = x; prey = y; break; Case motionevent. action_move: path. quadto (prex, prey, x, y); prex = x; prey = y; break; Case motionevent. action_up: cachecanvas. drawpath (path, paint); Path. reset (); break;} invalidate (); Return true ;}@ overrideprotected void ondraw (canvas) {super. ondraw (canvas); paint BMP paint = new paint (); canvas. drawbitmap (cachebitmap, 0, 0, BMP paint); canvas. drawpath (path, paint );}}

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.