title: Android Imageview: Measure unspecified to calculate Bitmap dimensions
body: <p>Google suggests to load Bitmaps from resources scaled down, depending on the actual ImageView size ("Loading Large Bitmaps Efficiently" at googles developer guides). Therefor, I have to know the width and height of the ImageView before I can decode the bitmap.</p>
  
  <p>My code looks something like the one posted below. <code>decodeSampledBitmapFromResources</code> returns the bitmap as a scaled down version of the one stored in resources.</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceSate)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.myLayout)
  
      ImageView imageView = (ImageView) findViewById(R.id.myImageView);
  
      /**
      At this point, I need to calculate width and height of the ImageView.
      **/
  
      Bitmap bitmap = MyBitmapManager.decodeSampledBitmapFromResource(
                          getResources(), R.drawable.my_icon, width, height);
      imageView.setImageBitmap(bitmap);
  }
  </code></pre>
  
  <p>The problem is, as I am in onCreate, my ImageView does not have any width and height, yet. getWidth() and getHeight() are just returning 0. I stumbled over this code to calculate the size of a view before it is actually drawn:</p>
  
  <pre><code>ImageView v = findViewById(R.id.myImageView);
  v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
  int width = v.getMeasuredWidth();
  int height = v.getMeasuredHeight();
  </code></pre>
  
  <p>Is this suitable for my situation? I tried it and the above code returns some values for width and height which seem to be correct, but Im not sure if this is the correct way to do it.</p>
  
  <p><strong>UPDATE:</strong>
  After some more testing, this seems NOT to work.
  In the above example, im using a PNG with a size of 192x192 pixels.
  After measureing the ImageView as seen above, i get a measured dimension of 128x128.
  If I call getWidth() and getHeight() AFTER the bitmap is set to the imageview, the dimension is 100x100.
  So in this scenario, the image is downsized from 192x192 to 128x128, but not to 100x100 as it should be.</p>
  
  <p>It seems that <code>Measurespec.UNSPECIFIED</code> always returns dimensions greater than they are at the end. </p>
  
  <p>Thanks in advance,</p>
  
  <p>danijoo</p>