Allows you to play the Image view of a GIF animation.

Source: Internet
Author: User

Generally, GIF animation cannot be played in imageview.

The core of GIF animation playing here is:

1. Take out each frame in the GIF and use the settime () and draw () Methods of the movie class to draw the interface in real time.

2. process the painting operations in ondraw. Perform logical judgment and determine whether to play the video automatically. If not, draw a start button and set the picture to locate the first frame of GIF animation.


Others can be viewed in the Code. The main class gifimageview. Java has a wide range of comments and should be difficult to understand.

Note that you need to create attrs under the values folder, because you need to customize the property auto_play


Package com.pzf.gif aniamtion; import Java. io. inputstream; import Java. lang. reflect. field; import android. content. context; import android. content. res. typedarray; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. movie; import android. OS. systemclock; import android. text. method. movementmethod; import android. util. attributeset; import and Roid. util. typedvalue; import android. view. view; import android. view. view. onclicklistener; import android. widget. imageview;/*** main class for playing an animation ** @ author pangzf * @ time 2:14:05 on January 1, October 14, 2014 */public class gifimageview extends imageview implements onclicklistener {private movie mmovie; // required for playing an animation, system class private int mimagewidth; // width of the imageview of the animation private int mimageheight; // The height of the animated imageview private long mmoviestart = 0; // start playing private Boolean isautoplay; // whether to automatically play private bitmap mstartplay; // start button private Boolean isplaying = false; // record whether public gifimageview (context, attributeset attrs, int defstyle) {super (context, attrs, defstyle); Init (context, attrs );} public gifimageview (context, attributeset attrs) {This (context, attrs, 0);} public gifimageview (context) {super (context);} private void Init (Context context, attributeset attrs) {typedarray attributes = context. obtainstyledattributes (attrs, R. styleable. gifimageview); // obtain the resource idint resourceid = getresourceid (attributes, context, attrs); If (resourceid! = 0) {// The description is gif animation // 1. change resourcesid to stream // 2. use move to decode the parsing stream // 3. obtain the length and width of the bitmap inputstream is = getresources (). openrawresource (resourceid); mmovie = movie. decodestream (is); If (mmovie! = NULL) {Bitmap bitmap = bitmapfactory. decodestream (is); mimagewidth = bitmap. getwidth (); mimageheight = bitmap. getheight (); // use up to release bitmap. recycle (); // obtain whether automatic playback is allowed. If automatic playback is not allowed, initialize the playback button isautoplay = attributes. getboolean (R. styleable. gifimageview_auto_play, false); If (! Isautoplay) {mstartplay = bitmapfactory. decoderesource (getresources (), R. drawable. start_play); setonclicklistener (this) ;}}@ overrideprotected void onmeasure (INT widthmeasurespec, int heightmeasurespec) {super. onmeasure (widthmeasurespec, heightmeasurespec); If (mmovie! = NULL) {// if it is GIF, set the size of setmeasureddimension (mimagewidth, mimageheight); }}@ overrideprotected void ondraw (canvas) {If (mmovie = NULL) {// normal image super. ondraw (canvas);} else {If (isautoplay) {// if it is a GIF animation, playmovie (canvas); invalidate ();} else {// If automatic playback is not allowed // 1. determine whether the video is being played. // 2. get the first frame of the image // 3. then add the play button if (isplaying) {// playmoive continues playing if (playmovie (canvas) {isplaying = false;} invalidate ();} Else {// the first frame of mmovie. settime (0); mmovie. draw (canvas, 0, 0); // draw start button int offsetw = (mimagewidth-mstartplay. getwidth ()/2; int offseth = (mimageheight-mstartplay. getheight ()/2; canvas. drawbitmap (mstartplay, offsetw, offseth, null) ;}}}/*** play GIF animation ** @ Param canvas */private Boolean playmovie (canvas) {// 1. obtain the playback time // 2. if start = 0, start is considered as start // 3. record the playback time // 4. set progress // 5. animation // 6. if the playback time is later than the playback time, the playback ends. Long Now = systemclock. uptimemillis (); If (mmoviestart = 0) {mmoviestart = now;} int duration = mmovie. duration (); If (duration = 0) {duration = 1000;} // records the duration of GIF playback. Int reltime = (INT) (now-mmoviestart) % duration); mmovie. settime (reltime); // set the time To mmovie. draw (canvas, 0, 0); // draw if (now-mmoviestart)> = duration) {// end mmoviestart = 0; return true;} return false ;} /*** get the SRC resource ID in the layout through reflection *** @ Param TRS * @ Param context * @ Param attributes */private int getresourceid (typedarray attributes, context, attributeset attrs) {try {field filed = typedarray. class. getdeclaredfield ("mvalue"); filed. setaccessible (true); typedvalue typevalue = (typedvalue) filed. get (attributes); Return typevalue. resourceid;} catch (exception e) {e. printstacktrace ();} finally {If (attributes! = NULL) {attributes. recycle () ;}} return 0 ;}@ overridepublic void onclick (view v) {If (v. GETID () = GETID () {isplaying = true; invalidate ();}}}
Attrs. xml
<?xml version="1.0" encoding="utf-8"?><resources>        <declare-styleable name="GifImageView">        <attr name="auto_play" format="boolean"></attr>    </declare-styleable></resources>

Layout File

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:attr="http://schemas.android.com/apk/res/com.pzf.gifaniamtion"    android:layout_width="match_parent"    android:layout_height="match_parent"     android:gravity="center"    >    <com.pzf.gifaniamtion.GifImageView        android:id="@+id/image_view"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/gif_animation"         attr:auto_play="false"        /></LinearLayout>

If you want to enable automatic playback, you only need

 attr:auto_play="true"

: The figure of the mobile phone captured manually by the app package does not work well. If you need it, you can download the source code by yourself, which is smooth.


Source Code address:

Click Open Link


Allows you to play the Image view of a GIF animation.

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.