Android-use surfaceview to draw an animation with multiple threads

Source: Internet
Author: User

Using the surface object in surfaceview for plotting, the essence of which is to use the lockcanvas of surfaceholder to obtain the canvas object for painting. For the animation, the double buffering or double thread must be used, one thread is responsible for specialized preprocessing, reading data from parts, and the other thread is responsible for specialized Drawing Graphics. Because surfaceview locks the canvas each time it draws, that is to say, the canvas cannot be painted next time in the same area. To improve the efficiency of animation playback, you must open a thread to draw a picture, start another thread for preprocessing.

Next we will give an example to explain how to use two threads to increase the drawing speed:

The animation shown below is a decoded image that is quickly moved from the leftmost part of the screen to the right. When it is restarted, the screen is displayed.

Package COM. test. surfaceview; import Java. lang. reflect. field; import Java. util. arraylist; import android. app. activity; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rect; import android. OS. bundle; import android. util. log; import android. VI Ew. surfaceholder; import android. view. surfaceview; public class testsurfaceviewactivity extends activity {private final static string tag = "testsurfaceviewactivity";/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // setcontentview (New mysurfaceview (this); // mysurfaceview is used here Display viewontestinit ();} private surfaceview drawsv = NULL; private surfaceholder drawsh = NULL; arraylist <integer> imglist = new arraylist <integer> (); Private int mwidth = 0, mheight = 0; private Bitmap bitmap = NULL; private LoadImage loadimg = new LoadImage (); Private drawimage drawimg = NULL; private void ontestinit () {drawsv = (surfaceview) This. findviewbyid (R. id. surfacedrawview); drawsh = drawsv. gethold Er (); drawsh. addcallback (New mysvcallback ();} private int onteststart () {If (loadimg! = NULL) {loadimg. start ();} drawimg = new drawimage (0, mheight); drawimg. start (); Return 0;} private void onteststop () {If (loadimg! = NULL) {loadimg. Stop ();} If (drawimg! = NULL) {drawimg. stop () ;}} private class mysvcallback implements surfaceholder. callback {@ overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {// todo auto-generated method stublog. I (TAG, "surfacechanged is called") ;}@ overridepublic void surfacecreated (surfaceholder holder) {// todo auto-generated method stublog. I (TAG, "surfacecreated is called"); // use the reflection mechanism to obtain Image ID and size field [] fields = R. drawable. Class. getdeclaredfields (); For (field: fields) {// If (! "Icon". Equals (field. getname ())&&! "Ic_launcher ". equals (field. getname () {int Index = 0; try {Index = field. getint (R. drawable. class);} catch (illegalargumentexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (illegalaccessexception e) {// todo auto-generated catch blocke. printstacktrace ();} // Save the idimglist image. add (INDEX) ;}} log. I (TAG, "imglist size =" + imglist. size (); // obtain the image size. Bitmap bmimg = bitmapfactory. decode Resource (getresources (), imglist. get (0); mwidth = bmimg. getwidth (); mheight = bmimg. getheight (); onteststart () ;}@ overridepublic void surfacedestroyed (surfaceholder holder) {// todo auto-generated method stublog. I (TAG, "surfacedestroyed is called") ;}} private class LoadImage extends thread {private int imgindex = 0; Public void run () {While (true) {bitmap = bitmapfactory. decoderesource (getresources (), Imglist. get (imgindex); ++ imgindex; If (imgindex = imglist. size () {// cyclically retrieve image data imgindex = 0 ;}}} private class drawimage extends thread {private int X, Y; Public drawimage (int x, int y) {This. X = x; this. y = y;} private void clearscreen () {canvas = drawsh. lockcanvas (null); canvas. drawcolor (color. black); // clear the canvas drawsh. unlockcanvasandpost (canvas);} public void run () {While (true) {If (Bitmap! = NULL) {/*** the following two items have obvious efficiency differences. lockcanvas () specifies the number of times the rect line is cyclically reduced, and * It can improve the drawing efficiency, */canvas c = drawsh. lockcanvas (New rect (this. x, this. y, this. X + mwidth, this. Y + mheight); // canvas c = drawsh. lockcanvas (); C. drawbitmap (bitmap, this. x, this. y, new paint (); drawsh. unlockcanvasandpost (c); // display the content on the screen this. X + = 10; // If the destination is reached, clear the screen and then if (this. x> 1280-mwidth) {This. X = 0; clearscreen ();}}}}}}

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.