Android下载并显示GIF图
最近项目需要实现在线加载GIF图的功能,于是我在网上翻了一番,发现有个开源项目(android-gif-drawable)可以很好的支持GIF动态图的显示。而且它的底层解码使用C实现,极大的提高了解码效率,同时很大程度上避免了OOM现象出现。
这篇文章主要是展示如何使用该项目的Demo,以备不时之需。
布局文件:
<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.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) { // 连接不成功 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.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:下载它的开源库,好像需要翻墙,不方便的同学可以直接从Demo中拿。
Demo:https://github.com/mandmLeee/GifDemo
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。