有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用Glide for 安卓压缩和降低图像质量

我正在使用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图像库

我的密码是

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

    }

共 (3) 个答案

  1. # 1 楼答案

    我的解决方案使用了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)
    
            }
    
        }
    
  2. # 2 楼答案

    Glide official site

    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.

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

    选项1:使用^{调整图像大小

    RequestOptions myOptions = new RequestOptions()
        .override(100, 100);
    
    Glide.with(fragment)
        .asBitmap()
        .apply(myOptions)
        .load(url)
        .into(bitmapView);
    

    选项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. The ImageView will be filled completely, but the entire image might not be displayed.

    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. The image will be displayed completely, but might not fill the entire ImageView.

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

    选项3:混合这两种解决方案

    RequestOptions myOptions = new RequestOptions()
        .fitCenter() // or centerCrop
        .override(100, 100);
    
     Glide.with(ctx)
         .asBitmap()
         .apply(myOptions)
         .load(url)
         .into(bitmapView);
    
  3. # 3 楼答案

    试试这个

    Glide
        .with(ctx)
        .load(url)
        .apply(new RequestOptions().override(600, 200))
        .into(imageView);