连接:https://www.jb51.net/article/98570.htm
在使用glide加载gif动画,有时需要设置播放的次数,然后播放玩一次或者几次之后,需要在播放完做一些其他的操作,直接看代码:
Glide.with(
this
)
.load(R.drawable.xiaoguo)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.listener(
new
RequestListener<Integer, GlideDrawable>() {
@Override
public
boolean
onException(Exception arg0, Integer arg1,
Target<GlideDrawable> arg2,
boolean
arg3) {
return
false
;
@Override
public
boolean
onResourceReady(GlideDrawable resource,
Integer model, Target<GlideDrawable> target,
boolean
isFromMemoryCache,
boolean
isFirstResource) {
// 计算动画时长
GifDrawable drawable = (GifDrawable) resource;
GifDecoder decoder = drawable.getDecoder();
for
(
int
i =
0
; i < drawable.getFrameCount(); i++) {
duration += decoder.getDelay(i);
//发送延时消息,通知动画结束
handler.sendEmptyMessageDelayed(MESSAGE_SUCCESS,
duration);
return
false
;
})
//仅仅加载一次gif动画
.into(
new
GlideDrawableImageViewTarget(imageview,
1
));
解决加载gif文件非常慢的情况:
为其添加缓存策略
复制代码
代码如下:
Glide.with(MainActivity.this).load(url).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
其中缓存策略可以为:Source及None,None及为不缓存,Source缓存原型.如果为ALL和Result就不行
DiskCacheStrategy.NONE 什么都不缓存
DiskCacheStrategy.SOURCE 仅仅只缓存原来的全分辨率的图像。
DiskCacheStrategy.RESULT 仅仅缓存最终的图像,即,降低分辨率后的(或者是转换后的)
DiskCacheStrategy.ALL 缓存所有版本的图像(默认行为)
设置gif播放次数:
这样写会报错的:
Glide.with(MainActivity.this).load(url).asGif().into(new GlideDrawableImageViewTarget(imageview, 1));
设置播放次数和播放监听的时候,不应加上.asGif()
Glide.with(MainActivity.this).load(url).into(new GlideDrawableImageViewTarget(imageview, 1));
new GlideDrawableImageViewTarget(imageview, 1),i
mageview
是控件,
1
是播放次数
设置播放监听:
添加listener()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
listener(
new
RequestListener<Integer, GlideDrawable>() {
@Override
//加载出错
public
boolean
onException(Exception arg0, Integer arg1,
Target<GlideDrawable> arg2,
boolean
arg3) {
return
false
;
}
@Override
//加载完成
public
boolean
onResourceReady(GlideDrawable resource,
Integer model, Target<GlideDrawable> target,
boolean
isFromMemoryCache,
boolean
isFirstResource) {
// 计算动画时长
GifDrawable drawable = (GifDrawable) resource;
GifDecoder decoder = drawable.getDecoder();
for
(
int
i =
0
; i < drawable.getFrameCount(); i++) {
duration += decoder.getDelay(i);
}
//发送延时消息,通知动画结束
handler.sendEmptyMessageDelayed(MESSAGE_SUCCESS,
duration);
return
false
;
}
})
|
连接:https://www.jb51.net/article/98570.htm 在使用glide加载gif动画,有时需要设置播放的次数,然后播放玩一次或者几次之后,需要在播放完做一些其他的操作,直接看代码:Glide.with(this) .load(R.drawable.xiaoguo) .diskCacheStrategy(DiskCache...
一.
Android
-stduio引入类库
在build.gradle中添加依赖:
compile 'com.github.bumptech.
glide
:
glide
:3.7.0'
需要support-v4库的支持,如果你的项目没有support-v4库(项目默认已经添加了),还需要添加support-v4依赖:
如果你用的是3.0以后sdk下面的v4包就不用导入了
compile 'com.
android
.support:support-v4:23.3.0'
然后配置混淆规则:
-keep public class * implements com.bumptech.
glide
.module.
Glide
Module
-keep public enum com.bumptech.
glide
.load.resource.bitmap.ImageHeaderParser$** {
**[] $VALUES;
public *;
Glide
是一个面向
Android
的快速高效的开源媒体管理和图像
加载
框架,它将媒体解码、内存和磁盘缓存以及资源池封装到一个简单易用的接口中;
Glide
支持获取、解码和显示视频静像、图像和
动画
GIF
。
Glide
包括一个灵活的API,允许开发人员插入几乎任何网络堆栈。默认情况下,
Glide
使用
自定义的基于HttpUrlConnection的堆栈,但也包括插件到Google的Volley项目或Square的OkHttp库中的实用程序库。
Glide
的主要重点是尽可能平滑和快速地滚动
//
加载
glide
gif
implementation 'com.github.bumptech.
glide
:
glide
:4.9.0'
annotationProcessor 'com.github.bumptech.
glide
:compiler:4.9.0'
2,assets中加入
gif
图片资源
Glide
.with(this).as
Gif
().load(
gif
Url).into(new
Glide
DrawableImageViewTarget(imageview, N)) ;//N为
播放
次数
2.添加
播放
结束监听
public static void loadOneTime
Gif
(Context context, Object model, final ImageView imageView,
Glide
Glide
是一个高效、开源、
Android
设备上的媒体管理框架,它遵循BSD、MIT以及Apache 2.0协议发布。
Glide
具有获取、解码和展示视频剧照、图片、
动画
等功能,它还有灵活的API,这些API使开发者能够将
Glide
应用在几乎任何网络协议栈里。创建
Glide
的主要目的有两个,一个是实现平滑的图片列表滚动效果,另一个是支持远程图片的获取、大小调整和展示。
Glide
3.0版本以后加入了多项重要功能,同时还支持
使用
Gradle以及Maven进行构建。该版本包括很多值得关注的新功能,如支持
Gif
动画
和视频剧照解码、智能的暂停和重新开始请求、支持缩略图等,具体新增功能如