Implementation of Android handset development picture Loop playback

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 configure the control (Main.xml) that will play the picture in the XML file:

The code is as follows Copy Code

<?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.

The code is as follows Copy Code

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
}

}

All right, it's all done here!


PS: To illustrate here, in the above code in fact, there are some places to achieve a better way.

Like what:

In order to realize the timing of the change to play the ID of the picture to achieve loop playback, a new thread opened, and opened a dead loop, but the actual writing is very low controllability, The JDK's java.util.concurrent provides a number of ways to control the execution of a piece of code, and the standard rewrite code is as follows:

The code is as follows Copy Code

Scheduledexecutor Service Scheduler = Executors.newscheduledthreadpool (1);
Scheduler.schedulewithfixeddelay (New Runner (), 0, 1, timeunit.seconds);
//or with Scheduler.scheduleatfixedrate (new Runner (), 0,1, timeunit.seconds);

//We will then implement the Runnable method, which is to change the ID of the current playback picture regularly

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.