《第二行代码进阶》 解决Glide相同图片链接,图片内容不同更新问题

这篇文章本来不打算放到进阶课程里面的,就当凑数吧

想实现的目标:既能换掉图片内容也能用上缓存

所以每次清缓存或者直接不用缓存的方式就不适合我了

首先Glide没有很明确的对于这问题的api,只能百度。

百度找到个有用的方法:

       RequestOptions userAvatarOptions = new RequestOptions()
      .signature(new ObjectKey(System.currentTimeMillis()));

       Glide.with(ProfileActivity.this).applyDefaultRequestOptions(userAvatarOptions).load(iamgePath).apply(RequestOptions.bitmapTransform(new CircleCrop())).
                    into(mIvApHeader);

如果你不用缓存,相同图片链接这样就可以直接更新了

关键点:经过测试它会跟.signature(new ObjectKey)走,也就是只要我的key不一致就用新的,一致就用旧的(也就是用缓存的)

这时就需要接口人员配合了,修改头像时传个时间戳给接口,在返回头像时也再连带时间戳返回给app

可参考我的处理方案

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void displayCircle(Context context, Object path, ImageView imageView) {
    if (context != null) {
        if (context instanceof Activity && ((Activity) context).isDestroyed()) {
            return;
        }
        RequestOptions userAvatarOptions = null;
        if (path instanceof String && path.toString().startsWith("http")){
            //是http才处理
             if (SharedPreferencesUtils.getLong(context,path.toString(),0) != 0){
                //如果之前存储有该链接的key,就直接用上次的
                userAvatarOptions = new RequestOptions()
                        .signature(new ObjectKey(SharedPreferencesUtils.getLong(context,path.toString(),0)))
                        .circleCrop().autoClone();
            } else {
                userAvatarOptions = new RequestOptions().circleCrop().autoClone();
            }
        } else {
            userAvatarOptions = new RequestOptions().circleCrop().autoClone();
        }

        Glide.with(context)
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .thumbnail(0.1f)
                .placeholder(R.drawable.default_avatar_yuan_40_3x)
                .apply(userAvatarOptions)
                .into(imageView);
    }
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void displayCircleAndKey(Context context, Object path,long updateAvatarTimestamp, ImageView imageView) {
    if (context != null) {
        if (context instanceof Activity && ((Activity) context).isDestroyed()) {
            return;
        }
        RequestOptions userAvatarOptions = null;
        if (path instanceof String && path.toString().startsWith("http")){
            //是http才处理
            if (updateAvatarTimestamp != 0){
                //如果传有缓存key,就直接用它并保存
                SharedPreferencesUtils.putLong(context,path.toString(),updateAvatarTimestamp);
                userAvatarOptions = new RequestOptions()
                        .signature(new ObjectKey(updateAvatarTimestamp))
                        .circleCrop().autoClone();
            } else if (SharedPreferencesUtils.getLong(context,path.toString(),0) != 0){
                //如果之前存储有该链接的key,就直接用上次的
                userAvatarOptions = new RequestOptions()
                        .signature(new ObjectKey(SharedPreferencesUtils.getLong(context,path.toString(),0)))
                        .circleCrop().autoClone();
            } else {
                userAvatarOptions = new RequestOptions().circleCrop().autoClone();
            }
        } else {
            userAvatarOptions = new RequestOptions().circleCrop().autoClone();
        }


        Glide.with(context)
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .thumbnail(0.1f)
                .placeholder(R.drawable.default_avatar_yuan_40_3x)
                .apply(userAvatarOptions)
                .into(imageView);
    }
}

关键是如何运用好ObjectKey

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章