How does Android clear the canvas?
It seems that no good solution has been found. Some methods restrict the use of solid background, but no matter how you try it, there is no effect,
Finally found the answer on the http://stackoverflow.com/questions/4650755/clearing-canvas-with-canvas-drawcolor:
The result is as follows:
The effects of earlier unsatisfactory Images
(Note: The graph is directed to the top layer)
This class is as follows:
Package COM. test. move; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. paint; import android. graphics. porterduff. mode; import android. graphics. porterduxfermode; import android. util. attributeset; import android. view. surfaceholder; import android. view. surfaceholder. callback; import android. view. surfa Ceview; import COM. test. r; public class globalmove extends surfaceview implements callback, runnable {public final static string tag = "globalmove"; public static int COUNT = 0; private Boolean bsurfacerun = true; private surfaceholder sh; private thread th; private paint P; private canvas; // objects to be moved... private bitmap BMP; private int lastx = 0; Public globalmove (context, attribu Teset attrs... Super (context, attrs); SH = This. getholder (); SH. addcallback (this); Th = new thread (this); P = new paint (); initcomponents (context);} private void initcomponents (context) {BMP = bitmapfactory. decoderesource (context. getresources (), R. drawable. icon);} private void draw () {canvas = Sh. lockcanvas (); If (canvas! = NULL ){
// Cancel the following four lines of comments to see the effect. // Paint = new paint (); // paint. setxfermode (New porterduduxfermode (mode. clear); // canvas. drawpaint (paint); // paint. setxfermode (New porterduduxfermode (mode. SRC); canvas. drawbitmap (BMP, lastx + 10, 50, p);} lastx + = 10; If (lastx> 400) lastx = 0; SH. unlockcanvasandpost (canvas) ;}@ overridepublic void run () {While (bsurfacerun) {draw (); try {thread. sleep (100);} catch (interruptedexception e) {e. printstacktrace ();}}} //////////////////////////////////////// //////////////////////////////////////// ////////////// // @ overridepublic void surfacecreated (surfaceholder holder) {th. start () ;}@ overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {}@ overridepublic void surfacedestroyed (surfaceholder holder) {bsurfacerun = false ;}}