下面列出了怎么用com.bumptech.glide.load.resource.bitmap.Downsampler的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
AttachmentSecret attachmentSecret = AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret();
byte[] secret = attachmentSecret.getModernKey();
registry.prepend(File.class, File.class, UnitModelLoader.Factory.getInstance());
registry.prepend(InputStream.class, new EncryptedCacheEncoder(secret, glide.getArrayPool()));
registry.prepend(File.class, Bitmap.class, new EncryptedBitmapCacheDecoder(secret, new StreamBitmapDecoder(new Downsampler(registry.getImageHeaderParsers(), context.getResources().getDisplayMetrics(), glide.getBitmapPool(), glide.getArrayPool()), glide.getArrayPool())));
registry.prepend(File.class, GifDrawable.class, new EncryptedGifCacheDecoder(secret, new StreamGifDecoder(registry.getImageHeaderParsers(), new ByteBufferGifDecoder(context, registry.getImageHeaderParsers(), glide.getBitmapPool(), glide.getArrayPool()), glide.getArrayPool())));
registry.prepend(BlurHash.class, Bitmap.class, new BlurHashResourceDecoder());
registry.prepend(Bitmap.class, new EncryptedBitmapResourceEncoder(secret));
registry.prepend(GifDrawable.class, new EncryptedGifDrawableResourceEncoder(secret));
registry.append(ContactPhoto.class, InputStream.class, new ContactPhotoLoader.Factory(context));
registry.append(DecryptableUri.class, InputStream.class, new DecryptableStreamUriLoader.Factory(context));
registry.append(AttachmentModel.class, InputStream.class, new AttachmentStreamUriLoader.Factory());
registry.append(ChunkedImageUrl.class, InputStream.class, new ChunkedImageUrlLoader.Factory());
registry.append(StickerRemoteUri.class, InputStream.class, new StickerRemoteUriLoader.Factory());
registry.append(BlurHash.class, BlurHash.class, new BlurHashModelLoader.Factory());
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
}
private static <T> Bitmap createScaledBitmapInto(Context context, T model, int width, int height)
throws BitmapDecodingException
{
final Bitmap rough = Downsampler.AT_LEAST.decode(getInputStreamForModel(context, model),
Glide.get(context).getBitmapPool(),
width, height,
DecodeFormat.PREFER_RGB_565);
final Resource<Bitmap> resource = BitmapResource.obtain(rough, Glide.get(context).getBitmapPool());
final Resource<Bitmap> result = new FitCenter(context).transform(resource, width, height);
if (result == null) {
throw new BitmapDecodingException("unable to transform Bitmap");
}
return result.get();
}
public static <T> byte[] createScaledBytes(Context context, T model, MediaConstraints constraints)
throws BitmapDecodingException
{
CompressFormat compressFormat = CompressFormat.PNG;
int quality = 100;
int attempts = 0;
byte[] bytes;
Bitmap scaledBitmap = Downsampler.AT_MOST.decode(getInputStreamForModel(context, model),
Glide.get(context).getBitmapPool(),
constraints.getImageMaxWidth(context),
constraints.getImageMaxHeight(context),
DecodeFormat.PREFER_RGB_565);
if (scaledBitmap == null) {
throw new BitmapDecodingException("Unable to decode image");
}
try {
do {
attempts++;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
scaledBitmap.compress(compressFormat, quality, baos);
bytes = baos.toByteArray();
Log.w(TAG, "iteration with quality " + quality + "; size " + (bytes.length / 1024) + "kb; attempts " + attempts);
compressFormat = CompressFormat.JPEG;
if (quality > MAX_COMPRESSION_QUALITY) {
quality = MAX_COMPRESSION_QUALITY;
} else {
quality = quality - COMPRESSION_QUALITY_DECREASE;
}
}
while (bytes.length > constraints.getImageMaxSize(context));
return bytes;
} finally {
if (scaledBitmap != null) scaledBitmap.recycle();
}
}
/**
* Load images using the given {@link Downsampler}. Replaces any existing image decoder. Defaults to
* {@link Downsampler#AT_LEAST}. Will be ignored if the data represented by the model is a video. This replaces any
* previous calls to {@link #imageDecoder(ResourceDecoder)} and {@link #decoder(ResourceDecoder)} with default
* decoders with the appropriate options set.
*
* @see #imageDecoder
*
* @param downsampler The downsampler.
* @return This request builder.
*/
private BitmapRequestBuilder<ModelType, TranscodeType> downsample(Downsampler downsampler) {
this.downsampler = downsampler;
imageDecoder = new StreamBitmapDecoder(downsampler, bitmapPool, decodeFormat);
super.decoder(new ImageVideoBitmapDecoder(imageDecoder, videoDecoder));
return this;
}
/**
* Load images at a size near the size of the target using {@link Downsampler#AT_LEAST}.
*
* @see #downsample(Downsampler)
*
* @return This request builder.
*/
public BitmapRequestBuilder<ModelType, TranscodeType> approximate() {
return downsample(Downsampler.AT_LEAST);
}
/**
* Load images at their original size using {@link Downsampler#NONE}.
*
* @see #downsample(Downsampler)
*
* @return This request builder.
*/
public BitmapRequestBuilder<ModelType, TranscodeType> asIs() {
return downsample(Downsampler.NONE);
}
/**
* Load images at a size that is at most exactly as big as the target using
* {@link Downsampler#AT_MOST}.
*
* @see #downsample(Downsampler)
*
* @return This request builder.
*/
public BitmapRequestBuilder<ModelType, TranscodeType> atMost() {
return downsample(Downsampler.AT_MOST);
}