title: onMeasure(...) being passed MeasureSpec.EXACTLY, apparently wrongly
body: <p>I've created a custom View that implements <code>onMeasure</code>:</p>
  
  <pre><code>@Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      float width = MeasureSpec.getSize(widthMeasureSpec);
      final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  
      float height = MeasureSpec.getSize(heightMeasureSpec);
      final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  
      float nominalHeight = getResources().getInteger(R.integer.nominalheight);
      float nominalWidth = getResources().getInteger(R.integer.nominalwidth);
  
      float aspectRatio = nominalWidth / nominalHeight;
  
      if( width / height &gt; aspectRatio //too wide
              &amp;&amp; (
                      widthMode == MeasureSpec.AT_MOST ||
                      widthMode == MeasureSpec.UNSPECIFIED 
              )
              ) { 
          width -= (width - height * aspectRatio);
      }
      if( width / height &lt; aspectRatio //too tall
              &amp;&amp; (
                      heightMode == MeasureSpec.AT_MOST ||
                      heightMode == MeasureSpec.UNSPECIFIED 
              )
              ) { 
          height -= (height - width / aspectRatio);
      }
  
      setMeasuredDimension((int)width, (int)height);
  }
  </code></pre>
  
  <p>The intention is, where possible, to create a rectangle whose aspect ratio is the same as the one specified by <code>nominalheight</code> and <code>nominalwidth</code>.  Obviously if the function is passed <code>MeasureSpec.EXACTLY</code> then it should lay out with the dimension given in that direction.</p>
  
  <p>I'm laying this <code>View</code> out with <code>WRAP_CONTENT</code> in the xml in both directions.  What's puzzling me is that, stepping through the onMeasure calls, half of them show MeasureSpec.AT_MOST and the routine calculates the correct rectangle, and the other half show MeasureSpec.EXACTLY and it obviously uses the given dimensions.  Even more puzzlingly, if I disable the conditionality (I would assume, from <a href="http://developer.android.com/guide/topics/ui/how-android-draws.html" rel="nofollow" title="How Android Draws Views">documentation</a> and the <a href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/LabelView.html" rel="nofollow" title="LabelView.java">example code</a>, incorrectly) it works fine.</p>
  
  <p>Why am I getting these alternating calls with different values, and how can I persuade Android to lay my View out in the correct dimensions?</p>