Code of Bofang.java: Import Android.content.Context; Import android.content.res.Resources; Import Android.graphics.Bitmap; Import Android.graphics.BitmapFactory; Import Android.graphics.Canvas; Import Android.graphics.Paint; Import Android.util.AttributeSet; Import Android.view.View; public class Bofang extends View { The width of the int component_width;//control The height of the int component_height;//control Boolean initflag = false;//whether the picture has been initialized Bitmap[] bmp;//An array of pictures to store int currpicindex = 0;//ID of the currently playing picture Int[] bitmapid;//picture number ID Boolean workflag = true;//thread ID for playing picture Public ggviewcx (Context Father,attributeset as)//rewrite constructor { First of all, to play the picture, the first need to have pictures, then give each picture number it, here's the picture resources stored in the res under the Drawable folder under the Int[] Bitmapid ={r.drawable.adv1, R.drawable.adv2, r.drawable.adv3}; Well, the picture's number is now done, what's next? Yes, you should put the picture in the resource into the bitmap array, so let's first determine the number of pictures that will be played, that is, the length of the bitmap array. BMP = new bitmap[bitmapid.length];//here do not directly assign values to BMP, because we may change the image resources at irregular intervals, so we have to modify multiple code, and we The ID of the picture to determine the number of pictures to reduce unnecessary trouble, the following start to initialize the picture, we will initialize the picture in a function Initbitmap ()//Picture initialization complete The image initialization is finished, the next thing we want to do is to play the picture, but before playing the picture, we have a problem, is how to make the picture loop playback? Here we open a new thread to change it regularly The ID of the picture to play to achieve the loop playback of the picture, to achieve the function of looping the picture, we need to overwrite the OnDraw function, first of all, we have to open a new thread New Thread () { Overriding the Run method public void Run () { TODO auto-generated Method Stub while (Workflag)//Always executes this loop (dead loop) { Currindex = (currindex+1)%bitmapid.length;//change the ID of a picture Bofang.this.postInvalidate ()//Refresh screen, causing screen redraw Try { Thread.Sleep (3000);//Pause for 3 seconds, then proceed to the run function, which is to refresh the screen every 3 seconds } catch (Interruptedexception e) { TODO auto-generated Catch block E.printstacktrace (); } } } }.start (); } Initializing a picture public void Initbitmap () { Get a picture of a resource Resources res = this.getresources (); for (int i=0;i<bitmapid.length;i++) { Bmp[i] = Bitmapfactory.decoderesource (res, bitmapid[i]); } } Overwrite OnDraw method @Override protected void OnDraw (Canvas Canvas) { TODO auto-generated Method Stub Super.ondraw (canvas); if (!initflag)//check is the width and height of the control I have acquired, and if not, get the width and height of the control { Component_width = This.getwidth (); Component_height = This.getheight (); Initflag = true; } Canvas.drawbitmap (Bma[currindex], 0, 0,paint);//Draw Picture } } |