An example method for the implementation of image looping by Android _android

Source: Internet
Author: User
Tags stub

Most of the time, we need to show the pictures on the client, but also the dynamic display, that is, constantly switching pictures. Now let's take a look at the specific implementation methods. First, we need to be in the XML ...
Most of the time, we need to show the pictures on the client, but also the dynamic display, that is, constantly switching pictures. Now let's take a look at the specific implementation methods.

First, we need to configure the control (Main.xml) that will play the picture in the XML file:

Copy Code code 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 to play the picture, Bofang is the view class used to play the picture-->

<com.sunianjinshi.bofang

Android:layout_width= "180dip"

Android:layout_height = "250dip"

/>

</LinearLayout>

OK, the control that needs to play the picture here is configured, next we will write the implementation class Bofang.java.
Copy Code code 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

{

The width of the int component_width;//control

The height of the int component_height;//control

Boolean Initflag = false;//is not already initialized picture

Bitmap[] An array of bmp;//used to register pictures

int Currpicindex = 0;//The ID of the picture to play after

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 the picture resources are sent under the Res drawable folder under the

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

Well, the picture number was already 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 irregularly, so that 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 ();//The end of the picture initialization

Image initialization ended, and then we have 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 have another new thread to change on time.

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 the 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 that I have obtained the width and height of the control, if not, then 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
}

}

PS: Here to illustrate, the following code in fact, there are a better way to implement.

Like what:

In order to realize the timely change to play the ID of the picture, in order to achieve loop playback, a new thread opened, and opened a dead loop, but in practice such a very low control of the wording, The JDK's java.util.concurrent provides a large number of ways to control a piece of code to execute on time, and the following code for the standard rewrite is as follows:

Copy Code code as follows:

Scheduledexecutorservice Scheduler = Executors.newscheduledthreadpool (1);
Scheduler.schedulewithfixeddelay (New Runner (), 0, 1, timeunit.seconds);
or use Scheduler.scheduleatfixedrate (New Runner (), 0,1, timeunit.seconds);

And then we're going to implement the Runnable method, which is to change the ID of the picture now.

Public class runner implements Runnable
{
public void Run ()

{

TODO auto-generated Method Stub
Currindex = (currindex+1)%bitmapid.length;
Bofang.this.postInvalidate ()//Refresh 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.