Android Implementation of image loop playback

Source: Internet
Author: User

Most of the time, we need to display images on the client and dynamically display the images, that is, constantly switching the images. Next we will look at the specific implementation method. First, we need...
Most of the time, we need to display images on the client and dynamically display the images, that is, constantly switching the images. Next we will look at the specific implementation method.

First, we need to configure the control (main. XML) of the image to be played in the xml file ):

Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: orientation = "vertical">

<! -- Here is the control for playing the image, and bofang is the View class for playing the image -->

<Com. sunianjinshi. bofang

Android: layout_width = "180dip"

Android: layout_height = "250dip"

/>

</LinearLayout>

Now, the control that needs to be used to play the image is configured. Next we will write the implementation class bofang. java.Copy codeThe Code is as follows: 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

{

Int COMPONENT_WIDTH; // control width

Int COMPONENT_HEIGHT; // control height

Boolean initflag = false; // whether the image has been initialized

Bitmap [] bmp; // array used to store images

Int currPicIndex = 0; // the ID of the image to be played later

Int [] bitmapId; // image ID

Boolean workFlag = true; // specifies the thread position of the picture to be played.

Public GGViewCX (Context father, AttributeSet as) // rewrite the constructor

{

// First, to play an image, you must first have an image. Then, you must first number each image. The image resources are stored in the drawable folder under res.

Int [] bitmapId = {R. drawable. adv1, R. drawable. adv2, R. drawable. adv3 };

// Well, the image number has been fixed. What should I do next? Yes, the images in the resource should be inserted into the Bitmap array. First, determine the number of images to be played, that is, the length of the Bitmap array.

Bmp = new Bitmap [bitmapId. length]; // do not directly assign the value to bmp here, because we may change the image resources from time to time, so we need to modify multiple codes.

// The image ID to determine the number of images to reduce unnecessary troubles. Next, initialize the image and put the initialized image in a function.

InitBitmap (); // The end of image Initialization

// The initialization of the image ends. Next, we need to play the image. But before playing the image, we have a question: how can we achieve loop playback of the image? Here we have another thread to change on time.

// The ID of the image to be played to realize the loop playing of the image. To implement the function of Loop Playing the image, we need to override the onDraw function. First, let's start a new thread.

New Thread ()

{

// Override the run Method

Public void run ()
{
// TODO Auto-generated method stub
While (workflag) // always execute this loop (endless loop)
{
CurrIndex = (currIndex + 1) % bitmapId. length; // change the image ID
Bofang. this. postInvalidate (); // refresh the screen, causing screen repainting
Try
{
Thread. sleep (3000); // pause for 3 seconds here, and then continue to run the run function to refresh the screen every 3 seconds.
}
Catch (InterruptedException e)
{
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}

}. Start ();

}

// Initialize the image

Public void initBitmap ()

{

// Obtain the resource Image

Resources res = this. getResources ();

For (int I = 0; I <bitmapId. length; I ++)

{

Bmp [I] = BitmapFactory. decodeResource (res, bitmapId [I]);

}

}

// Override the onDraw Method

@ Override
Protected void onDraw (Canvas canvas)
{
// TODO Auto-generated method stub
Super. onDraw (canvas );
If (! Initflag) // check whether the width and height of the control have been obtained. If not, the width and height of the control are obtained.
{
COMPONENT_WIDTH = this. getWidth ();
COMPONENT_HEIGHT = this. getHeight ();
Initflag = true;
}
Canvas. drawBitmap (bma [currIndex], 0, 0, paint); // draw an image
}

}

PS: It should be noted that there are some better implementation methods in the code below.

For example:

In order to change the ID of the image to be played on time to achieve loop playback, we have opened a new thread and an endless loop. However, this method is very controllable, JDK java. util. concurrent provides a large number of methods to control the punctual execution of a piece of code. The following code is rewritten as follows:

Copy codeThe Code is as follows: ScheduledExecutorService schedpool = Executors. newScheduledThreadPool (1 );
Scheddel. scheduleWithFixedDelay (new runner (), 0, 1, TimeUnit. SECONDS );
// Or use schedner. scheduleAtFixedRate (new runner (), TimeUnit. SECONDS );

// Then we need to implement the Runnable method, that is, to change the ID of the current playback image on time.

Public class runner implements Runnable
{
Public void run ()

{

// TODO Auto-generated method stub
CurrIndex = (currIndex + 1) % bitmapId. length;
Bofang. this. postInvalidate (); // refresh the screen
}
}

Related Article

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.