Recent projects need to implement the ability to load GIFs online, so I doubled on the web and found an open source project (android-gif-drawable) It can support the display of GIF dynamic graph well. and its lower level decoding uses the C implementation, greatly improves the decoding efficiency, at the same time avoids the oom phenomenon to a large extent.
This article is mainly about showing how to use the project demo for a rainy time.
Layout file:
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testgif.MainActivity" >
<pl.droidsonroids.gif.GifImageView
android:id="@+id/myGifView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
Activity code:
package com.example.test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
protected static final String IMAGE_URL = "http://img.blog.csdn.net/20150410135837339";
// protected static final String IMAGE_URL =
// "http://img.blog.csdn.net/20150310123909933";
protected static final String TAG = "MainActivity";
private GifImageView myGifImageView;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
myGifImageView = (GifImageView) findViewById (R.id.myGifView);
new AsyncTask <Void, Void, GifDrawable> () {
@Override
protected GifDrawable doInBackground (Void ... params) {
byte [] gifbyte = null;
HttpURLConnection conn = null;
try {
URL url = new URL (IMAGE_URL);
conn = (HttpURLConnection) url.openConnection ();
ByteArrayOutputStream out = new ByteArrayOutputStream ();
InputStream in = conn.getInputStream ();
if (conn.getResponseCode ()! = HttpURLConnection.HTTP_OK) {
// connection was unsuccessful
Log.i (TAG, "Unsuccessful connection");
return null;
}
byte [] buffer = new byte [1024];
int len = 0;
while ((len = in.read (buffer))> 0) {
out.write (buffer, 0, len);
}
gifbyte = out.toByteArray ();
} catch (MalformedURLException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
conn.disconnect ();
}
// write to file
/ * FileOutputStream fos = null;
try {
File root = Environment.getExternalStorageDirectory ();
File myFile = new File (root, "test.gif");
Log.v (TAG, myFile.getAbsolutePath ());
fos = new FileOutputStream (myFile);
fos.write (gifbyte);
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
if (fos! = null) {
try {
fos.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
} * /
GifDrawable gifDrawable = null;
try {
gifDrawable = new GifDrawable (gifbyte);
} catch (IOException e) {
e.printStackTrace ();
}
return gifDrawable;
}
protected void onPostExecute (GifDrawable drawable) {
myGifImageView.setImageDrawable (drawable);
};
} .execute ();
}
@Override
public boolean onCreateOptionsMenu (Menu menu) {
getMenuInflater (). inflate (R.menu.main, menu);
return true;
}
}
PS: Download its open source Library, as if the need to turn over the wall, inconvenient students can directly from the demo to take.
Demo: Https://github.com/mandmLeee/GifDemo
Android downloads and displays GIF images