private boolean encodeTransformedToFile(GifDrawable drawable, File file) { long startTime = LogTime.getLogTime(); OutputStream os = null; boolean success = false; try { os = new BufferedOutputStream(new FileOutputStream(file)); success = encodeTransformedToStream(drawable, os); os.close(); } catch (IOException e) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Failed to encode GIF", e); } } finally { if (os != null) { try { os.close(); } catch (IOException e) { // Ignored. } } } if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Re-encoded GIF with " + drawable.getFrameCount() + " frames and " + drawable.getBuffer().limit() + " bytes in " + LogTime.getElapsedMillis(startTime) + " ms"); } return success; }
@Test public void testReturnsFrameCountFromDecoder() { int expected = 4; when(frameLoader.getFrameCount()).thenReturn(expected); assertEquals(expected, drawable.getFrameCount()); }
@Override public void onFrameReady() { if (findCallback() == null) { stop(); invalidateSelf(); return; } invalidateSelf(); if (getFrameIndex() == getFrameCount() - 1) { loopCount++; } if (maxLoopCount != LOOP_FOREVER && loopCount >= maxLoopCount) { notifyAnimationEndToListeners(); stop(); } }
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; }