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

4 人关注

我正在使用Glide库来上传图片。 在另一个应用程序中,我使用这样的代码

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);

通过声明.setOutputCompressQuality(50)来减少图像的大小。但我不知道如何在Glide Image库中做同样的事情。

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);
    
java
android
android-glide
Bir Nepali
Bir Nepali
发布于 2018-09-13
3 个回答
Son Truong
Son Truong
发布于 2020-04-20
已采纳
0 人赞同

From 格莱德官方网站 :

在默认情况下,Glide更倾向于使用RGB_565,因为它只需要两个字节 每个像素只需要两个字节(16位),因此它的内存占用只有更高质量和系统默认的ARGB_8888的一半。 高质量和系统默认的ARGB_8888。

所以你不能再降低位图的质量,但你有一些其他的选择。

Option 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

centerCrop()是一种裁剪技术,它可以缩放图像,使其填满ImageView要求的边界,然后裁剪多余的部分。 填充到ImageView的要求范围内,然后裁剪多余的部分。 ImageView将被完全填满,但整个图像可能无法显示。 不被显示。

RequestOptions myOptions = new RequestOptions()
    .centerCrop();
Glide.with(ctx)
    .asBitmap()
    .apply(myOptions)
    .load(url)
    .into(bitmapView);
  

fitCenter()是一种裁剪技术,它可以缩放图像,使两个尺寸都等于或小于要求的边界。 尺寸都等于或小于请求的 图像视图的边界。图像将被完全显示,但可能不会填满 整个ImageView。

RequestOptions myOptions = new RequestOptions()
        .fitCenter();
    Glide.with(ctx)
        .asBitmap()
        .apply(myOptions)
        .load(url)
        .into(bitmapView);

Option 3: Mix both of these solutions

RequestOptions myOptions = new RequestOptions()
    .fitCenter() // or centerCrop
    .override(100, 100);
 Glide.with(ctx)
     .asBitmap()
     .apply(myOptions)
     .load(url)
     .into(bitmapView);
    
Vincent Mungai
Vincent Mungai
发布于 2020-04-20
0 人赞同

Try this

Glide
    .with(ctx)
    .load(url)
    .apply(new RequestOptions().override(600, 200))
    .into(imageView);
    
Steve Cortis
Steve Cortis
发布于 2020-04-20
0 人赞同

我对此的解决方案使用了Kotlin的扩展函数。使用图片的宽度和高度,这使得它更有效率。在某些情况下,你可能需要在宽度和高度上加一个乘数,以获得更好的质量。

    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)