The single buffer and the double buffering mechanism are implemented separately.
One, single buffering mechanism
1.1 Customizing view, overriding the OnDraw function
public class Brush extends View{private paintbrush= new Paint ();p rivate pathpath= New Path (); int j= 0, n = 0;//xml The constructor that is called when the view is created public Brush (Context Context,attributeset attrs) {super (context,attrs); Brush. Setantialias (True); Brush.setcolor (Color.Black); Brush.setstyle (Paint.Style.STROKE);//STROKE Contour, Fill fills Brush.setstrokejoin (Paint.Join.ROUND),///Set the drawing when the combination of various graphics, such as smoothing effect, etc.//Brush.setstrokecap (Cap.round);// When the style is not fill, the graphic style brush.setstrokewidth (2f);} @Overridepublic boolean ontouchevent (Motionevent event) {Float Pointx = event.getx (); Float pointy = event.gety ();< Span style= "White-space:pre" ></span><pre name= "code" class= "java" > <span style= "White-space:pre" ></span>setdrawingcacheenabled (TRUE);//Opens the view's buffering mechanism, placing this action in 1.1
Switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:path.moveTo (pointx, pointy); Break;case Motionevent.action_ MOVE:path.lineTo (Pointx, pointy); Force a view to draw again//postinvalidate (); invalidate (); return true;} @Override protected void OnDraw (canvas canvas) {canvas.drawbitmap (cachebbitmap, 0, 0, null); Canvas.drawpath (PATH, brush );}
1.2 XML layout file
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" tools:context= ". Mainactivity "> <button android:id=" @+id/save " android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:text= "save"/> <com.example.move.brush android : id= "@+id/picture" android:layout_width= "match_parent" android:layout_height= "Match_parent"/> < /linearlayout>
1.3 Main activity
public class Mainactivity extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Brush picture = (brush) Findviewbyid (r.id.picture); Button Btnsave = (button) Findviewbyid (R.id.save); Btnsave.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {Bitmap bm=bitmap.createbitmap (Picture.getdrawingcache ()));p icture.setdrawingcacheenabled (false); Try{fileoutputstream stream=new FileOutputStream (New File (Environment.getexternalstoragepublicdirectory) ( Environment.directory_pictures), "Hello.png"), <span style= "WHITE-SPACE:PRE;" > </span><pre name= "code" class= "java" ><span style= "White-space:pre" ></span> Bm.compress (Compressformat.png, N, stream);//Compress BM data into PNG image format stream.close (); <pre name= "code" class= "Java" > <span style= "White-space:pre" ></span>} catch (Exception E1) {e1.printstacktrace ();} <pre name= "code" class= "Java" ><spaN style= "font-family:arial, Helvetica, Sans-serif;" ><span style= "White-space:pre" ></span>}</span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ><span style= "White-space:pre" ></SPAN>}); </span><pre name= "code" class= "Java" ><span Style= "Font-family:arial, Helvetica, Sans-serif;" ><span style= "White-space:pre" ></span>}</span><pre name= "code" class= "Java" ><span Style= "Font-family:arial, Helvetica, Sans-serif;" >}</span>
Two-double buffering mechanism
2.1 Custom view, overloaded OnDraw function
public class Brush extends View{private paintbrush= new Paint ();p rivate pathpath= New Path ();p rivate CANVASCACHECANVAS;PR Ivate bitmapcachebbitmap;//C1 is when the code creates the view, C2 is the XML when the view is created public Brush (Context Context,attributeset attrs) {super ( CONTEXT,ATTRS); Brush.setantialias (true); Brush.setcolor (Color.Black); Brush.setstyle (Paint.Style.STROKE);// Stroke Contour, fill fill brush.setstrokejoin (Paint.Join.ROUND),//Set the shape of the drawing when the combination, such as smoothing effect, etc.//Brush.setstrokecap (Cap.round);// When the style is not fill, the graphic style brush.setstrokewidth (2f), <span style= "White-space:pre" ></span>//note below, Used to save images Cachebbitmap = Bitmap.createbitmap (config.argb_8888), Cachecanvas = new Canvas (CACHEBBITMAP); Cachecanvas.drawcolor (Color.White);} @Overridepublic boolean ontouchevent (Motionevent event) {Float Pointx = event.getx (); Float pointy = event.gety (); switch ( Event.getaction ()) {case MotionEvent.ACTION_DOWN:path.moveTo (pointx, pointy); Break;case Motionevent.action_move: Path.lineto (Pointx, pointy); Break;case MotionEvent.ACTION_UP:cacheCanvas.drawPath (path, brush);//lift, only draw line//path.reset ();//affect the part outside the canvas break;} Force a view to draw again//postinvalidate (); invalidate (); return true;} @Override protected void OnDraw (canvas canvas) {canvas.drawbitmap (cachebbitmap, 0, 0, null); Canvas.drawpath (PATH, brush );}}
2.2 XML Layout file
Same 1.2;
2.3 Main Activity
You only need to change the bitmap information in 1.3, that is, bm to Cachbbitmap.
Android Custom view, live drawing saved as a picture