最近在做一个项目,遇到一个问题,想在这里请教下大神,如何在android6.0的基础上加载gif图片呢?如果大神看到了,麻烦在评论里面赐教下,也让大伙学习学习,先谢过。如果我找到解决办法了也会分享出来。
下面是我写的一个加载gif图片的源码,直接放在onCreate方法里面就行了,只能支持5.0以前的版本,异步加载也太老了点。说一下需要注意的文件与步骤:
new AsyncTask<Void, Void, byte[]>() {
protected byte[] doInBackground(Void... params) {
String IMAGE_URL = "http://image.sinajs.cn/newchart/min/n/sz300066.gif";
byte[] gifbyte = null;
ByteArrayOutputStream out = null;
InputStream in = null;
HttpURLConnection conn = null;
try {
URL url = new URL(IMAGE_URL);
conn = (HttpURLConnection) url.openConnection();
out = new ByteArrayOutputStream();
in = conn.getInputStream();
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
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 {
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
conn.disconnect();
}
return gifbyte;
}
protected void onPostExecute(byte[] gifbyte) {
Movie gif = Movie.decodeByteArray(gifbyte, 0, gifbyte.length);
if (gif != null) {
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();
以下是需要的jni文件,这里我就贴出图片来了,后面给大家一个下载地址。
这里需要注意的是:android studio新建jni文件夹要修改为jniLibs,这个是固定的,不可命名为别的,否则加载不到.so文件。