[英]How to Compress and reduce Quality of image using Glide for android
I'm using Glide library to upload image.我正在使用 Glide 库上传图片。 In my another app I use this code在我的另一个应用程序中,我使用此代码
void imageButtonclick() {
iv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CropImage.activity(filePath).setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1,1).setOutputCompressQuality(50).start(UploadBook.this);
// Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//intent.putExtra(MediaStore.EXTRA_OUTPUT,imageuri);
//startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
to decrease size of image by declaring .setOutputCompressQuality(50)
But I don't know how to do same with Glide Image library .通过声明.setOutputCompressQuality(50)
来减小图像的大小但我不知道如何对 Glide Image library 做同样的事情。
My code is我的代码是
public static void loadLocalImage(Context ctx, RequestOptions glideRequests, Uri uri, ImageView imageView,
RequestListener listener) {
glideRequests.fitCenter()
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(ctx)
.applyDefaultRequestOptions(glideRequests.placeholder(R.drawable.ic_stub).error(R.drawable.ic_stub))
.asBitmap()
.load(uri)
.listener(listener)
.into(imageView);
}
From Glide official site :来自Glide 官方网站:
By default Glide prefers RGB_565 because it requires only two bytes per pixel (16 bit) and therefore has half the memory footprint of the higher quality and system default ARGB_8888.默认情况下,Glide 更喜欢 RGB_565,因为它每个像素只需要两个字节(16 位),因此内存占用是更高质量和系统默认 ARGB_8888 的一半。
So you cannot reduce the quality of a bitmap anymore, but you have some another options.所以你不能再降低位图的质量,但你有其他一些选择。
Option 1 : Resizing image size with override(x, y)
.选项 1 :使用override(x, y)
调整图像大小。
RequestOptions myOptions = new RequestOptions()
.override(100, 100);
Glide.with(fragment)
.asBitmap()
.apply(myOptions)
.load(url)
.into(bitmapView);
Option 2 : Scaling image with centerCrop
or fitCenter
选项 2 :使用centerCrop
或fitCenter
缩放图像
centerCrop() is a cropping technique that scales the image so that it fills the requested bounds of the ImageView and then crops the extra. centerCrop() 是一种裁剪技术,可以缩放图像,使其填充 ImageView 的请求边界,然后裁剪额外的边界。 The ImageView will be filled completely, but the entire image might not be displayed. ImageView 将被完全填充,但可能不会显示整个图像。
RequestOptions myOptions = new RequestOptions()
.centerCrop();
Glide.with(ctx)
.asBitmap()
.apply(myOptions)
.load(url)
.into(bitmapView);
fitCenter() is a cropping technique that scales the image so that both dimensions are equal to or less than the requested bounds of the ImageView. fitCenter() 是一种裁剪技术,可以缩放图像,使两个维度等于或小于 ImageView 的请求边界。 The image will be displayed completely, but might not fill the entire ImageView.图像将完整显示,但可能不会填满整个 ImageView。
RequestOptions myOptions = new RequestOptions()
.fitCenter();
Glide.with(ctx)
.asBitmap()
.apply(myOptions)
.load(url)
.into(bitmapView);
Option 3 : Mix both of these solutions选项 3 :混合这两种解决方案
RequestOptions myOptions = new RequestOptions()
.fitCenter() // or centerCrop
.override(100, 100);
Glide.with(ctx)
.asBitmap()
.apply(myOptions)
.load(url)
.into(bitmapView);
Try this尝试这个
Glide
.with(ctx)
.load(url)
.apply(new RequestOptions().override(600, 200))
.into(imageView);
My solution for this uses Kotlin's Extensions functions.我对此的解决方案使用 Kotlin 的扩展功能。 Using the width and height of the image which makes it more efficient.使用图像的宽度和高度使其更有效率。 In some cases you might need to add a multiplier to the width and height for better quality.在某些情况下,您可能需要为宽度和高度添加乘数以获得更好的质量。
fun ImageView.loadImage(uri: String) {
this.post {
val myOptions = RequestOptions()
.override(this.width, this.height)
.centerCrop()
Glide
.with(this.context)
.load(uri)
.apply(myOptions)
.into(this)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.