Android glide的使用场景(进阶篇)
2018-05-26 本文已影响44人
肖赛Soaic
之前有写过一篇关于glide的基本使用和相关配置,这篇就介绍一下glide在项目中通常会遇到的一些场景及解决方法
- 当你项目中的头像地址是用前缀加用户ID拼接成的时,修改头像后,会发现头像还是你之前的头像,这时你可以不使用缓存加载图片,代码如下:
Glide.with(MyApplication.getContext())
.load(url)
.dontAnimate()
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(imageView);
- 正确设置圆角图片
当布局文件中用到了自定义圆角控件时
Glide.with(MyApplication.getContext())
.load(url)
.asBitmap()
.dontAnimate()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
imageView.setImageBitmap(resource);
}
});
没用到自定义圆角控件
Glide.with(MyApplication.getContext())
.load(url)
.asBitmap()
.into(new BitmapImageViewTarget(imageView) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(SSTApplication.getContext().getResources(), resource);
circularBitmapDrawable.setCircular(true);
imageView.setImageDrawable(circularBitmapDrawable);
}
});
- 加载大图出现OOM,可以控制高宽尺寸
Glide.with(MyApplication.getContext())
.load(url)
.override(180, 180)
.into(imageView);
- 第一次加载图片出现拉伸变形,可以设置取消动画解决
Glide.with(MyApplication.getContext()).load(url)
.dontAnimate()
.into(imageView);
- 当设置placeholder图片的ScaleType和真实图片的ScaleType不一致时,这里巧妙的通过listener来处理,代码如下:
Glide.with(MyApplication.getContext()).load(url).dontAnimate()
.listener(new SSTGlideRequestListener(imageView,ImageView.ScaleType.CENTER_INSIDE,ImageView.ScaleType.FIT_XY))
.error(R.drawable.categore_def_icon)
.placeholder(R.drawable.categore_def_icon).into(imageView);
static class SSTGlideRequestListener implements RequestListener<String, GlideDrawable> {
private ImageView.ScaleType mPlaceScaleType;
private ImageView.ScaleType mActualScaleType;
private ImageView mImageView;
SSTGlideRequestListener(ImageView mImageView, ImageView.ScaleType placeScaleType, ImageView.ScaleType actualScaleType){
this.mPlaceScaleType = placeScaleType;
this.mActualScaleType = actualScaleType;
this.mImageView = mImageView;
mImageView.setScaleType(placeScaleType);
}
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
mImageView.setScaleType(mPlaceScaleType);
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
mImageView.setScaleType(mActualScaleType);
return false;
}
}
- 提前预下载好图片, 使用preload()方法
Glide.with(MyApplication.getContext())
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.preload();
- 当需要通过url获取缓存文件对象时
File localFile = DiskLruCacheWrapper.get(Glide.getPhotoCacheDir(SSTApplication.getContext()),
DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE).get(new OriginalKey(url, EmptySignature.obtain()));
static class OriginalKey implements Key {
private final String id;
private final Key signature;
private OriginalKey(String id, Key signature) {
this.id = id;
this.signature = signature;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
OriginalKey that = (OriginalKey) o;
return id.equals(that.id) && signature.equals(that.signature);
}
@Override
public int hashCode() {
int result = id.hashCode();
result = 31 * result + signature.hashCode();
return result;
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) throws UnsupportedEncodingException {
messageDigest.update(id.getBytes(STRING_CHARSET_NAME));
signature.updateDiskCacheKey(messageDigest);
}
}
8.根据url判断图片是否下载
File localFile = DiskLruCacheWrapper.get(Glide.getPhotoCacheDir(SSTApplication.getContext()),
DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE).get(new OriginalKey(url, EmptySignature.obtain()));
return localFile!=null;
- 清除当前url的缓存
File localFile = DiskLruCacheWrapper.get(Glide.getPhotoCacheDir(SSTApplication.getContext()),
DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE).get(new OriginalKey(url, EmptySignature.obtain()));
if(file != null)
file.delete();
10.通过自定义View让Glide也可以实现Fresco的viewAspectRatio
宽高比功能
public class SImageView extends AppCompatImageView {
private float mAspectRatio = 0;
public SImageView(Context context) {
this(context,null);
}
public SImageView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public SImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
private void init(AttributeSet attrs) {
final TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.SImageView);
if(typedArray != null) {
if (typedArray.hasValue(R.styleable.SImageView_viewAspectRatio)) {
mAspectRatio = typedArray.getFloat(R.styleable.SImageView_viewAspectRatio,0);
}
typedArray.recycle();
}
}
public void setAspectRatio(float aspectRatio){
if(mAspectRatio != 0) {
mAspectRatio = aspectRatio;
requestLayout();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if(mAspectRatio != 0) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) (width / mAspectRatio);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
attrs.xml
<declare-styleable name="SImageView">
<attr name="viewAspectRatio" format="float"/>
</declare-styleable>
布局文件中只要在SImageView设置app:viewAspectRatio
属性即可,
如:
<com.soaic.widget.SImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:viewAspectRatio="2" />
OK, 以上就是我在项目中遇到的一些场景,希望对大家有所帮助!