0

If some ImageViews are related to GridView or ListView, and we want to display some images on screen, it often happens the value of an image's width and height are zero(0), so it occurs Exception when we pass this arguments to calculate the compressed Bitmap, I review some frameworks but it's still a problem for me.I want to obtain some kernel solutions to solve this problem. what should I do when the ImageView's width and height is 0.Any help is much appreciated.

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);
    }
}
wislie
  • 101
  • 1
  • 9
  • 1
    You could delay the loading of the images until the layout is measured. See [this so entry](http://stackoverflow.com/questions/12068945/get-layout-height-and-width-at-run-time-android) of how to do it. – GPuschka Jun 16 '16 at 07:46
  • 1
    where you get the image's size ? in onCreate() ? – Alex Hong Jun 16 '16 at 07:46
  • I create a new thread, and calculate image's size@Alex Hong – wislie Jun 16 '16 at 07:52

1 Answers1

0

You need to use a global layout listener. Something like:

    yourView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (yourView.getHeight() > 0 && yourView.getWidth() > 0) {
                CompatUtil.removeOnGlobalLayoutListener(yourView, this);
                // do your stuff
            }
        }
    });
kingston
  • 11,053
  • 14
  • 62
  • 116
  • but this operation couldn't guarantee my view's width and height are not equals "0", is there anything else I should do. @kingston – wislie Jun 16 '16 at 08:45
  • 1
    the onGlobalLayout will be called multiple times while the view is being laid out. At the end the size won't be zero anymore and you will call removeOnGlobalLayoutListener not to get called anymore and then you will show the image – kingston Jun 16 '16 at 09:03
  • thank you @kingston, I'm still confused about this question, at first I have displayed my ListView , it needs some time to load these images, and at first width and height are 0, and I must filter this case, otherwise, it occurs Exception. So due to this reason, my screen couldn't show images. And I found as I scroll this listview, some Images will show. I have added my codes. – wislie Jun 16 '16 at 09:57