@Override public void initialize() { drawable.getFirstFrame().prepareToDraw(); } }
@Override public void initialize() { if (drawable instanceof BitmapDrawable) { ((BitmapDrawable) drawable).getBitmap().prepareToDraw(); } else if (drawable instanceof GifDrawable) { ((GifDrawable) drawable).getFirstFrame().prepareToDraw(); } } }
public static Bitmap getBitmap(GlideDrawable glideDrawable) { if (glideDrawable instanceof GlideBitmapDrawable) { return ((GlideBitmapDrawable) glideDrawable).getBitmap(); } else if (glideDrawable instanceof GifDrawable) { return ((GifDrawable) glideDrawable).getFirstFrame(); } return null; } }
@NonNull @Override public Resource<GifDrawable> transform( @NonNull Context context, @NonNull Resource<GifDrawable> resource, int outWidth, int outHeight) { GifDrawable drawable = resource.get(); // The drawable needs to be initialized with the correct width and height in order for a view // displaying it to end up with the right dimensions. Since our transformations may arbitrarily // modify the dimensions of our GIF, here we create a stand in for a frame and pass it to the // transformation to see what the final transformed dimensions will be so that our drawable can // report the correct intrinsic width and height. BitmapPool bitmapPool = Glide.get(context).getBitmapPool(); Bitmap firstFrame = drawable.getFirstFrame(); Resource<Bitmap> bitmapResource = new BitmapResource(firstFrame, bitmapPool); Resource<Bitmap> transformed = wrapped.transform(context, bitmapResource, outWidth, outHeight); if (!bitmapResource.equals(transformed)) { bitmapResource.recycle(); } Bitmap transformedFrame = transformed.get(); drawable.setFrameTransformation(wrapped, transformedFrame); return resource; }
@Test @SuppressWarnings("unchecked") public void testSetsTransformationAsFrameTransformation() { Resource<GifDrawable> resource = mockResource(); GifDrawable gifDrawable = mock(GifDrawable.class); Transformation<Bitmap> unitTransformation = UnitTransformation.get(); when(gifDrawable.getFrameTransformation()).thenReturn(unitTransformation); when(gifDrawable.getIntrinsicWidth()).thenReturn(500); when(gifDrawable.getIntrinsicHeight()).thenReturn(500); when(resource.get()).thenReturn(gifDrawable); Bitmap firstFrame = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(gifDrawable.getFirstFrame()).thenReturn(firstFrame); final int width = 123; final int height = 456; Bitmap expectedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Resource<Bitmap> expectedResource = mockResource(); when(expectedResource.get()).thenReturn(expectedBitmap); when(wrapped.transform(any(Context.class), Util.<Bitmap>anyResource(), anyInt(), anyInt())) .thenReturn(expectedResource); transformation.transform(context, resource, width, height); verify(gifDrawable).setFrameTransformation(isA(Transformation.class), eq(expectedBitmap)); }
@Override public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) { super.onResourceReady(resource, animation); if (!autoplayGifs) { resource.stop(); } BadgedFourThreeImageView badgedImageView = (BadgedFourThreeImageView) getView(); if (resource instanceof GlideBitmapDrawable) { Palette.from(((GlideBitmapDrawable) resource).getBitmap()) .clearFilters() .generate(this); } else if (resource instanceof GifDrawable) { Bitmap image = ((GifDrawable) resource).getFirstFrame(); Palette.from(image).clearFilters().generate(this); // 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)); } }
@Override public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) { super.onResourceReady(resource, animation); if (!autoplayGifs) { resource.stop(); } BadgedFourThreeImageView badgedImageView = (BadgedFourThreeImageView) getView(); if (resource instanceof GlideBitmapDrawable) { Palette.from(((GlideBitmapDrawable) resource).getBitmap()) .clearFilters() .generate(this); } else if (resource instanceof GifDrawable) { Bitmap image = ((GifDrawable) resource).getFirstFrame(); Palette.from(image).clearFilters().generate(this); // 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)); } }
@Override public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) { Drawable drawable = resource; if (resource instanceof GifDrawable) { isGif = true; if (!loadGif) { drawable = new BitmapDrawable(((GifDrawable) resource).getFirstFrame()); } } else { isGif = false; } initCornerIcon(); super.onResourceReady(drawable, transition); }
@Override public int getSize() { return drawable.getData().length + Util.getBitmapByteSize(drawable.getFirstFrame()); }
@Override public Resource<GifDrawable> transform(Resource<GifDrawable> resource, int outWidth, int outHeight) { GifDrawable drawable = resource.get(); // The drawable needs to be initialized with the correct width and height in order for a view displaying it // to end up with the right dimensions. Since our transformations may arbitrarily modify the dimensions of // our gif, here we create a stand in for a frame and pass it to the transformation to see what the final // transformed dimensions will be so that our drawable can report the correct intrinsic width and height. Bitmap firstFrame = resource.get().getFirstFrame(); Resource<Bitmap> bitmapResource = new BitmapResource(firstFrame, bitmapPool); Resource<Bitmap> transformed = wrapped.transform(bitmapResource, outWidth, outHeight); Bitmap transformedFrame = transformed.get(); if (!transformedFrame.equals(firstFrame)) { return new GifDrawableResource(new GifDrawable(drawable, transformedFrame, wrapped)); } else { return resource; } }
private GifBitmapWrapper decodeGifWrapper(InputStream bis, int width, int height) throws IOException { GifBitmapWrapper result = null; Resource<GifDrawable> gifResource = gifDecoder.decode(bis, width, height); if (gifResource != null) { GifDrawable drawable = gifResource.get(); // We can more efficiently hold Bitmaps in memory, so for static GIFs, try to return Bitmaps // instead. Returning a Bitmap incurs the cost of allocating the GifDrawable as well as the normal // Bitmap allocation, but since we can encode the Bitmap out as a JPEG, future decodes will be // efficient. if (drawable.getFrameCount() > 1) { result = new GifBitmapWrapper(null /*bitmapResource*/, gifResource); } else { Resource<Bitmap> bitmapResource = new BitmapResource(drawable.getFirstFrame(), bitmapPool); result = new GifBitmapWrapper(bitmapResource, null /*gifResource*/); } } return result; }
public static Bitmap getBitmap(GlideDrawable glideDrawable) { if (glideDrawable instanceof GlideBitmapDrawable) { return ((GlideBitmapDrawable) glideDrawable).getBitmap(); } else if (glideDrawable instanceof GifDrawable) { return ((GifDrawable) glideDrawable).getFirstFrame(); } return null; } }