android – 使用Glide保存已调整大小的图像

前端之家收集整理的这篇文章主要介绍了android – 使用Glide保存已调整大小的图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否可以通过在文件中滑动来保存调整大小的图像?
我正在使用此代码
Glide
    .with(context)
    .load(path)
    .override(600,200) 
    .centerCrop() 
    .into(imageViewResizeCenterCrop);

我该怎么办?

谢谢

解决方法

是的,这是可能的.我正在使用我为此特定目的创建的SimpleTarget(在Glide用语中为 custom target)后代.它使用起来非常简单.这是目标代码
import android.graphics.Bitmap;

import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.io.FileOutputStream;
import java.io.IOException;

public class FileTarget extends SimpleTarget<Bitmap> {
    public FileTarget(String fileName,int width,int height) {
        this(fileName,width,height,Bitmap.CompressFormat.JPEG,70);
    }
    public FileTarget(String fileName,int height,Bitmap.CompressFormat format,int quality) {
        super(width,height);
        this.fileName = fileName;
        this.format = format;
        this.quality = quality;
    }
    String fileName;
    Bitmap.CompressFormat format;
    int quality;
    public void onResourceReady(Bitmap bitmap,GlideAnimation anim) {
            try {
                FileOutputStream out = new FileOutputStream(fileName);
                bitmap.compress(format,quality,out);
                out.flush();
                out.close();
                onFileSaved();
            } catch (IOException e) {
                e.printStackTrace();
                onSaveException(e);
            }
    }
    public void onFileSaved() {
        // do nothing,should be overriden (optional)
    }
    public void onSaveException(Exception e) {
        // do nothing,should be overriden (optional)
    }

}

以下是您在自己的示例中使用它的方法

Glide
    .with(context)
    .load(path)
    .asBitmap()
    .centerCrop()
    .into(new FileTarget(pathToDestination,600,200));

代码不会在任何视图上显示图像,而是将其直接保存到目标.

原文链接:https://www.f2er.com/android/309158.html

猜你在找的Android相关文章