Using double buffering technology to realize the application of Android artboard

Source: Internet
Author: User
Tags config

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);

}

}

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.