Drawing with double buffering technology for Android Development

Source: Internet
Author: User

The Double Buffer technology is mainly used for drawing and animation effects. The principle is to load resources to the buffer zone first, and then load the buffer to the View. The dual-buffer technology can effectively prevent flickering and improve the display quality.

DrawView. java:

Package com. example. handdraw;


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 intVIEW_WIDTH = 720;

Finalint VIEW_HEIGHT = 800;

// Define an image in memory as a buffer

Bitmap cacheBitmap = null;

// Define the Canvas object on cacheBitmap

Canvas cacheCanvas = null;


Public DrawView (Context context, AttributeSet set ){

Super (context, set );

// TODO Auto-generated constructor stub

// Create a buffer with the same size as the View

CacheBitmap = Bitmap. createBitmap (VIEW_WIDTH, VIEW_HEIGHT, Config. ARGB_8888 );

CacheCanvas = new Canvas ();

Path = new Path ();

// When cacheCanvas is set, it will be drawn to the cacheBitmap in the memory.

CacheCanvas. setBitmap (cacheBitmap );

// Set the paint brush color

Paint = new Paint (Paint. DITHER_FLAG );

Paint. setColor (Color. RED );

// Set the paint brush Style

Paint. setStyle (Paint. Style. STROKE );

Paint. setStrokeWidth (1 );

// Anti-sawtooth

Paint. setAntiAlias (true );

Paint. setDither (true );

}


@ Override

Public boolean onTouchEvent (MotionEvent event ){

// Obtain the location where the drag event occurs

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

// Indicates that the handling method has handled the change event

Returntrue;

}


@ Override

Protected void onDraw (Canvas canvas ){

Paint BMP Paint = new Paint ();

// Draw the cacheBitmap to the View component.

Canvas. drawBitmap (cacheBitmap, 0, 0, BMP paint );

// Draw along path

Canvas. drawPath (path, paint );

}

}

My_menu.xml:

Vcg = ">

Activity. java:

Package com. example. handdraw;


Import android. OS. Bundle;
Import android. app. Activity;
Import android. graphics. BlurMaskFilter;
Import android. graphics. Color;
Import android. graphics. EmbossMaskFilter;
Import android. view. Menu;
Import android. view. MenuInflater;
Import android. view. MenuItem;


Public class MainActivity extends Activity {

EmbossMaskFilter emboss;
BlurMaskFilter blur;


@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Emboss = new EmbossMaskFilter (new float [] {1.5f, 1.5f, 1.5f}, 0.6f, 6, 4.2f );
Blur = new BlurMaskFilter (8, BlurMaskFilter. Blur. NORMAL );
}


@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflator = new MenuInflater (this );
Inflator. inflate (R. menu. my_menu, menu );
Return super. onCreateOptionsMenu (menu );
}


@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
// TODO Auto-generated method stub
DrawView dv = (DrawView) findViewById (R. id. draw );
Switch (item. getItemId ())
{
Case R. id. red:
Dv. paint. setColor (Color. RED );
Item. setChecked (true );
Break;
Case R. id. green:
Dv. paint. setColor (Color. GREEN );
Item. setChecked (true );
Break;
Case R. id. blue:
Dv. paint. setColor (Color. BLUE );
Item. setChecked (true );
Break;
Case R. id. width_1:
Dv. paint. setStrokeWidth (1 );
Break;
Case R. id. width_3:
Dv. paint. setStrokeWidth (3 );
Break;
Case R. id. width_5:
Dv. paint. setStrokeWidth (5 );
Break;
Case R. id. blur:
Dv. paint. setMaskFilter (blur );
Break;
Case R. id. emboss:
Dv. paint. setMaskFilter (emboss );
Break;
}
Return true;
}
}

Main. xml

Xmlns: tools = "http://schemas.android.com/tools"

Android: layout_width = "match_parent"

Android: layout_height = "match_parent"

Android: paddingBottom = "@ dimen/activity_vertical_margin"

Android: paddingLeft = "@ dimen/activity_horizontal_margin"

Android: paddingRight = "@ dimen/activity_horizontal_margin"

Android: paddingTop = "@ dimen/activity_vertical_margin"

Tools: context = ". MainActivity">


Android: id = "@ + id/draw"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: text = "@ string/hello_world"/>



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.