@Override
    public boolean encode(Resource<GifDrawable> resource, OutputStream os) {
        long startTime = LogTime.getLogTime();

        GifDrawable drawable = resource.get();
        Transformation<Bitmap> transformation = drawable.getFrameTransformation();
        if (transformation instanceof UnitTransformation) {
            return writeDataDirect(drawable.getData(), os);
        }

        GifDecoder decoder = decodeHeaders(drawable.getData());

        AnimatedGifEncoder encoder = factory.buildEncoder();
        if (!encoder.start(os)) {
            return false;
        }

        for (int i = 0; i < decoder.getFrameCount(); i++) {
            Bitmap currentFrame = decoder.getNextFrame();
            Resource<Bitmap> transformedResource = getTransformedFrame(currentFrame, transformation, drawable);
            try {
                if (!encoder.addFrame(transformedResource.get())) {
                    return false;
                }
                int currentFrameIndex = decoder.getCurrentFrameIndex();
                int delay = decoder.getDelay(currentFrameIndex);
                encoder.setDelay(delay);

                decoder.advance();
            } finally {
                transformedResource.recycle();
            }
        }

        boolean result = encoder.finish();

        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "Encoded gif with " + decoder.getFrameCount() + " frames and " + drawable.getData().length
                    + " bytes in " + LogTime.getElapsedMillis(startTime) + " ms");
        }

        return result;
    }
An {@link ResourceEncoder} that can write
{@link GifDrawable} to cache.
core/src/main/java/com/bumptech/glide/load/resource/gif/GifResourceEncoder.java