Android with ImageView picture loop playback

Source: Internet
Author: User
Tags stub

Xml

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"
Android:id= "@+id/linearlayout" >

</LinearLayout>

Main file:

The code is as follows Copy Code

public class Picbaractivity extends activity{
Private LinearLayout LinearLayout;
Private Picbar MyView;

@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.pic_progress);

LinearLayout = (linearlayout) This.findviewbyid (r.id.linearlayout);
MyView = new Picbar (this);
Linearlayout.addview (MyView);
}
}

View File:

The code is as follows Copy Code


public class Picbar 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 currindex = 0;//ID of the currently playing picture
Int[] bitmapid;//picture number ID
Boolean workflag = true;//thread ID for playing picture

Public Picbar {
Super (context);

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
Bitmapid =new int[]{r.drawable.loading1_03, r.drawable.loading2_03, r.drawable.loading3_03};

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
PicBar.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;
}
Paint p = new Paint ();
Canvas.drawbitmap (Bmp[currindex], 0, 0, p);//Draw Picture
}

}

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.