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 Android game development, 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 do I implement 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 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);
}
}