Double buffering technology realizes the application _android of Android artboard

Source: Internet
Author: User
Tags android games

What is double buffering technology? Double buffering technology is when the user interface is complete, there will be a buffer to save the results of user operations.

Why use double buffering technology? Take the development of Android games, the interface is all repaint each time, also said to draw a new, the old is gone, so need to use double buffer technology to save the previous content.

How to achieve double 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 Path; 
 Public Paint Paint = null; 
 Final int view_width = 320; 
 Final int view_height = 480; 
 Bitmap cachebitmap = null; 
 
 Canvas Cachecanvas = null; 
  Public Drawview (context, AttributeSet set) {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); 
  @Override public 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; 
  } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); 
  Paint bmppaint = new Paint (); 
  Canvas.drawbitmap (cachebitmap, 0, 0, bmppaint); 
 Canvas.drawpath (path, paint);  } 
 
}

Above is the Android double buffering technology implementation of the artboard application example, the need for friends can refer to.

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.