How does Android clear the canvas? (Find the answer for a long time)

Source: Internet
Author: User

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 ;}}

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.