private void loadImageView() { final String url = getArguments().getString(ARGUMENTS_IMAGE); Glide.with(this).downloadOnly().load(url) /* todo replace error icon */ .apply(new RequestOptions().error(R.mipmap.qq_refresh_success)) .into(new SimpleTarget<File>() { @Override public void onResourceReady(File resource, Transition<? super File> transition) { mImageView.setImage(ImageSource.uri(Uri.fromFile(resource))); } @Override public void onLoadFailed(@Nullable Drawable errorDrawable) { super.onLoadFailed(errorDrawable); } }); }
@Override public void onGranted() { Glide.with(context).load(url).into(new SimpleTarget<Drawable>() { @Override public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) { String path = String.format("%s/Android/data/%s/", Environment.getExternalStorageDirectory(), AppUtils.getAppPackageName()); String filename = TimeUtils.getNowString() + ".png"; boolean ok = ImageUtils.save(ImageUtils.drawable2Bitmap(resource), path + filename, Bitmap.CompressFormat.PNG); if (ok) { ToastUtils.showShort(R.string.success); } else { ToastUtils.showShort(R.string.error); } } }); }
/** * Internal {@link #onResourceReady(Resource, DataSource)} where arguments are known to be safe. * * @param resource original {@link Resource}, never <code>null</code> * @param result object returned by {@link Resource#get()}, checked for type and never * <code>null</code> */ private void onResourceReady(Resource<R> resource, R result, DataSource dataSource) { // We must call isFirstReadyResource before setting status. boolean isFirstResource = isFirstReadyResource(); status = Status.COMPLETE; this.resource = resource; if (glideContext.getLogLevel() <= Log.DEBUG) { Log.d(GLIDE_TAG, "Finished loading " + result.getClass().getSimpleName() + " from " + dataSource + " for " + model + " with size [" + width + "x" + height + "] in " + LogTime.getElapsedMillis(startTime) + " ms"); } if (requestListener == null || !requestListener.onResourceReady(result, model, target, dataSource, isFirstResource)) { Transition<? super R> animation = animationFactory.build(dataSource, isFirstResource); target.onResourceReady(result, animation); } notifyLoadSuccess(); }
@Test public void testClone() throws IOException { Target<Drawable> firstTarget = mock(Target.class); doAnswer(new CallSizeReady(100, 100)).when(firstTarget).getSize(isA(SizeReadyCallback.class)); Target<Drawable> secondTarget = mock(Target.class); doAnswer(new CallSizeReady(100, 100)).when(secondTarget).getSize(isA(SizeReadyCallback.class)); RequestBuilder<Drawable> firstRequest = requestManager .load(mockUri("content://first")); firstRequest.into(firstTarget); firstRequest.clone() .apply(placeholderOf(new ColorDrawable(Color.RED))) .into(secondTarget); verify(firstTarget).onResourceReady(isA(Drawable.class), isA(Transition.class)); verify(secondTarget).onResourceReady(notNull(Drawable.class), isA(Transition.class)); }
@Test public void testLoadColorDrawable_withUnitBitmapTransformation_returnsColorDrawable() { ColorDrawable colorDrawable = new ColorDrawable(Color.RED); requestManager .load(colorDrawable) .apply(new RequestOptions() .override(100, 100) .centerCrop()) .into(target); ArgumentCaptor<Object> argumentCaptor = ArgumentCaptor.forClass(Object.class); verify(target).onResourceReady(argumentCaptor.capture(), isA(Transition.class)); Object result = argumentCaptor.getValue(); assertThat(result).isInstanceOf(ColorDrawable.class); assertThat(((ColorDrawable) result).getColor()).isEqualTo(Color.RED); }
@Test public void testLoadColorDrawable_withNonUnitBitmapTransformation_returnsBitmapDrawable() { ColorDrawable colorDrawable = new ColorDrawable(Color.RED); requestManager .load(colorDrawable) .apply(new RequestOptions() .override(100, 100) .circleCrop()) .into(target); ArgumentCaptor<Object> argumentCaptor = ArgumentCaptor.forClass(Object.class); verify(target).onResourceReady(argumentCaptor.capture(), isA(Transition.class)); Object result = argumentCaptor.getValue(); assertThat(result).isInstanceOf(BitmapDrawable.class); Bitmap bitmap = ((BitmapDrawable) result).getBitmap(); assertThat(bitmap.getWidth()).isEqualTo(100); assertThat(bitmap.getHeight()).isEqualTo(100); }
@Test public void removeFromManagers_afterRequestManagerRemoved_clearsRequest() { target = requestManager.load(mockUri("content://uri")).into(new SimpleTarget<Drawable>() { @Override public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) { // Do nothing. } }); Request request = Preconditions.checkNotNull(target.getRequest()); requestManager.onDestroy(); requestManager.clear(target); assertThat(target.getRequest()).isNull(); assertThat(request.isCancelled()).isTrue(); }
private void changeViewState(){ imageName.setText(mainObject.getFileName()); if (image.getDrawable() == null) { Glide.with(parentActivity) .load(mainObject.getPath()) .into(new DrawableImageViewTarget(image) { @Override public void onResourceReady(Drawable resource, @Nullable Transition<? super Drawable> transition) { super.onResourceReady(resource, transition); circleProgress.setVisibility(View.GONE); mainObject.setState(ImageState.unselected); } }); } }
@Override public void onResume(){ super.onResume(); if(mUserService.isLoggedIn()){ mUserService.me() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(u -> { userName.setText(u.getUsername()); mAvatarView.setAvatarOfUser(u); GlideApp.with(this) .load(NodeBBService.url(u.getCoverUrl())) .into(new SimpleTarget<Drawable>() { @Override public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) { mHeaderView.setBackground(resource); } }); }); } }
private void getUserProfile() { mUserService.me() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(u -> { mUserName.setText(u.getUsername()); mAvatarView.setAvatarOfUser(u); mReputation.setText(u.getReputation()); mPost.setText(u.getPostcount()); mFollowing.setText(String.valueOf(u.getFollowingCount())); mFollower.setText(String.valueOf(u.getFollowerCount())); mLoginTime.setText(DateTimeFormatter.format(Long.parseLong(u.getLastonline()))); mRegTime.setText(DateTimeFormatter.format(Long.parseLong(u.getJoindate()))); mProfileViews.setText(u.getProfileviews()); mEmail.setText(u.getEmail()); GlideApp.with(UserProfileActivity.this) .load(NodeBBService.url(u.getCoverUrl())) .into(new SimpleTarget<Drawable>() { @Override public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) { mHeaderView.setBackground(resource); } }); }); }
@Override public void onResourceReady(final Bitmap resource, Transition<? super Bitmap> transition) { try { final ViewTreeObserver observer = getView().getViewTreeObserver(); observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { //final Bitmap scaledBitmap = Bitmap.createScaledBitmap(resource, mPhotoContainer.getWidth(), mPhotoContainer.getHeight(), true); mPhoto.setImageBitmap(resource); mPhoto.invalidate(); mPhoto.setVisibility(View.VISIBLE); mPhotoNoImage.setVisibility(View.GONE); return true; } }); } catch (Exception ex) { Log.e(getClass().getSimpleName(),ex.getMessage()); } }
private void renderImage(Object image, final ImageView imageView){ GlideApp.with(mContext) .asBitmap() .load(image) .placeholder(R.drawable.iconerror) .error(R.drawable.iconerror) .into(new SimpleTarget<Bitmap>() { @Override public void onResourceReady(final Bitmap resource, Transition<? super Bitmap> transition) { int valueWidth = (int) mContext.getResources().getDimension(R.dimen.book_cover_width); int valueHeight = (int) mContext.getResources().getDimension(R.dimen.book_cover_height); final Bitmap scaledBitmap = Bitmap.createScaledBitmap(resource, valueWidth, valueHeight, true); imageView.setImageBitmap(resource); imageView.invalidate(); } }); }
@Override public void onResourceReady(Drawable resource, @Nullable Transition<? super Drawable> transition) { super.onResourceReady(resource, transition); BadgedFourThreeImageView badgedImageView = (BadgedFourThreeImageView) getView(); if (resource instanceof GifDrawable) { Bitmap image = ((GifDrawable) resource).getFirstFrame(); if (image != null) { // look at the corner to determine the gif badge color int cornerSize = (int) (56 * getView().getContext().getResources().getDisplayMetrics ().scaledDensity); Bitmap corner = Bitmap.createBitmap(image, image.getWidth() - cornerSize, image.getHeight() - cornerSize, cornerSize, cornerSize); boolean isDark = ColorUtils.isDark(corner); corner.recycle(); badgedImageView.setBadgeColor(ContextCompat.getColor(getView().getContext(), isDark ? R.color.gif_badge_dark_image : R.color.gif_badge_light_image)); } else { badgedImageView.setBadgeColor(ContextCompat.getColor(getView().getContext(), R.color.gif_badge_light_image)); } } }
@Override public void onBindViewHolder(final ContributorAdapter.ViewHolder holder, int position) { ContributorData contributor = contributors.get(position); Glide.with(holder.imageView.getContext()).asBitmap().load(contributor.imageUrl).into(new SimpleTarget<Bitmap>() { @Override public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) { holder.imageView.setImageBitmap(resource); } }); holder.textView.setText(contributor.name); holder.itemView.setTag(contributor); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (view.getTag() != null && view.getTag() instanceof ContributorData) view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(((ContributorData) view.getTag()).url))); } }); }
@Override public void loadArtWork(long artistId) { LogUtils.i(TAG, "loadArtWork"); if (mView != null) { Glide.with(mView.getContext()).asBitmap().load(String.valueOf(artistId)) .into(new SimpleTarget<Bitmap>() { @Override public void onLoadFailed(Drawable errorDrawable) { super.onLoadFailed(errorDrawable); if (mView != null) { mView.showArtwork(errorDrawable); } } @Override public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) { if (mView != null) { mView.showArtwork(resource); } } }); } }
@Override public void loadAlbum(long albumID) { if (mView != null) { Glide.with(mView.getContext()).asBitmap() .load(Utils.getAlbumArtUri(albumID)) .into(new SimpleTarget<Bitmap>() { @Override public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) { if (mView != null) { mView.showAlbum(resource); } } @Override public void onLoadFailed(Drawable errorDrawable) { super.onLoadFailed(errorDrawable); if (mView != null) { mView.showAlbum(errorDrawable); } } }); } }
private void setCoverImage(User user) { if (user == null || TextUtils.isEmpty(user.getCoverUrl()) || user.getCoverUrl().equals("/assets/images/cover-default.png")) { mDefaultCover.setVisibility(View.VISIBLE); mShadow.setVisibility(View.GONE); mHeaderView.setBackgroundColor(ThemeColorManagerCompat.getColorPrimary()); } else { mDefaultCover.setVisibility(View.GONE); mShadow.setVisibility(View.VISIBLE); GlideApp.with(getContext()) .load(NodeBB.BASE_URL + user.getCoverUrl()) .diskCacheStrategy(DiskCacheStrategy.NONE) .into(new SimpleTarget<Drawable>() { @Override public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) { mHeaderView.setBackground(resource); } }); } }
/** * A callback that should never be invoked directly. */ @Override public synchronized void onResourceReady(R resource, Transition<? super R> transition) { // We might get a null result. resultReceived = true; this.resource = resource; waiter.notifyAll(this); }
@Override public void load(String url, final ImageView iv) { Glide.with(iv.getContext()) .load(url) .into(new SimpleTarget<Drawable>(200, 200) { @Override public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) { iv.setImageDrawable(resource); } }); }
private void runTestFileDefaultLoader() { File file = new File("fake"); mockUri(Uri.fromFile(file)); requestManager.load(file).into(target); requestManager.load(file).into(imageView); verify(target).onResourceReady(isA(BitmapDrawable.class), isA(Transition.class)); verify(target).setRequest((Request) notNull()); assertNotNull(imageView.getDrawable()); }
@Test public void testUrlDefaultLoader() throws MalformedURLException { URL url = new URL("http://www.google.com"); requestManager.load(url).into(target); requestManager.load(url).into(imageView); verify(target).onResourceReady(isA(BitmapDrawable.class), isA(Transition.class)); verify(target).setRequest((Request) notNull()); assertNotNull(imageView.getDrawable()); }
@Test public void testAsBitmapOption() { Uri uri = Uri.parse("content://something/else"); mockUri(uri); requestManager.asBitmap().load(uri).into(target); verify(target).onResourceReady(isA(Bitmap.class), isA(Transition.class)); }
@Test public void testToBytesOption() { Uri uri = Uri.parse("content://something/else"); mockUri(uri); requestManager.as(byte[].class).apply(decodeTypeOf(Bitmap.class)).load(uri).into(target); verify(target).onResourceReady(isA(byte[].class), isA(Transition.class)); }
private void runTestUriDefaultLoader() { Uri uri = Uri.parse("content://test/something"); mockUri(uri); requestManager.load(uri).into(target); requestManager.load(uri).into(imageView); verify(target).onResourceReady(notNull(), isA(Transition.class)); verify(target).setRequest((Request) notNull()); assertNotNull(imageView.getDrawable()); }
private void runTestIntegerDefaultLoader() { int integer = android.R.drawable.star_on; mockUri("android.resource://" + "android" + "/drawable/star_on"); requestManager.load(integer).into(target); requestManager.load(integer).into(imageView); verify(target).onResourceReady(isA(BitmapDrawable.class), isA(Transition.class)); verify(target).setRequest((Request) notNull()); assertNotNull(imageView.getDrawable()); }
@Test public void testByteArrayDefaultLoader() { byte[] bytes = new byte[10]; requestManager.load(bytes).into(target); requestManager.load(bytes).into(imageView); verify(target).onResourceReady(isA(BitmapDrawable.class), isA(Transition.class)); verify(target).setRequest((Request) notNull()); assertNotNull(imageView.getDrawable()); }
@Test public void testReceivesGif() throws IOException { String fakeUri = "content://fake"; InputStream testGifData = openResource("test.gif"); mockUri(Uri.parse(fakeUri), testGifData); requestManager.asGif().load(fakeUri).into(target); verify(target).onResourceReady(isA(GifDrawable.class), isA(Transition.class)); }
@Test public void testReceivesGifBytes() throws IOException { String fakeUri = "content://fake"; InputStream testGifData = openResource("test.gif"); mockUri(Uri.parse(fakeUri), testGifData); requestManager.as(byte[].class).apply(decodeTypeOf(GifDrawable.class)).load(fakeUri) .into(target); verify(target).onResourceReady(isA(byte[].class), isA(Transition.class)); }
@Test public void testReceivesBitmapBytes() { String fakeUri = "content://fake"; mockUri(fakeUri); requestManager.as(byte[].class).apply(decodeTypeOf(Bitmap.class)).load(fakeUri).into(target); verify(target).onResourceReady(isA(byte[].class), isA(Transition.class)); }
@Test public void testReceivesThumbnails() { String full = mockUri("content://full"); String thumb = mockUri("content://thumb"); requestManager .load(full) .thumbnail(requestManager.load(thumb)) .into(target); verify(target, times(2)).onResourceReady(isA(Drawable.class), isA(Transition.class)); }
@Test public void testReceivesRecursiveThumbnails() { requestManager.load(mockUri("content://first")).thumbnail( requestManager.load(mockUri("content://second")).thumbnail( requestManager.load(mockUri("content://third")).thumbnail( requestManager.load(mockUri("content://fourth"))))) .into(target); verify(target, times(4)).onResourceReady(isA(Drawable.class), isA(Transition.class)); }
@Test public void testReceivesRecursiveThumbnailWithPercentage() { requestManager.load(mockUri("content://first")) .thumbnail(requestManager.load(mockUri("content://second")).thumbnail(0.5f)) .into(target); verify(target, times(3)).onResourceReady(isA(Drawable.class), isA(Transition.class)); }
@Test public void testTargetIsCalledWithAnimationFromFactory() { SingleRequest<List> request = harness.getRequest(); Transition<List> transition = mockTransition(); when(harness.factory.build(any(DataSource.class), anyBoolean())).thenReturn(transition); request.onResourceReady(harness.resource, DataSource.DATA_DISK_CACHE); verify(harness.target).onResourceReady(eq(harness.result), eq(transition)); }
@Override public void onResourceReady(T resource, Transition<? super T> transition) { gestureImageView.setImageDrawable(resource); if (resource instanceof GifDrawable) { ((GifDrawable) resource).start(); } swipeRefreshLayout.setRefreshing(false); }
private SimpleTarget<Object> getTarget() { return new SimpleTarget<Object>() { @Override public void onResourceReady(Object resource, Transition<? super Object> transition) { // Do nothing. } }; }
private SimpleTarget<Object> getTarget(int width, int height) { return new SimpleTarget<Object>(width, height) { @Override public void onResourceReady(Object resource, Transition<? super Object> transition) { // Do nothing. } }; }
@Test public void testSetsDrawableOnViewInOnResourceReadyWhenAnimationReturnsFalse() { @SuppressWarnings("unchecked") Transition<Drawable> animation = mock(Transition.class); when(animation.transition(any(Drawable.class), eq(target))).thenReturn(false); Drawable resource = new ColorDrawable(Color.GRAY); target.onResourceReady(resource, animation); assertEquals(resource, target.resource); }
@Test public void testDoesNotSetDrawableOnViewInOnResourceReadyWhenAnimationReturnsTrue() { Drawable resource = new ColorDrawable(Color.RED); @SuppressWarnings("unchecked") Transition<Drawable> animation = mock(Transition.class); when(animation.transition(eq(resource), eq(target))).thenReturn(true); target.onResourceReady(resource, animation); assertNull(target.resource); }
@Test public void testProvidesCurrentPlaceholderToAnimationIfPresent() { Drawable placeholder = new ColorDrawable(Color.BLACK); view.setImageDrawable(placeholder); @SuppressWarnings("unchecked") Transition<Drawable> animation = mock(Transition.class); target.onResourceReady(new ColorDrawable(Color.GREEN), animation); verify(animation).transition(eq(placeholder), eq(target)); }