0

First I have a custom component, which is show pictures from the web. It's a simple layout and include a circle progressbar and an ImageView. In the default state the progressbar is visible and it's showing while picture downloading and when it's finished I hide the progressbar and show the picture in the ImageView. It's working very well, but int he emulator and on HTC Hero I got java.lang.OutOfMemoryError: bitmap size exceeds VM budget error. I found the solution here. But my problem, that the TARGET_WIDTH and TARGET_HEIGHT not fix, sometimes 60x90, sometimes fill_parent x fill_parent, and I can't calculate this values in the decode function. For example I added my view in the xml with layout_width="fill_parent" and use in Activity

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageLoader picture = (ImageLoader) findViewById(R.id.picture);
picture.load("http://winportal.net/images/galleries/wallpapers/Earth.jpg");

and in the load method I call the decode function from the solution and try here calculate sizes:

...
this.onMeasure(0, 0);
Log.d("Log", "Layout width: " + this.getMeasuredWidth());

TARGET_WIDTH =  this.getMeasuredWidth();
...

and the result is: "Layout width: 24"

I don't understand why got 24 bacause is the width of progressbar, but the layout - which is include it - is fill_parent width. I tried override onMeasure method but got 0.

@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
    this.setMeasuredDimension(parentWidth, parentHeight);
} 

I tried override onSizeChanged too. It's give good values but it's run later, after decode :(

How can I calculate my layout size?

Community
  • 1
  • 1
Robertoq
  • 373
  • 1
  • 5
  • 11

1 Answers1

0

When you call "this.onMeasure(0, 0);" you force the view to do measurements with following rules - UNSPECIFIED. Your view have following state: ProgressBar is visible, ImageView is hidden, so the result will be 24.

At the beginning, try to use FrameLayout with ProgressBar and ImageView. In that scenario you don't have to care about measurements. Just only make sure that the image is no too big ( in case of OutOfMemory). You can download image file to temporary file, than check the size with this, calculate appropriate sampling size and do decode, set bitmap for ImageView, set scale type for ImageView.

Damian Kołakowski
  • 2,731
  • 22
  • 25