SoFunction
Updated on 2025-04-09

Double buffering technology realizes Android artboard application

What is double buffering technology? Double buffering technology means that when the user operation interface is completed, a buffer will save the results of the user operation.

Why use double buffering technology? Take Android game development as an example. Interface Zhen is repainted every time, which means that if the new one is drawn, the old one is gone, so you need to use double buffering technology to save the previous content.

How to implement double buffering? Just use a Bitmap object to preserve the previous canvas.

package ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
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 = (VIEW_WIDTH, VIEW_HEIGHT, 
    Config.ARGB_8888); 
  cacheCanvas = new Canvas(); 
 
  path = new Path(); 
  (cacheBitmap); 
 
  paint = new Paint(Paint.DITHER_FLAG); 
  (); 
  (); 
  (1); 
  (true); 
  (true); 
 } 
 
 @Override 
 public boolean onTouchEvent(MotionEvent event) { 
  float x = (); 
  float y = (); 
 
  switch (()) { 
  case MotionEvent.ACTION_DOWN: 
   (x, y); 
   preX = x; 
   preY = y; 
   break; 
  case MotionEvent.ACTION_MOVE: 
   (preX, preY, x, y); 
   preX = x; 
   preY = y; 
   break; 
  case MotionEvent.ACTION_UP: 
   (path, paint); 
   (); 
   break; 
  } 
  invalidate(); 
  return true; 
 } 
 
 @Override 
 protected void onDraw(Canvas canvas) { 
  (canvas); 
  Paint bmpPaint = new Paint(); 
  (cacheBitmap, 0, 0, bmpPaint); 
  (path, paint); 
 } 
 
} 

The above is an example of the application of Android double buffering technology to implement artboards. Friends who need it can refer to it.