有没有办法强制加载Glide的gif是非动画的

时间:2016-11-17 13:42:42

标签: android android-glide

我有一组图像,通过Glide加载。

使用滑翔(wasabeef)位图转换模糊NSFW,但有些可以是GIF动画,在这种情况下,第一帧显示为模糊,然后动画开始循环(不模糊)。

以下是我尝试过但不起作用的内容:

DrawableTypeRequest<String> builder = Glide.with(mImage.getContext()).load(...);

if (entry.isNsfw()) {
    builder.asBitmap();
}

builder.diskCacheStrategy(DiskCacheStrategy.SOURCE) //it's likely none of this is relevant
    .skipMemoryCache(true)
    .override(props.getWidth(), props.getHeight());

if (entry.isNsfw()) {
    //successfully blurs nsfw images, but still allows unblurred animation
    builder.dontAnimate().bitmapTransform(centreCrop, blur);
} else {
    builder.centerCrop();
}

builder.crossFade(500) //moving crossfade only into the sfw posts has no effect
       .into(mImage);

也无法正常拦截负载:

builder.listener(new RequestListener<String, GlideDrawable>() {
    @Override
    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
        return false;
    }

    @Override
    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
        if (entry.isNsfw()) {
            target.onResourceReady(resource, null); //If I add this and return true the image is static, but unblurred
            resource.stop(); //if this is called before target, it has no effect
            return true;
        }
        return false; //returning true without calling the target stops any of the images being set on the view
    }
});

如何在保持模糊变换的同时关闭单个图像的gif播放?

2 个答案:

答案 0 :(得分:0)

  

我有一组图像,通过Glide加载

从上面可以看出,我假设您正在使用RecyclerView来显示这些图像,而我的回答是基于这种假设。

尝试使用您自己的自定义Glide目标,如果NSFW像这样停止动画

public class CustomTarget extends GlideDrawableImageViewTarget {

    private boolean isNSFW;

    public CustomTarget(ImageView view) {
        super(view);
    }

    public void setNSFW(boolean isNSFW){
        this.isNSFW = isNSFW;
    }

    @Override
    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {

        // this super call loads the image, sets it to the ImageView and starts
        // animating if it is Gif
        super.onResourceReady(resource, animation); 

        if(isNSFW){
            onStop(); // stop playing the animation if NSFW
        }
    }
}

在View Holder类中,像对待其他视图一样创建并保留对此目标的引用

public class MyViewHolder extends RecyclerView.ViewHolder {

    ImageView myImageView;
    CustomTarget myCustomTarget;

    public MyViewHolder(View itemView) {
        super(itemView);
        myImageView = (ImageView) itemView.findViewById(R.id.myIv);
        myCustomTarget = new CustomTarget(myImageView);
    }
}

然后在onBindViewHolder上执行以下操作

public void onBindViewHolder(MyViewHolder holder, int position) {

    // get the current entry in your image set
    final Entry entry = entries.get(position); 

    // call this before loading the resource
    holder.myCustomTarget.setNSFW(entry.isNSFW()); 

    // No need to specify asGif()
    // Refer http://stackoverflow.com/a/34870817/3533289
    Glide.with(context)
            .load(entry.getURL())
            .into(holder.myCustomTarget);

}

我自己没有测试过这个解决方案。但我希望它会让你知道如何解决这个问题。

答案 1 :(得分:0)

最后混合我在问题中概述的几个步骤,解决了问题。动画NSFW GIF现在只显示第一帧,它已成功模糊。

find /-name pdo.so