2017-07-07 64 views
1

美好的一天。使用Glide從URL加載佔位符以在加載GIF時顯示(Android)

什麼我是這樣的:

Glide 
      .with(this) 
      .load(imageUrl) 
      .asGif() 
      .diskCacheStrategy(DiskCacheStrategy.SOURCE) 
      .placeholder(R.drawable.gif) 
      .into(imageView); 

但是,相反,我想利用滑行來加載相同的GIF asBitmap()作爲佔位符使用,而它的加載實際GIF。

一樣,如果我能做到:.placeholder(Glide.with(this).load(imageUrl).asBitmap())

在此先感謝。

回答

1

你必須通過URL中.thumbnail(URL)作爲

.thumbnail(Glide 
     .with(context) 
     .load(Url) 
     .asBitmap() 

或者這樣: -

DrawableRequestBuilder<String> thumbnail = Glide.with(context) 
      .diskCacheStrategy(DiskCacheStrategy.ALL) 
      .load(url); 
    try { 
     Glide.with(context) 
       .diskCacheStrategy(DiskCacheStrategy.ALL) 
       .error(placeholder) 
       .load(url) 
       .thumbnail(thumbnail) 
       .into(imageView); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

參考:

https://github.com/bumptech/glide/issues/1198

https://futurestud.io/tutorials/glide-thumbnails

https://github.com/bumptech/glide/issues/362

private void loadImage(ImageView image, @RawRes int typeID, String imagePath) { 
Context context = image.getContext(); 
BitmapPool pool = Glide.get(context).getBitmapPool(); 

// OPTION 1 Bitmap 
Glide 
    .with(image.getContext()) 
    .load(imagePath) 
    .asBitmap() 
    .animate(android.R.anim.fade_in) 
    .placeholder(R.drawable.image_loading) 
    .error(R.drawable.image_error) 
    .thumbnail(Glide 
     .with(context) 
     .load(typeID) 
     .asBitmap() 
     .imageDecoder(new SvgBitmapDecoder(pool)) // implements ResourceDecoder<InputStream, Bitmap> 
    ) 
    .into(image) 
; 

// OPTION 2 GlideDrawable 
Glide 
    .with(image.getContext()) 
    .load(imagePath) 
    .crossFade() 
    .placeholder(R.drawable.image_loading) 
    .error(R.drawable.image_error) 
    .thumbnail(Glide 
     .with(context) 
     .load(typeID) 
     .decoder(new GifBitmapWrapperResourceDecoder(
        new ImageVideoBitmapDecoder(
         new SvgBitmapDecoder(pool), 
         null /*fileDescriptorDecoder*/ 
        ), 
        // just to satisfy GifBitmapWrapperResourceDecoder.getId() which throws NPE otherwise 
        new GifResourceDecoder(context, pool), 
        pool 
       ) 
     ) 
    ) 
    .into(image) 
; 
}