@Override public void onBindViewHolder(@NonNull ViewHolderImpl viewHolder, int row, int column) { final TestViewHolder vh = (TestViewHolder) viewHolder; String itemData = mTableDataSource.getItemData(row, column); // skip headers if (TextUtils.isEmpty(itemData)) { itemData = ""; } itemData = itemData.trim(); vh.tvText.setVisibility(View.VISIBLE); vh.ivImage.setVisibility(View.VISIBLE); vh.tvText.setText(itemData); Glide.with(vh.ivImage.getContext()) .load(itemData) .apply(new RequestOptions().transform(new FitCenter())) .listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { vh.ivImage.setVisibility(View.INVISIBLE); vh.tvText.setVisibility(View.VISIBLE); return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { vh.ivImage.setVisibility(View.VISIBLE); vh.tvText.setVisibility(View.INVISIBLE); return false; } }) .into(vh.ivImage); }
@Override public void onAnimationEnd(View view) { super.onAnimationEnd(view); //change the image when alpha=0 Glide.with(imageView.getContext()).load(urlImage) .apply(new RequestOptions().centerCrop()) .listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { //then fade to alpha=1 new Handler(Looper.getMainLooper()) {}.post(new Runnable(){ @Override public void run(){ fadeIn(viewToAnimate, alpha, fadeDuration, new ViewPropertyAnimatorListenerAdapter()); if (imageLoadListener != null) { imageLoadListener.OnImageLoad(imageView, ((BitmapDrawable) imageView.getDrawable()).getBitmap()); } } }); return false; } }) .into(viewToAnimate); } });
private void runTestStringDefaultLoader(String string) { requestManager.load(string).listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { throw new RuntimeException("Load failed"); } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { return false; } }).into(target); requestManager.load(string).into(imageView); verify(target).onResourceReady(isA(BitmapDrawable.class), isA(Transition.class)); verify(target).setRequest((Request) notNull()); assertNotNull(imageView.getDrawable()); }
@Test public void testListenerApiOverridesListeners() { getNullModelRequest().addListener(listener1).listener(listener2).into(target); verify(requestManager).track(any(Target.class), requestCaptor.capture()); requestCaptor.getValue().onResourceReady(new SimpleResource<>(new Object()), DataSource.LOCAL); // The #listener API removes any previous listeners, so the first listener should not be called. verify(listener1, never()) .onResourceReady( any(), any(), isA(Target.class), isA(DataSource.class), anyBoolean()); verify(listener2) .onResourceReady( any(), any(), isA(Target.class), isA(DataSource.class), anyBoolean()); }
public static void loadFlickrFull(Photo photo, ImageView image, LoadingListener listener) { final String photoUrl = photo.getLargeSize() == null ? photo.getMediumUrl() : photo.getLargeUrl(); final RequestOptions options = new RequestOptions() .diskCacheStrategy(DiskCacheStrategy.DATA) .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) .dontTransform(); final RequestBuilder<Drawable> thumbRequest = Glide.with(image) .load(photo.getMediumUrl()) .apply(options); Glide.with(image) .load(photoUrl) .apply(new RequestOptions().apply(options).placeholder(image.getDrawable())) .thumbnail(thumbRequest) .listener(new RequestListenerWrapper<>(listener)) .into(image); }
@Test public void clearRequest_withError_afterPrimaryFails_clearsErrorRequest() { WaitModel<Integer> errorModel = WaitModelLoader.Factory.waitOn(ResourceIds.raw.canonical); FutureTarget<Drawable> target = Glide.with(context) .load((Object) null) .error( Glide.with(context) .load(errorModel) .listener(requestListener)) .submit(); Glide.with(context).clear(target); errorModel.countDown(); // Make sure any pending requests run. concurrency.pokeMainThread(); Glide.tearDown(); // Make sure that any callbacks posted back to the main thread run. concurrency.pokeMainThread(); }
@Test public void requestManager_intoImageView_withSameByteArrayAndMemoryCacheEnabled_loadsFromMemory() throws IOException { final byte[] canonicalBytes = getCanonicalBytes(); concurrency.loadOnMainThread( Glide.with(context) .load(canonicalBytes) .apply(skipMemoryCacheOf(false)), imageView); Glide.with(context).clear(imageView); concurrency.loadOnMainThread( Glide.with(context) .load(canonicalBytes) .listener(requestListener) .apply(skipMemoryCacheOf(false)), imageView); verify(requestListener).onResourceReady( anyDrawable(), any(), anyDrawableTarget(), eq(DataSource.MEMORY_CACHE), anyBoolean()); }
@Test public void requestBuilder_intoImageView_withSameByteArrayAndMemoryCacheEnabled_loadsFromMemory() throws IOException { final byte[] canonicalBytes = getCanonicalBytes(); concurrency.loadOnMainThread( Glide.with(context) .asDrawable() .load(canonicalBytes) .apply(skipMemoryCacheOf(false)), imageView); Glide.with(context).clear(imageView); concurrency.loadOnMainThread( Glide.with(context) .asDrawable() .load(canonicalBytes) .listener(requestListener) .apply(skipMemoryCacheOf(false)), imageView); verify(requestListener).onResourceReady( anyDrawable(), any(), anyDrawableTarget(), eq(DataSource.MEMORY_CACHE), anyBoolean()); }
@Test public void load_whenLoadSucceeds_butEncoderFails_doesNotCallOnLoadFailed() { WaitForErrorStrategy strategy = new WaitForErrorStrategy(); Glide.init(context, new GlideBuilder() .setAnimationExecutor(GlideExecutor.newAnimationExecutor(/*threadCount=*/ 1, strategy)) .setSourceExecutor(GlideExecutor.newSourceExecutor(strategy)) .setDiskCacheExecutor(GlideExecutor.newDiskCacheExecutor(strategy))); Glide.get(context).getRegistry().prepend(Bitmap.class, new FailEncoder()); concurrency.get( Glide.with(context) .load(ResourceIds.raw.canonical) .listener(requestListener) .submit()); verify(requestListener) .onResourceReady( anyDrawable(), any(), anyDrawableTarget(), any(DataSource.class), anyBoolean()); verify(requestListener, never()) .onLoadFailed(any(GlideException.class), any(), anyDrawableTarget(), anyBoolean()); }
@Test public void load_whenEncoderFails_callsUncaughtThrowableStrategy() { WaitForErrorStrategy strategy = new WaitForErrorStrategy(); Glide.init(context, new GlideBuilder() .setAnimationExecutor(GlideExecutor.newAnimationExecutor(/*threadCount=*/ 1, strategy)) .setSourceExecutor(GlideExecutor.newSourceExecutor(strategy)) .setDiskCacheExecutor(GlideExecutor.newDiskCacheExecutor(strategy))); Glide.get(context).getRegistry().prepend(Bitmap.class, new FailEncoder()); concurrency.get( Glide.with(context) .load(ResourceIds.raw.canonical) .listener(requestListener) .submit()); // Writing to the disk cache and therefore the exception caused by our FailEncoder may happen // after the request completes, so we should wait for the expected error explicitly. ConcurrencyHelper.waitOnLatch(strategy.latch); assertThat(strategy.error).isEqualTo(FailEncoder.TO_THROW); verify(requestListener, never()) .onLoadFailed(any(GlideException.class), any(), anyDrawableTarget(), anyBoolean()); }
void setImage(final int adapterPosition) { // Load the image with Glide to prevent OOM error when the image drawables are very large. requestManager .load(IMAGE_DRAWABLES[adapterPosition]) .listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { viewHolderListener.onLoadCompleted(image, adapterPosition); return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { viewHolderListener.onLoadCompleted(image, adapterPosition); return false; } }) .into(image); }
public static void loadYouTubeThumb(Context context, CollapsingToolbarLayout toolbar, ImageView imageView, String url) { Glide.with(context) .load(url) .listener(getPalette(Type.YOUTUBE, null, toolbar, null, url)) .transition(DrawableTransitionOptions.withCrossFade()) .apply(RequestOptions.centerCropTransform()) .into(imageView); }
public static void loadPoster(Context context, CardView card, ImageView imageView, String url) { Glide.with(context) .load(url) .listener(getPalette(Type.POSTER, null, null, card, url)) .transition(DrawableTransitionOptions.withCrossFade()) .apply(RequestOptions.centerCropTransform()) .into(imageView); }
public static void loadItemImg(Context context, MoviesAdapter.MoviesHolder holder, String url) { Glide.with(context) .load(url) .listener(getPalette(Type.ITEM, holder, null, null, url)) .transition(DrawableTransitionOptions.withCrossFade()) .apply(RequestOptions.centerCropTransform()) .into(holder.moviePosterImageView); }
@Override public void loadImageCall(String url, ImageView imageView, int placeholder, RequestListener<Drawable> listener) { Glide.with(imageView.getContext()) .load(url) .apply(new RequestOptions() .skipMemoryCache(true) .diskCacheStrategy(DiskCacheStrategy.RESOURCE) .error(R.drawable.ic_bottom_bar_more) .centerCrop() ) .listener(listener) .into(imageView); }
public void loadImage(final ImageView imageView, final AlbumItem albumItem) { Glide.with(imageView.getContext()) .asBitmap() .load(albumItem.getPath()) .listener(new RequestListener<Bitmap>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) { albumItem.error = true; return false; } @Override public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) { if (!albumItem.hasFadedIn) { fadeIn(); } else { imageView.clearColorFilter(); } return false; } }) .apply(albumItem.getGlideRequestOptions(imageView.getContext())) .into(imageView); }
/** * This method loads the image into ImageView using Glide. * * @param imageView CircularImageView * @param textImage TextView * @param imageUrl Image Url * @param placeholderImage The res id for the placeholder image */ private void loadImage(final CircleImageView imageView, final TextView textImage, String imageUrl, int placeholderImage) { RequestOptions options = new RequestOptions() .centerCrop() .placeholder(placeholderImage) .error(placeholderImage); Glide.with(context).load(imageUrl).apply(options).listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { if (textImage != null) { textImage.setVisibility(View.GONE); } imageView.setVisibility(View.VISIBLE); return false; } }).into(imageView); }
public static void bindGif(final GifViewHolder gifViewHolder, final ImageView imageView, final AlbumItem albumItem) { ViewCompat.setTransitionName(imageView, albumItem.getPath()); Context context = imageView.getContext(); Glide.with(context) .asGif() .load(albumItem.getUri(context)) .listener(new RequestListener<GifDrawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) { albumItem.error = true; return false; } @Override public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) { resource.start(); gifViewHolder.setAttacher(imageView); return false; } }) .apply(albumItem.getGlideRequestOptions(imageView.getContext())) .into(imageView); } }
@Override public void loadImage(Context context, String url, ImageView imageView, final View progressView) { Glide.with(context) .asBitmap() .load(url) .apply(new RequestOptions().fitCenter().error(R.mipmap.ic_launcher).placeholder(R.drawable.default_placeholder)) .listener(new RequestListener<Bitmap>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) { progressView.setVisibility(View.GONE); return false; } @Override public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) { progressView.setVisibility(View.GONE); return false; } }) .into(imageView); }
@Override public void displayImage(String url, String title) { if (image == null) return; Log.d("GetStripUrlListener", "url: " + url); if (url != null) { preferences.saveCurrentUrl(getArguments().getString(ARGUMENT_DATE), url); preferences.saveCurrentTitle(getArguments().getString(ARGUMENT_DATE), title); } Glide.with(DilbertFragment.this.getContext()) .asBitmap() .load(url) .apply(new RequestOptions().dontAnimate().fitCenter().diskCacheStrategy(DiskCacheStrategy.ALL).error(R.drawable.cancel)) .listener(dilbertImageLoadingListener) .into(image); Context c = getContext(); if (c != null) { LocalBroadcastManager.getInstance(c).sendBroadcast(new Intent(BROADCAST_TITLE_UPDATE)); } } };