能否用Glide调整大小并保存图片到文件中呢?我正在使用以下代码:
Glide
.with(context)
.load(path)
.override(600, 200)
.centerCrop()
.into(imageViewResizeCenterCrop);
我该怎么做?
谢谢
能否用Glide调整大小并保存图片到文件中呢?我正在使用以下代码:
Glide
.with(context)
.load(path)
.override(600, 200)
.centerCrop()
.into(imageViewResizeCenterCrop);
我该怎么做?
谢谢
是的,这是可能的。我正在使用一个SimpleTarget
(Glide术语中的自定义目标)后代,我为此特定目的创建了它。它非常简单易用。这是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 width, 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));
Glide.with(this)
.load(path)
.bitmapTransform(new CropTransformation(this, 600, 200))
.into(imageView);
内部
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {`
在
保存位图
之前
return BitmapResource.obtain(bitmap, mBitmapPool);
查看所有转换这里
asBitmap()
调用。答案现已更新! - Loudenvier