1. 程式人生 > >Android使用Glide載入Gif

Android使用Glide載入Gif

1,解決Glide載入Gif非常慢問題

Glide.with(MainActivity.this).load(url).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);

為其新增快取策略,其中快取策略可以為:Source及None,None及為不快取,Source快取原型.如果為ALL和Result就不行

2,載入第一貞:

Glide.with(context).load(gifUrl).asBitmap().into(imageViewGifAsBitmap);

3,控制動畫次數:
Glide.with(this).load(getResource()).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(new GlideDrawableImageViewTarget(imageView, 1));

4,GIF 時間
Glide.with(FirstActivity.this)
        .load(file)
        .asGif()
        .fitCenter()
        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
        .listener(new RequestListener<File, GifDrawable>() {
            @Override
            public boolean onException(Exception e, File model, Target<GifDrawable> target, boolean isFirstResource) {
             
                return false;
            }
 
            @Override
            public boolean onResourceReady(final GifDrawable resource, File model, Target<GifDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int duration = 0;
                        try {
                            GifDrawable gifDrawable = (GifDrawable) resource;
                            GifDecoder decoder = gifDrawable.getDecoder();
                            for (int i = 0; i < gifDrawable.getFrameCount(); i++) {
                                duration += decoder.getDelay(i);
                            }
 
                            mGifAdTime = duration;
                        } catch (Throwable e) {
                        }
                    }
                }).start();
 
                return false;
            }
        })
        .into(mAdImg);