2016-11-17 60 views
1

我有一组图像,通过Glide加载。有没有办法强制GIF加载Glide是非动画

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

以下是我已经尝试并不起作用:

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回放单个图像,同时仍然保持模糊转换?

+0

你有没有试过'onResourceReady'方法返回'true'?返回'false'将执行默认行为。同时检查[this](https://github.com/bumptech/glide/issues/1107#issuecomment-205735933) –

+0

返回true将停止在目标上设置的图像。使用asBitmap也没有效果,我会更新我的示例中的代码,以显示我现在尝试过的其他东西 –

回答

0

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

DrawableRequestBuilder<String> builder = Glide.with(ctx).load(...); 
if (nsfw) { 
    builder.bitmapTransform(new BlurTransformation(ctx, 8, 16), new CentreCrop(...)); 
} else { 
    builder.centerCrop(); 
} 
builder.listener(new RequestListener<String, GlideDrawable>() { 
    ... //onException method 
    @Override 
    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { 
     boolean handled = false; 
     if (nsfw && resource.isAnimated()) { 
      target.onResourceReady(new GlideBitmapDrawable(null, ((GifDrawable) resource).getFirstFrame()), null); 
      handled = true; //glide now won't set the resource on the target itself 
     } 
     return handled; 
    } 
}); 
builder.crossFade(500).into(mImageView); 
0

我有一组图片,通过滑翔

从上面加载,我假设你正在使用RecyclerView显示这些图片,我的答案是基于这样的假设。

用自己的自定义滑行的目标是停止动画,如果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 
     } 
    } 
} 

并且在视图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); 

} 

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

+0

谢谢,不幸的是,这不会成功。这不是我无法访问NSFW标志,而是stop()不起作用。这里的自定义目标不起作用,即使stop()工作,我们也会经过模糊的框架 –

相关问题