[英]How to compress image in Glide on Android
I want show image in my application and for show images I use Glide library. 我想要在我的应用程序中显示图像,对于显示图像,我使用Glide库。
In my codes I can change image size but I want compress images. 在我的代码中,我可以更改图像大小,但是我想压缩图像。
Glide.with(context)
.load(model.get(position).getImageUrl2())
.placeholder(R.drawable.default_image)
.override(900, 600)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model,
Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
})
.into(holder.newsImage);
How can I compress images? 如何压缩图像?
Please help me 请帮我
Get your image 取得你的形象
Drawable drawable = getDrawable(R.drawable.bird);
Get the bitmap from drawable object 从可绘制对象获取位图
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
Initialize a new ByteArrayStream 初始化一个新的ByteArrayStream
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Compress the bitmap with JPEG format and quality 50% 以JPEG格式压缩位图,质量为50%
bitmap.compress(Bitmap.CompressFormat.JPEG,50,stream);
byte[] byteArray = stream.toByteArray();
Bitmap compressedBitmap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
Use the compressed Bitmap to load image. 使用压缩的位图加载图像。 I know its not what you want but i think it might helps you.
我知道这不是您想要的,但我认为它可以为您提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.