/** * Gif增强 * * @return */ public GlidePlus gifPlus() { mRequestBuilder = Glide.with(mContext) .using(new StreamStringLoader(mContext), InputStream.class) .from(String.class) .as(ImageWrapper.class) .transcode(new ImageWrapperDrawableTranscoder(mContext), Drawable.class) .decoder(new ImageWrapperStreamResourceDecoder(mContext)) .cacheDecoder(new ImageWrapperFileToStreamDecoder(mContext)) .sourceEncoder(new StreamEncoder()) .animate(new DrawableCrossFadeAnimator()) .diskCacheStrategy(DiskCacheStrategy.SOURCE); return this; }
Glide(Engine engine, RequestQueue requestQueue, MemoryCache memoryCache, BitmapPool bitmapPool, Context context) { this.engine = engine; this.requestQueue = requestQueue; this.bitmapPool = bitmapPool; this.memoryCache = memoryCache; dataLoadProviderFactory = new DataLoadProviderFactory(); dataLoadProviderFactory.register(InputStream.class, Bitmap.class, new StreamBitmapDataLoadProvider(bitmapPool)); dataLoadProviderFactory.register(ParcelFileDescriptor.class, Bitmap.class, new FileDescriptorBitmapDataLoadProvider(bitmapPool)); ImageVideoDataLoadProvider imageVideoDataLoadProvider = new ImageVideoDataLoadProvider(bitmapPool); dataLoadProviderFactory.register(ImageVideoWrapper.class, Bitmap.class, imageVideoDataLoadProvider); GifDataLoadProvider gifDataLoadProvider = new GifDataLoadProvider(context, bitmapPool); dataLoadProviderFactory.register(ImageVideoWrapper.class, GifBitmapWrapper.class, new ImageVideoGifDataLoadProvider(imageVideoDataLoadProvider, gifDataLoadProvider)); register(File.class, ParcelFileDescriptor.class, new FileDescriptorFileLoader.Factory()); register(File.class, InputStream.class, new StreamFileLoader.Factory()); register(Integer.class, ParcelFileDescriptor.class, new FileDescriptorResourceLoader.Factory()); register(Integer.class, InputStream.class, new StreamResourceLoader.Factory()); register(String.class, ParcelFileDescriptor.class, new FileDescriptorStringLoader.Factory()); register(String.class, InputStream.class, new StreamStringLoader.Factory()); register(Uri.class, ParcelFileDescriptor.class, new FileDescriptorUriLoader.Factory()); register(Uri.class, InputStream.class, new StreamUriLoader.Factory()); register(URL.class, InputStream.class, new StreamUrlLoader.Factory()); register(GlideUrl.class, InputStream.class, new VolleyUrlLoader.Factory(requestQueue)); transcoderFactory.register(Bitmap.class, BitmapDrawable.class, new BitmapDrawableTranscoder(context.getResources(), bitmapPool)); transcoderFactory.register(GifBitmapWrapper.class, Drawable.class, new GifBitmapDrawableTranscoder(context)); }
@Override public void onAttach(Context context) { super.onAttach(context); BitmapPool pool = Glide.get(context).getBitmapPool(); // default decoder, this is the base complexity Glide.with.load.into() hides from you GifBitmapWrapperStreamResourceDecoder decoder = new GifBitmapWrapperStreamResourceDecoder( new GifBitmapWrapperResourceDecoder( new ImageVideoBitmapDecoder( new StreamBitmapDecoder(pool), new FileDescriptorBitmapDecoder(context) ), new GifResourceDecoder(context, pool), pool ) ); // all the defaults hardcoded here in order to end up with a normal Drawable instead of a GlideDrawable urlGlide = Glide .with(this) .using(new StreamStringLoader(getActivity()), InputStream.class) .from(String.class) .as(GifBitmapWrapper.class) .transcode(new GeneralizingTranscoder<GifBitmapWrapper, GlideDrawable, Drawable>( new GifBitmapWrapperDrawableTranscoder(new GlideBitmapDrawableTranscoder(context))), Drawable.class) .decoder(decoder) .sourceEncoder(new StreamEncoder()) .cacheDecoder(new FileToStreamDecoder<>(decoder)) .animate(new AlwaysCrossFade<>(true)) .encoder(new GifBitmapWrapperResourceEncoder(new BitmapEncoder(), new GifResourceEncoder(pool))) .diskCacheStrategy(DiskCacheStrategy.ALL) // just to demonstrate it's working, pick your preference .transform(new GifBitmapWrapperTransformation(pool, new FitCenter(context))) // == .fitCenter() .listener(new LoggingListener<String, Drawable>("url")) // debug ; // see https://github.com/bumptech/glide/issues/122#issuecomment-99629392 drawableGlide = Glide .with(this) .using(new PassthroughModelLoader<Drawable, Drawable>(), Drawable.class) .from(Drawable.class) .as(Drawable.class) // this works even if the drawables don't behave well regarding constantState.newDrawable // Beware: might be problematic if constant state is supported, but altered (e.g. color filters) .decoder(new SimpleResourceDecoder<Drawable>()) // prefer DrawableResourceDecoder if possible! .animate(new AlwaysCrossFade<>(true)) .diskCacheStrategy(DiskCacheStrategy.NONE) .listener(new LoggingListener<Drawable, Drawable>("drawable")) // debug ; // prevents null check the first time // don't forge to null everything out explicitly (error/placeholder/fallback/thumbnail), if they're set above lastLoad = urlGlide.clone().load(null).listener(null).dontAnimate(); }