Java Code Examples for com.bumptech.glide.load.engine.Resource#recycle()

The following examples show how to use com.bumptech.glide.load.engine.Resource#recycle() . 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: PaletteCacheDecoder.java    From glide-support with The Unlicense 6 votes vote down vote up
@Override public Resource<Palette> decode(InputStream source, int width, int height) throws IOException {
	if (!source.markSupported()) {
		source = new BufferedInputStream(source);
	}
	Log.d("PALETTE", "Decoding from cache");
	if (isBitmap(source)) {
		Log.d("PALETTE", "It's a cached bitmap");
		Resource<Bitmap> bitmap = bitmapDecoder.decode(source, width, height);
		try {
			Palette palette = new Palette.Builder(bitmap.get())
					.resizeBitmapArea(-1)
					.generate();
			Log.d("PALETTE", "Palette generated");
			return new SimpleResource<>(palette);
		} finally {
			bitmap.recycle();
		}
	} else {
		Log.d("PALETTE", "It's a cached palette");
		if (PaletteCacheEncoder.PALETTE_MAGIC_BYTE != source.read()) {
			throw new IOException("Cannot read palette magic.");
		}
		return paletteDecoder.decode(source, width, height);
	}
}
 
Example 2
Source File: GifResourceEncoder.java    From giffun with Apache License 2.0 5 votes vote down vote up
private Resource<Bitmap> getTransformedFrame(Bitmap currentFrame, Transformation<Bitmap> transformation,
        GifDrawable drawable) {
    // TODO: what if current frame is null?
    Resource<Bitmap> bitmapResource = factory.buildFrameResource(currentFrame, bitmapPool);
    Resource<Bitmap> transformedResource = transformation.transform(bitmapResource,
            drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    if (!bitmapResource.equals(transformedResource)) {
        bitmapResource.recycle();
    }
    return transformedResource;
}
 
Example 3
Source File: BitmapBytesTranscoder.java    From giffun with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<byte[]> transcode(Resource<Bitmap> toTranscode) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    toTranscode.get().compress(compressFormat, quality, os);
    toTranscode.recycle();
    return new BytesResource(os.toByteArray());
}
 
Example 4
Source File: GifBitmapWrapperResource.java    From giffun with Apache License 2.0 5 votes vote down vote up
@Override
public void recycle() {
    Resource<Bitmap> bitmapResource = data.getBitmapResource();
    if (bitmapResource != null) {
        bitmapResource.recycle();
    }
    Resource<GifDrawable> gifDataResource = data.getGifResource();
    if (gifDataResource != null) {
        gifDataResource.recycle();
    }
}
 
Example 5
Source File: MultiTransformation.java    From giffun with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<T> transform(Resource<T> resource, int outWidth, int outHeight) {
    Resource<T> previous = resource;

    for (Transformation<T> transformation : transformations) {
        Resource<T> transformed = transformation.transform(previous, outWidth, outHeight);
        if (previous != null && !previous.equals(resource) && !previous.equals(transformed)) {
            previous.recycle();
        }
        previous = transformed;
    }
    return previous;
}
 
Example 6
Source File: TestFragment.java    From glide-support with The Unlicense 5 votes vote down vote up
private BitmapDrawable transformDrawable(Drawable drawable, Transformation<Bitmap> transform, int size) {
	// render original
	Bitmap bitmap = Bitmap.createBitmap(size, size, Config.ARGB_8888);
	Canvas canvas = new Canvas(bitmap);
	drawable.setBounds(0, 0, size, size);
	drawable.draw(canvas);
	// make rounded
	Resource<Bitmap> original = BitmapResource.obtain(bitmap, Glide.get(getContext()).getBitmapPool());
	Resource<Bitmap> rounded = transform.transform(original, size, size);
	if (!original.equals(rounded)) {
		original.recycle();
	}
	return new BitmapDrawable(getResources(), rounded.get());
}
 
Example 7
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;
}