如果某些 ImageView
s与 GridView
或 ListView
相关,我们想在屏幕上显示一些图像,通常会发生图像宽度的值,高度为零(0),所以当我们通过此参数来计算压缩位图时,这是例外,我回顾了一些框架,但这仍然是一个问题。我想获得一些内核解决方案来解决此问题。当ImageView
的宽度和高度为0时,我该怎么办0。
public void loadImage(final ImageView imageView, final String imageUrl){
mExecutor.execute(new PriorityRunnable(mThreadsCount) {
@Override
public void doSomething() {
while(!mExecutor.isPause()){
if(imageView != null && imageView.getTag().equals(imageUrl)){
mImageCache.loadImage(imageView, imageUrl);
}
}
}
});
}
public void loadImage(ImageView imageView, String imageUrl){
//readFromMemoryCache
Bitmap bitmap = getBitmap(imageUrl);
if(bitmap != null){
mImageView = imageView;
mBitmap = bitmap;
mHandler.sendEmptyMessage(DISPLAY_IMAGE);
return;
}
int width = imageView.getWidth();
int height = imageView.getHeight();
//because width = 0, so it doesn't do anything, only return
if(width == 0 || height == 0){
return;
}
//load from http or disk
bitmap = getBitmap(imageUrl, width, height);
if(bitmap != null){
mImageView = imageView;
mBitmap = bitmap;
mHandler.sendEmptyMessage(DISPLAY_IMAGE);
}
}
您需要使用全局布局侦听器。类似:
yourView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (yourView.getHeight() > 0 && yourView.getWidth() > 0) {
CompatUtil.removeOnGlobalLayoutListener(yourView, this);
// do your stuff
}
}
});