/** * Returns a {@link DrawableTransitionOptions} object that enables a cross fade animation. * * @see #crossFade() */ @NonNull public static DrawableTransitionOptions withCrossFade() { return new DrawableTransitionOptions().crossFade(); }
/** * See {@code getDrawable(Context, int, Theme)}. */ public static Drawable getDrawable( Context ourContext, Context targetContext, @DrawableRes int id) { return getDrawable(ourContext, targetContext, id, /*theme=*/ null); }
@Nullable @Override public Resource<Drawable> decode(@NonNull Uri source, int width, int height, @NonNull Options options) { String packageName = source.getAuthority(); Context targetContext = findContextForPackage(source, packageName); @DrawableRes int resId = findResourceIdFromUri(targetContext, source); // We can't get a theme from another application. Drawable drawable = DrawableDecoderCompat.getDrawable(context, targetContext, resId); return NonOwnedDrawableResource.newInstance(drawable); }
/** * Returns a {@link DrawableTransitionOptions} object that uses the given transition factory. * * @see com.bumptech.glide.GenericTransitionOptions#with(TransitionFactory) */ @NonNull public static DrawableTransitionOptions with( @NonNull TransitionFactory<Drawable> transitionFactory) { return new DrawableTransitionOptions().transition(transitionFactory); }
/** * Enables a cross fade animation between both the placeholder and the first resource and between * subsequent resources (if thumbnails are used). */ @NonNull public DrawableTransitionOptions crossFade() { return crossFade(new DrawableCrossFadeFactory.Builder()); }
@Test public void getDefaultTransitionOptions_withSuperClassRegistered_returnsSuperClassOptions() { DrawableTransitionOptions expected = new DrawableTransitionOptions(); transitionOptions.put(Drawable.class, expected); assertThat(context.getDefaultTransitionOptions(BitmapDrawable.class)) .isEqualTo(expected); assertThat(context.getDefaultTransitionOptions(GifDrawable.class)) .isEqualTo(expected); } }
@Test public void testDoesNotReturnOriginalDrawableOnGet() { when(drawable.getConstantState()).thenReturn(mock(Drawable.ConstantState.class)); assertNotEquals(drawable, resource.get()); }
/** * Enables a cross fade animation between both the placeholder and the first resource and between * subsequent resources (if thumbnails are used). */ @NonNull public DrawableTransitionOptions crossFade( @NonNull DrawableCrossFadeFactory drawableCrossFadeFactory) { return transition(drawableCrossFadeFactory); }
@DrawableRes private int findResourceIdFromUri(Context context, Uri source) { List<String> segments = source.getPathSegments(); if (segments.size() == NAME_URI_PATH_SEGMENTS) { return findResourceIdFromTypeAndNameResourceUri(context, source); } else if (segments.size() == ID_PATH_SEGMENTS) { return findResourceIdFromResourceIdUri(source); } else { throw new IllegalArgumentException("Unrecognized Uri format: " + source); } }
@SuppressWarnings("unchecked") @Nullable static Resource<Drawable> newInstance(@Nullable Drawable drawable) { return drawable != null ? new NonOwnedDrawableResource(drawable) : null; }
@Nullable @Override public Resource<Drawable> decode(@NonNull Drawable source, int width, int height, @NonNull Options options) { return NonOwnedDrawableResource.newInstance(source); } }
@Nullable @Override public Resource<Bitmap> decode(@NonNull Uri source, int width, int height, @NonNull Options options) { Resource<Drawable> drawableResource = drawableDecoder.decode(source, width, height, options); if (drawableResource == null) { return null; } Drawable drawable = drawableResource.get(); return DrawableToBitmapConverter.convert(bitmapPool, drawable, width, height); } }
/** * Returns a {@link DrawableTransitionOptions} object that enables a cross fade animation. * * @see #crossFade(int) */ @NonNull public static DrawableTransitionOptions withCrossFade(int duration) { return new DrawableTransitionOptions().crossFade(duration); }
/** * Enables a cross fade animation between both the placeholder and the first resource and between * subsequent resources (if thumbnails are used). * * @param duration The duration of the animation, see * {@code DrawableCrossFadeFactory.Builder(int)} * @see com.bumptech.glide.request.transition.DrawableCrossFadeFactory.Builder */ @NonNull public DrawableTransitionOptions crossFade(int duration) { return crossFade(new DrawableCrossFadeFactory.Builder(duration)); }
@Test public void get_withNullState_returnsOriginalDrawable() { when(drawable.getConstantState()).thenReturn(null); assertThat(resource.get()).isEqualTo(drawable); }
private Drawable loadDrawable(@DrawableRes int resourceId) { Theme theme = requestOptions.getTheme() != null ? requestOptions.getTheme() : context.getTheme(); return DrawableDecoderCompat.getDrawable(glideContext, resourceId, theme); }
/** * Returns a {@link DrawableTransitionOptions} object that enables a cross fade animation. * * @see #crossFade(DrawableCrossFadeFactory) */ @NonNull public static DrawableTransitionOptions withCrossFade( @NonNull DrawableCrossFadeFactory drawableCrossFadeFactory) { return new DrawableTransitionOptions().crossFade(drawableCrossFadeFactory); }
/** * Enables a cross fade animation between both the placeholder and the first resource and between * subsequent resources (if thumbnails are used). */ @NonNull public DrawableTransitionOptions crossFade(@NonNull DrawableCrossFadeFactory.Builder builder) { return crossFade(builder.build()); } }
@Test public void testReturnsNewDrawableOnGet() { GifDrawable expected = mock(GifDrawable.class); Drawable.ConstantState constantState = mock(Drawable.ConstantState.class); when(constantState.newDrawable()).thenReturn(expected); when(drawable.getConstantState()).thenReturn(constantState); assertThat(resource.get()).isEqualTo(expected); verify(drawable).getConstantState(); verify(constantState).newDrawable(); }
/** * Returns a {@link DrawableTransitionOptions} object that enables a cross fade animation. * * @see #crossFade(DrawableCrossFadeFactory.Builder) */ @NonNull public static DrawableTransitionOptions withCrossFade( @NonNull DrawableCrossFadeFactory.Builder builder) { return new DrawableTransitionOptions().crossFade(builder); }