Java Code Examples for com.bumptech.glide.gifdecoder.GifDecoder#getFrameCount()

The following examples show how to use com.bumptech.glide.gifdecoder.GifDecoder#getFrameCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GifResourceEncoder.java    From giffun with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example 2
Source File: TransformationUtil.java    From LiTr with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Nullable
public static GlFilter createGlFilter(@NonNull Context context,
                                      @NonNull Uri overlayUri,
                                      @NonNull PointF size,
                                      @NonNull PointF position,
                                      float rotation) {
    GlFilter filter = null;

    try {
        if (TextUtils.equals(context.getContentResolver().getType(overlayUri), "image/gif")) {
            ContentResolver contentResolver = context.getApplicationContext().getContentResolver();
            InputStream inputStream = contentResolver.openInputStream(overlayUri);
            BitmapPool bitmapPool = new LruBitmapPool(10);
            GifBitmapProvider gifBitmapProvider = new GifBitmapProvider(bitmapPool);
            final GifDecoder gifDecoder = new StandardGifDecoder(gifBitmapProvider);
            gifDecoder.read(inputStream, (int) TranscoderUtils.getSize(context, overlayUri));

            AnimationFrameProvider animationFrameProvider = new AnimationFrameProvider() {
                @Override
                public int getFrameCount() {
                    return gifDecoder.getFrameCount();
                }

                @Nullable
                @Override
                public Bitmap getNextFrame() {
                    return gifDecoder.getNextFrame();
                }

                @Override
                public long getNextFrameDurationNs() {
                    return TimeUnit.MILLISECONDS.toNanos(gifDecoder.getNextDelay());
                }

                @Override
                public void advance() {
                    gifDecoder.advance();
                }
            };
            filter = new FrameSequenceAnimationOverlayFilter(animationFrameProvider, size, position, rotation);
        } else {
            filter = new BitmapOverlayFilter(context.getApplicationContext(), overlayUri, size, position, rotation);
        }
    } catch (IOException ex) {
        Log.e(TAG, "Failed to create a GlFilter", ex);
    }

    return filter;
}