Android下载并显示GIF图

   最近项目需要实现在线加载GIF图的功能,于是我在网上翻了一番,发现有个开源项目(android-gif-drawable可以很好的支持GIF动态图的显示。而且它的底层解码使用C实现,极大的提高了解码效率,同时很大程度上避免了OOM现象出现。

  这篇文章主要是展示如何使用该项目的Demo,以备不时之需。

  Demo内容:通过URL下载图片,并且判断是否是gif图片,显示图片。


布局文件:

<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代码:

package com.example.test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Movie;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

	// protected static final String IMAGE_URL = "https://img-blog.csdn.net/20150410135837339";
	protected static final String IMAGE_URL ="https://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, byte[]>() {

			@Override
			protected byte[] 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) {
						// 连接不成功
						Log.i(TAG, "连接不成功");
						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();
				}

				// 写入文件

				/*
				 * FileOutputStream fos = null; try {
				 * 
				 * File root = Environment.getExternalStorageDirectory(); File
				 * myFile = new File(root, "test.jpg"); 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(); } } }
				 */

				return gifbyte;

			}

			protected void onPostExecute(byte[] gifbyte) {

				// 判断是否是gif图
				Movie gif = Movie.decodeByteArray(gifbyte, 0, gifbyte.length);
				if (gif != null) {
					Log.v(TAG, "是gif图片");
					GifDrawable gifDrawable = null;
					try {
						gifDrawable = new GifDrawable(gifbyte);
					} catch (IOException e) {
						e.printStackTrace();
					}
					myGifImageView.setImageDrawable(gifDrawable);
				} else {
					Bitmap gifBitmap = BitmapFactory.decodeByteArray(gifbyte,
							0, gifbyte.length);
					myGifImageView.setImageBitmap(gifBitmap);
				}

			};

		}.execute();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

PS:下载它的开源库,好像需要翻墙,不方便的同学可以直接从Demo中拿。

Demo:https://github.com/mandmLeee/GifDemo


猜你喜欢

转载自blog.csdn.net/u012964281/article/details/45393709