Android Display Network GIF pictures

Source: Internet
Author: User

This feature is derived from the ability to add a display GIF to the app.

Android comes with controls that do not support GIF images, many of which support GIF images by extending imageview or view, but after android4.0, you need to turn off hardware acceleration for use and memory overflow issues.

Two open source packages were found on the Internet to display GIF images

Android-gif-drawable view controls that support GIF display

Project Address:

With JNI implementation, the compiler generates so library directly after the XML definition view, it is said that the performance is better, can also be better to avoid memory memory overflow problem.

To add an Android studio project using:

Build.gradle file dependencies Add content:

dependencies {

Compile ' pl.droidsonroids.gif:android-gif-drawable:1.1.+ '/* * Add GIF Control Library Reference */



Project Address: Https://

Contains a lot of useful Android tools, the main use it to download files

package com.penngo.gif;import;import android.content.context;import  android.os.environment;import android.os.bundle;import android.util.log;import  com.lidroid.xutils.httputils;import com.lidroid.xutils.exception.httpexception;import  com.lidroid.xutils.http.responseinfo;import com.lidroid.xutils.http.callback.requestcallback;import;import pl.droidsonroids.gif.gifdrawable;import pl.droidsonroids.gif.gifimageview;/**  * * * xutils */public class mainactivity extends activity {     private final string tag =  "Mainactivity-->";    private  gifimageview gif1;    private gifimageview gif2;    @ override    protected void&Nbsp;oncreate (bundle savedinstancestate)  {         Super.oncreate (savedinstancestate);         setcontentview ( R.layout.activity_main);        gif1 =  (GifImageView) This.findviewbyid (R.ID.INFO_GIF1);        gif2 =  ( Gifimageview) This.findviewbyid (R.ID.INFO_GIF2);         initgif ();     }    private void initgif () {         String url1 =  ",1511311477 &fm=21&gp=0.jpg ";        string url2 = " http//,2623525132&fm=21&gp=0.jpg ";         file saveimgpath = this.getimagedir (this);         file gifsavepath1 = new file ( saveimgpath,  "GIF1");        file gifsavepath2 =  New file (saveimgpath,  "Gif2");         displayimage (URL1, &NBSP;GIFSAVEPATH1,&NBSP;GIF1);         displayimage (url2,  GIFSAVEPATH2,&NBSP;GIF2);     }    public void displayimage ( String url, file savefile, final gifimageview gifview) {         httputils http = new httputils ();         //  Download Image (url,  Savefile.getabsolutepath (), new requestcallback<file> ()  {             puBlic void onsuccess (Responseinfo<file> responseinfo)  {                 try {      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.E (tag,  " onsuccess======== " + responseinfo.result.getabsolutepath ());                     gifdrawable giffrom  = new gifdrawable ( responseinfo.result.getabsolutepath ()  );                      Gifview.setimagedrawable (Giffrom);                 }                 catch (exception e) {                    &NBSP;LOG.E (Tag, e.getmessage ());                 }            }             public void onfailure (HttpException &NBSP;ERROR,&NBSP;STRING&NBSP;MSG)  {            &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.E (tag,  "onfailure========"  + msg);             }        });     }    public file getfilesdir (Context context, String  tag) {        if (Issdcardexist ()  == true) {              return context.getexternalfilesdir (TAG);         }        else{             return context.getfilesdir ();         }    }    public file getimagedir (Context  Context) {        file file = getfilesdir (context,  "Images");        return file;    }     public boolean issdcardexist ()  {         if  (Environment.getexternalstoragestate (). Equals (                 environment.media_mounted))  {             return true;        }         return false;    }}


<relativelayout xmlns:android= "Http://"      xmlns:tools= "Http://"  android:layout_width= "Match_parent"      android:layout_height= "Match_parent"  android:paddingleft= "@dimen/activity_horizontal_margin"      android:paddingright= "@dimen/activity_horizontal_margin"     android: paddingtop= "@dimen/activity_vertical_margin"     android:paddingbottom= "@dimen/activity_ Vertical_margin " tools:context=". Mainactivity ">    <textview android:text=" @string/label_info "         android:id= "@+id/info"         android: Layout_width= "Wrap_content"         android:layout_height= "wrap_content " />    <!-- gif control  -->    <pl.droiDsonroids.gif.gifimageview        android:id= "@+id/info_gif1"          android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"         android: Scaletype= "Fitxy"         android:layout_below= "@+id/info"          />    <pl.droidsonroids.gif.GifImageView         android:id= "@+id/info_gif2"          android:layout_width= "Wrap_content"         android:layout_ height= "Wrap_content"         android:scaletype= "FitXY"          android:layout_below= "@+id/info_gif1"          /></relativElayout> 

Operating effect:

Android Display Network GIF pictures

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