@Before public void setUp() { drawable = mock(GifDrawable.class); resource = new GifDrawableResource(drawable); }
@Nullable private GifDrawableResource decode( ByteBuffer byteBuffer, int width, int height, GifHeaderParser parser, Options options) { long startTime = LogTime.getLogTime(); try { final GifHeader header = parser.parseHeader(); if (header.getNumFrames() <= 0 || header.getStatus() != GifDecoder.STATUS_OK) { // If we couldn't decode the GIF, we will end up with a frame count of 0. return null; } Bitmap.Config config = options.get(GifOptions.DECODE_FORMAT) == DecodeFormat.PREFER_RGB_565 ? Bitmap.Config.RGB_565 : Bitmap.Config.ARGB_8888; int sampleSize = getSampleSize(header, width, height); GifDecoder gifDecoder = gifDecoderFactory.build(provider, header, byteBuffer, sampleSize); gifDecoder.setDefaultBitmapConfig(config); gifDecoder.advance(); Bitmap firstFrame = gifDecoder.getNextFrame(); if (firstFrame == null) { return null; } Transformation<Bitmap> unitTransformation = UnitTransformation.get(); GifDrawable gifDrawable = new GifDrawable(context, gifDecoder, unitTransformation, width, height, firstFrame); return new GifDrawableResource(gifDrawable); } finally { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Decoded GIF from stream in " + LogTime.getElapsedMillis(startTime)); } } }
@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 GifDrawableResource decode(byte[] data, int width, int height, GifHeaderParser parser, GifDecoder decoder) { final GifHeader header = parser.parseHeader(); if (header.getNumFrames() <= 0 || header.getStatus() != GifDecoder.STATUS_OK) { // If we couldn't decode the GIF, we will end up with a frame count of 0. return null; } Bitmap firstFrame = decodeFirstFrame(decoder, header, data); if (firstFrame == null) { return null; } Transformation<Bitmap> unitTransformation = UnitTransformation.get(); GifDrawable gifDrawable = new GifDrawable(context, provider, bitmapPool, unitTransformation, width, height, header, data, firstFrame); return new GifDrawableResource(gifDrawable); }