如何在Android上使用Glide获得原始图像尺寸? [英] How to get original image size by using Glide on Android?
问题描述
我正在从动态来源加载图像并将其加载到我的应用程序中.但是有时图像太小,在我的应用中看起来很糟糕.我要做的是获取图像大小,如果小于5x5,则根本不显示ImageView.
I am loading images from dynamic sources and loading them in my app. However sometimes the images are so small and looks bad in my app. What I want to do is get image size and if it is smaller than 5x5, don't show the ImageView at all.
如何实现?
当我使用sizeReadyCallback时,它返回ImageView的大小而不是图像.当我使用请求侦听器时,它返回0,0.
When I use sizeReadyCallback, it returns the size of ImageView instead of image. When I use request listener it returns 0,0.
Glide.with(getContext()).load(imageUrl).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) {
//This returns 0,0
Log.e("TAG","_width: " + resource.getBounds().width() + " _height:" +resource.getBounds().height());
return false;
}
}).into(ivImage).getSize(new SizeReadyCallback() {
@Override
public void onSizeReady(int width, int height) {
//This returns size of imageview.
Log.e("TAG","width: " + width + " height: " + height);
}
});
推荐答案
更新:
@TWiStErRob在评论中提供了一个更好的解决方案:更好的解决方案
A better solution has been provided by @TWiStErRob in comments: better solution
对于Glide v4:
For Glide v4:
Glide.with(getContext().getApplicationContext())
.asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap bitmap,
Transition<? super Bitmap> transition) {
int w = bitmap.getWidth();
int h = bitmap.getHeight()
mImageView.setImageBitmap(bitmap);
}
});
重点是在将位图设置为 ImageView
之前.
The point is getting the bitmap before set into an ImageView
.
这篇关于如何在Android上使用Glide获得原始图像尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!