I have been studying this problem for the past two or three days. Today I have finally succeeded. The final result I want to achieve is that when I press the button for the first time when taking a photo like a camera, a rectangle box will appear and I don't want to take a photo. If I press it again, the face will be framed again, draw a new rectangle. That's simple.
Misunderstanding 1: I have set a cleardraw () sub-function and set it in the sub-function:
Void cleardraw () {/* clear the canvas */canvas = Sh. lockcanvas (); If (canvas! = NULL) {paint = new paint (); paint. setxfermode (New porterduxfermode (porterduff. mode. clear); canvas. drawpaint (paint); // canvas. drawcolor (color. blue);} Sh. unlockcanvasandpost (canvas );}
Then, clear the screen before pressing the key to identify the object. The result is that the screen is clear, but the first rectangle will pop out when the rectangle is drawn for the second time. And so on, there will indeed be a short clear screen effect, but the screen cannot be cleared eventually.
Misunderstanding 2:
Please refer to the following link for more information: http://www.eoeandroid.com/thread-79664-1-1.html. I use canvas.drawline (). That is to say, set the paint to the clear screen mode and repaint all the painted images with this paint brush, but unfortunately, this effect is the same as the first one.
Misunderstanding 3:
Some people proposed to use the SAVE and restore of canvas to solve the problem, but unfortunately this is not the case at all. Reference: http://www.devdiv.com/article-1948-1.html
The correct method is as follows: Define a painting method in surfaceview and execute screen cleaning at the beginning of the painting method. The following three statements are important:
Public void drawline (point P1, point P2, point P3) {canvas = Sh. lockcanvas (); canvas. drawcolor (color. transparent); paint P = new paint (); // clear screen p. setxfermode (New porterduduxfermode (mode. clear); canvas. drawpaint (p); p. setxfermode (New porterduduxfermode (mode. SRC); p. setantialias (true); p. setcolor (color. red); p. setstyle (paint. style. fill); canvas. drawline (0,0, 500,500, P); SH. unlockcanvasandpost (canvas );}
Clear screen:
Paint P = new paint ();
// Clear screen
P. setxfermode (New porterduxfermode (mode. Clear ));
Canvas. drawpaint (P );
P. setxfermode (New porterduxfermode (mode. SRC ));
........ Set paint here and start drawing your own .........
Reference: http://stackoverflow.com/questions/4650755/clearing-canvas-with-canvas-drawcolor
Http://blog.csdn.net/yuanlong_zheng/article/details/7592457
For more information, please add android to the daily Q & A group: 248217350.