2016-11-17 5 views
1

Glideで読み込まれた一連の画像があります。Glideで読み込まれたgifにアニメーションが適用されないようにする方法がありますか?

グライド(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); 

も動作していないが、負荷を傍受されています

私は一方で、個々のイメージのGIFの再生をオフにするにはどうすればよい
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 
    } 
}); 

まだぼかし変換を維持していますか?

+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を使用していて、私の答えは、この仮定に基づいていると仮定します。

あなたが他のために行うように、このターゲットへの参照を作成しておく、この

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クラスのようNSFW場合、アニメーションを停止し、独自のカスタムグライド目標とそれを試してみてくださいビュー

その後onBindViewHolderで
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); 
    } 
} 

次の操作を行います

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()が動作しても、ぼやけたフレームを過ぎてしまいます。 –

関連する問題