我正在从动态来源加载图像并将它们加载到我的应用程序中。但是有时图像很小,在我的应用程序中看起来很糟糕。我想要做的是获取图像大小,如果它小于 5x5,则根本不显示 ImageView。
如何实现?
当我使用 sizeReadyCallback 时,它返回 ImageView 的大小而不是图像。当我使用请求监听器时,它返回 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 在评论中提供了更好的解决方案: better solution
对于 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
之前获取位图。
关于android - 如何在 Android 上使用 Glide 获取原始图像大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47827015/