简体   繁体   English

如何使用 Glide for android 压缩和降低图像质量

[英]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 :使用centerCropfitCenter缩放图像

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.

相关问题 如何减少在android内存中滑动加载的图像大小 - How Reduce image size loaded with glide on memory in android Android:BitmapFactory.decodeByteArray-降低图像质量 - Android: BitmapFactory.decodeByteArray - reduce image quality Android 从相机意图压缩图像,体积小,质量好 - Android compress image with small size and good quality from camera intent 如何使用 Glide android 工作室延迟加载图像? - How to Lazy Load image using Glide android studio? 如何使用 Glide android 从 facebook 配置文件加载图像? - How to load an image from facebook profile using Glide android? 不使用Bitmap#compress()方法将JPEG图像压缩到特定质量级别 - Compress JPEG image to specific quality level without using Bitmap#compress() method 如何使用php或任何其他语言减小图像文件大小而又不损失质量或分辨率? - How to reduce Image file size without losing quality or resolution using php or any other language? 如何减小 pdf 中 png 图像的大小(在 pdf 中压缩 png) - how to reduce the size of png image in pdf (compress png in pdf) 如何使用 Glide 从 JSON 获取图像 - How to get Image from JSON using Glide 如何使用Glide [Android]下载多张图片? - How to download multiple images using Glide [Android]?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM