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: https://github.com/koral--/android-gif-drawable

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 */

}


Xutils

Project Address: Https://github.com/wyouflf/xUtils

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


Mainactivity.java

package com.penngo.gif;import android.app.activity;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  java.io.file;import pl.droidsonroids.gif.gifdrawable;import pl.droidsonroids.gif.gifimageview;/**  * * https://github.com/koral--/android-gif-drawable * https://github.com/wyouflf/ 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 =  "http://img5.imgtn.bdimg.com/it/u=3026352344,1511311477 &fm=21&gp=0.jpg ";        string url2 = " http// Img5.imgtn.bdimg.com/it/u=808161139,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         http.download (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;    }}


Activity_main.xml

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"      xmlns:tools= "Http://schemas.android.com/tools"  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

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.