protected void OnDraw (canvas canvas) {//TODO auto-generated method Stubsuper.ondraw (canvas);//construction of two brushes, one red, one green paint Paint_green = Generatepaint (Color.green, Style.stroke, 3); Paint paint_red = Generatepaint (color.red, Style.stroke, 3);//Constructs a rectangle rect rect1 = new Rect (0,0,400,220);// Draw the Border Canvas.drawrect (Rect1, Paint_green) with green before panning the canvas, and then redraw the rectangle canvas.translate (100, 100) with a red border after panning the canvas. Canvas.drawrect (Rect1, paint_red);} Private Paint generatepaint (int color,paint.style style,int width) {Paint paint = new Paint ();p aint.setcolor (color); Paint.setstyle (Style);p aint.setstrokewidth (width); return paint;}
Code Analysis:
In this code, for the same rectangle, use green to draw a rectangular border before panning the canvas, and then use red to draw a rectangular border. Do you think the two frames will overlap? The actual result is this.
A Canvas object is called before and after, however, this object is not directly assigned to the graph, this object is actually a "layer" for drawing, but it is not, each call to draw and other functions, the completion of a layer, so although the call is a canvas the same object, Actually calling a different layer
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Canvas Explore