Use Android Open Source Library android-gif-drawable to load GIF format pictures

Source: Internet
Author: User

In an Android project, you'll learn the most about PNG-formatted images, or JPEG images. What should we do with the animated type picture gif format picture? We can use the Android-gif-drawable framework to implement the GIF image loading, which is directly below the tool class I used in the project:

public class Gifloader {
/** Save the map*/of a picture reference
public static Map<imageview, string> Mimageviewmap = Collections.synchronizedmap (New Hashmap<imageview, String> ());
Private Executorservice Executorservice;
/** Cache Size 10mib*/
private static int mmemcachemaxsize = 10 * 1024 * 1024;
/**lrucache Cache Pictures */
private static lrucache<string, byte[]> Mmemlrucache;
/** Version Number */
private static int mappversion = 1;
/** hard Disk Cache 50m*/
private static int mdiskcachemaxsize = 50 * 1024 * 1024;
/** hard Disk Cache Object */
private static Disklrucache Mdisklrucache;
/** whether to initialize */
private static Boolean mcacheinit = false;
private static final int disk_cache_count = 1;
/**gifloader Object */
private static Gifloader loader;
/** default id*/of a picture
Final int default_image_id = R.drawable.icon_app_normal;


/** Construction Object */
Private Gifloader (context context) {
Executorservice = Executors.newfixedthreadpool (2);
Initcaches (context);
}


/** single-Case mode */
Public synchronized static Gifloader getinstance (context context) {
if (loader = = null) {
Loader = new Gifloader (context);
}
return loader;
}


/** displaying pictures on controls */
public void displayimage (String url, Gifimageview imageView, Boolean isgif) {
try {
if (new File (URL). Exists ()) {
Imageview.setimagedrawable (new gifdrawable (URL));
Return
}
}
catch (Exception e) {
}


Mimageviewmap.put (ImageView, URL);
byte[] data = mmemlrucache.get (URL);
if (data! = NULL) {
try {
Imageview.setimagedrawable (new gifdrawable (data));
}
catch (Exception e) {
E.printstacktrace ();
Imageview.setimageresource (default_image_id);
}
}
else {
Queuephoto (URL, imageView);
Imageview.setimageresource (default_image_id);
}


}


private void Queuephoto (String url, Gifimageview imageView) {
Phototoload phototoload = new Phototoload (URL, imageView);
Executorservice.submit (New Photosloader (phototoload));
}


/** This method is optimized to prevent memory overflow from being read from the file, and then to download it online * *
Private byte[] getbitmap (String URL) {
Snapshot cacheentry = null;
try {
CacheEntry = Mdisklrucache.get (cachehelper.uritodisklrucachestring (URL));
}
catch (Exception e) {
E.printstacktrace ();
}


byte[] image = null;


if (cacheentry! = null) {
Image = Inputstreamtobytearray (Cacheentry.getinputstream (0), (int) cacheentry.getlength (0));
Mmemlrucache.put (URL, image);
}
try {
if (image! = null) {


return image;
}
else {
URL imageUrl = new URL (URL);
HttpURLConnection con = (httpurlconnection) imageurl.openconnection ();
Con.setconnecttimeout (30000);
Con.setreadtimeout (30000);
Con.setinstancefollowredirects (TRUE);
InputStream is = Con.getinputstream ();
Image = Inputstreamtobytearray (is, 8096);
if (image! = null) {


try {
Editor editor = Mdisklrucache.edit (cachehelper.uritodisklrucachestring (URL));
if (editor! = null) {
if (Cachehelper.writebytearraytoeditor (image, editor)) {
Mdisklrucache.flush ();
Editor.commit ();
}
else {
Editor.abort ();
}
}
}
catch (Exception e) {
E.printstacktrace ();
}


Mmemlrucache.put (URL, image);
}
}


}
catch (FileNotFoundException e) {
E.printstacktrace ();
}
catch (Malformedurlexception e) {
E.printstacktrace ();
}
catch (IOException e) {
E.printstacktrace ();
}


return image;
}


Private class Photosloader implements Runnable {
Private Phototoload phototoload;


Public Photosloader (Phototoload phototoload) {
Super ();
This.phototoload = Phototoload;
}


@Override
public void Run () {
/** Check if ImageView is re-used before downloading * *
if (imageviewreused (phototoload)) {return;}
byte[] BM = Getbitmap (Phototoload.url);


/** Check if the ImageView is re-used after the download is complete */
if (imageviewreused (phototoload)) {return;}
Displayimagerunnable displayimagerunnable = new Displayimagerunnable (BM, phototoload);
Activity A = (activity) photoToLoad.imageView.getContext ();
A.runonuithread (displayimagerunnable);


}


}


Boolean imageviewreused (Phototoload phototoload) {
String tag = Mimageviewmap.get (Phototoload.imageview);
/** represents the value of the ImageView stored in the Imageviews map, which is already overwritten, that is, reusing the */
if (tag = = NULL | |!tag.equals (PHOTOTOLOAD.URL)) {
return true;
}
else {
return false;
}


}


Private class Displayimagerunnable implements Runnable {
Private byte[] data;
Private Phototoload phototoload;


Public displayimagerunnable (byte[] data, Phototoload phototoload) {
Super ();
This.data = data;
This.phototoload = Phototoload;
}


@Override
public void Run () {
if (imageviewreused (phototoload)) {return;}
if (data! = NULL) {
try {
PhotoToLoad.imageView.setImageDrawable (new gifdrawable (data));
}
catch (Exception e) {
E.printstacktrace ();
PhotoToLoad.imageView.setImageResource (default_image_id);
}
}
else {
PhotoToLoad.imageView.setImageResource (default_image_id);
}


}
}


Private class Phototoload {
public String URL;
Public Gifimageview ImageView;


Public phototoload (String URL, Gifimageview imageView) {
Super ();
This.url = URL;
This.imageview = ImageView;
}


}


private void Initcaches (context context) {
if (!mcacheinit) {
Mmemlrucache = new lrucache<string, byte[]> (mmemcachemaxsize) {
protected int sizeOf (String key, byte[] value) {
return value.length;
}
};
File Diskcachedir = Cachehelper.getdiskcachedir (Context, "Imagecache");
try {
Mdisklrucache = Disklrucache.open (Diskcachedir, Mappversion, Disk_cache_count, mdiskcachemaxsize);
}
catch (IOException ignored) {
}
Mcacheinit = true;
}
}


Private byte[] Inputstreamtobytearray (InputStream is, int size) {
Bytearrayoutputstream Bytebuffer = new Bytearrayoutputstream ();
byte[] buffer = new Byte[size];


int len = 0;
try {
while (len = is.read (buffer))! =-1) {
Bytebuffer.write (buffer, 0, Len);
}
}
catch (IOException e) {
E.printstacktrace ();
}


Buffer = Bytebuffer.tobytearray ();
return buffer;
}
}

Use Android Open Source Library android-gif-drawable to load GIF format 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.