这功能源自负责app中要加一个显示gif广告图功能。
android自带控件不支持gif图片,网上很多通过扩展ImageView或View来实现支持gif图片,但在android4.0后,需要关闭硬件加速功能才能使用,而且也容易出现内存溢出问题。
网上找了两个开源包来实现显示Gif图
android-gif-drawable 支持gif显示的view控件
项目地址:https://github.com/koral--/android-gif-drawable,
(如果访问不了,可以此这里下载)
用jni实现的,编译生成so库后直接xml定义view,据说性能比较好,也能比较好避免内存内存溢出问题。
在Android Studio项目添加使用:
build.gradle文件dependencies添加内容:
dependencies {
compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.+' /* 添加gif控件库引用 */
}
xUtils
项目地址:https://github.com/wyouflf/xUtils
包含了很多实用的android工具,这里主要用它下载文件
MainActivity.java
package com.penngo.gif;
import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.os.Bundle;
import android.util.Log;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import java.io.File;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;
/**
*
* https://github.com/koral--/android-gif-drawable
* https://github.com/wyouflf/xUtils
*/
public class MainActivity extends Activity {
private final String tag = "MainActivity-->";
private GifImageView gif1;
private GifImageView gif2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gif1 = (GifImageView)this.findViewById(R.id.info_gif1);
gif2 = (GifImageView)this.findViewById(R.id.info_gif2);
initGif();
}
private void initGif(){
String url1 = "http://img5.imgtn.bdimg.com/it/u=3026352344,1511311477&fm=21&gp=0.jpg";
String url2 = "http://img5.imgtn.bdimg.com/it/u=808161139,2623525132&fm=21&gp=0.jpg";
File saveImgPath = this.getImageDir(this);
File gifSavePath1 = new File(saveImgPath, "gif1");
File gifSavePath2 = new File(saveImgPath, "gif2");
displayImage(url1, gifSavePath1, gif1);
displayImage(url2, gifSavePath2, gif2);
}
public void displayImage(String url, File saveFile, final GifImageView gifView){
HttpUtils http = new HttpUtils();
// 下载图片
http.download(url, saveFile.getAbsolutePath(), new RequestCallBack<File>() {
public void onSuccess(ResponseInfo<File> responseInfo) {
try {
Log.e(tag, "onSuccess========" + responseInfo.result.getAbsolutePath());
GifDrawable gifFrom = new GifDrawable( responseInfo.result.getAbsolutePath() );
gifView.setImageDrawable(gifFrom);
}
catch(Exception e){
Log.e(tag, e.getMessage());
}
}
public void onFailure(HttpException error, String msg) {
Log.e(tag, "onFailure========" + msg);
}
});
}
public File getFilesDir(Context context, String tag){
if(isSdCardExist() == true){
return context.getExternalFilesDir(tag);
}
else{
return context.getFilesDir();
}
}
public File getImageDir(Context context){
File file = getFilesDir(context, "images");
return file;
}
public boolean isSdCardExist() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return true;
}
return false;
}
}
activity_main.xml
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/label_info"
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- gif控件 -->
<pl.droidsonroids.gif.GifImageView
android:id="@+id/info_gif1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_below="@+id/info"
/>
<pl.droidsonroids.gif.GifImageView
android:id="@+id/info_gif2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_below="@+id/info_gif1"
/>
</RelativeLayout>
运行效果: