Class Library for loading GIF animation in Android

Source: Internet
Author: User

I haven't written a blog for a long time. I feel that I can't accumulate knowledge if I don't write anything. However, these days have been hard-working and have encountered learning bottlenecks, so the blog has not been updated. However, in the past few days, a mobile phone anti-theft software has been implemented, that is, after a mobile phone is lost, you can obtain the location of your lost mobile phone via text message. However, the effect is not very satisfactory, so I have never dared to write it down on my blog. If you have any good ideas, you are welcome to discuss with me. Through the station short message or mailbox, can be: crazypebble.android@gmail.com.

Now, let's get started. I hope you don't mind my complaints ~ I want to share my current mood with you. Next let's go to the topic.

On the Android platform, Google does not provide a class library for displaying GIF animations. If you want to display the animation effect on the Android platform, you may need to use the animation class provided by Android to help us create the animation effect. However, the animation display effect is not as ideal as that of GIF files, animations created by the animation class are relatively simple and simple. Therefore, I have collected some online methods for displaying GIF animations on the Android platform. I would like to share with you here.

1. gif animation Segmentation

After encoding, you must use a third-party tool to divide GIF animations into BMP or JPG images, and then load these images using the animation class provided by Android to achieve the animation effect. In fact, to put it bluntly, we still use the animation method, and we also need to manually split GIF images, which is very troublesome! However, this disguised method can also implement the display animation function.

No more text here. Just give a link. Previously said, is the information collected on the internet, please jump to the following link: http://www.cnblogs.com/TerryBlog/archive/2010/09/06/1819641.html If You Need To reprint, please respect the labor results of this blogger.

Ii. gifview class

Let's take a look at the development documentation and look for the gifview class. Are there wood and wood? The result is yes. Yes, this is a class written by N people to load GIF animation. It has been made into a jar class library for developers to directly load and use. This class is very convenient to use, and is the same as the use of ordinary UI controls such as imageview, button, and textview.

1. Principle:

I have analyzed the core of this class in detail, basically according to the GIF file format, the GIF is divided into BMP images, and then these BMP images are displayed. The principle is the same as the first method. However, the gifview class places operations on splitting GIF image frames in the same class, so we don't need to worry too much about how GIF is split, the gifview class also provides a public method for developers to conveniently call.

2. Public methods provided by gifview

Set the data source of GIF Animation:

/**
* Load GIF animation from the file data of a GIF File
* @ Param GIF File Content
*/
Public void setgifimage (byte [] GIF ){
Setgifdecoderimage (GIF );
}

/**
* Load GIF animation from a file or network input stream
* @ Param input stream
*/
Public void setgifimage (inputstream is ){
Setgifdecoderimage (is );
}

/**
*
* @ Param resid the resource ID of the GIF Animation
*/
Public void setgifimage (INT resid ){
Resources r = This. getresources ();
Inputstream is = R. openrawresource (resid );
Setgifdecoderimage (is );
}

Control the display mode of GIF Animation: dynamic or static

/**
* Display the first frame of GIF animation, that is, to prevent GIF animation from moving.
*/
Public void showcover (){
If (gifdecoder = NULL)
Return;
Pause = true;
Currentimage = gifdecoder. getimage ();
Invalidate ();
}
/**
* Dynamically display GIF Animation
*/
Public void showanimation (){
If (pause ){
Pause = false;
}
}

Set the display ratio of GIF Animation:

/**
* Set the height and width of the GIF animation.
* @ Param width
* @ Param height
*/
Public void setshowdimension (INT width, int height ){
If (width> 0 & height> 0 ){
Showwidth = width;
Showheight = height;
Rect = new rect ();
Rect. Left = 0;
Rect. Top = 0;
Rect. Right = width;
Rect. Bottom = height;
}
}

How to Set GID animation display: (3)

Load first and then display: gifimagetype. wait_finish

Edge loading and display: gifimagetype. sync_decoder

Only display the first frame and then display: gifimagetype. Cover

    public void setGifImageType(GifImageType type){
if(gifDecoder == null)
animationType = type;
}

3. Usage

The specific usage will be released together with the source code and JAR file of the gifview class at the end of the article. Here we will introduce it to you first.

A. Add gifview. jar to the project;

B. Add the GIF view control to the layout file and set the basic attributes of the GIF view, including ID, width, height, and margin. Similar to other UIS

     <com.ant.liao.GifView
android:id="@+id/gif1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingRight="14px" android:enabled="false" />

C. Add a control instance in the Code and load the display.

// Get the gifview handle from XML
Gf1 = (gifview) findviewbyid(r.id.gif 1 );
// Set the GIF Image Source
Gf1.setgifimage(r.drawable.gif 1 );
// Add listener
Gf1.setonclicklistener (this );
// Set the display size, stretch, or compress
Gf1.setshowdimension (300,300 );
// Set the loading mode: First load and then display, while loading, only display the first frame and then display
Gf1.setgifimagetype (gifimagetype. cover );

The above is what we want to introduce. After that, we will give a link to the author of the GIF View class. You can pay more attention to and learn more about the technology. Pay tribute to csdn. Http://blog.csdn.net/iamlazybone/archive/2010/10/28/5972234.aspx

With the gifview class source code and demo program: http://u.115.com/file/f648778d4a

Because the gifdecode. java file annotations cannot be normally displayed, So I analyzed the source code and briefly wrote a comment: gifdecode. java to make it easier for everyone to learn.

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.