title: Android onMeasure change View's parameters twice
body: <p>I've created a new view (extends ImageView) and override onMeasure method.</p>
  <p>Xml code:</p>
  <pre><code>&lt;by.bsuir.klondike.mainCardsView
      android:id=&quot;@+id/imageview1&quot;
  
      android:layout_width=&quot;fill_parent&quot;
      android:layout_height=&quot;fill_parent&quot;
  
      android:scaleType=&quot;fitEnd&quot;
      android:baselineAlignBottom=&quot;true&quot;
      android:src=&quot;@drawable/cardsmain&quot;
      android:layout_alignParentBottom=&quot;true&quot;
      android:layout_alignParentRight=&quot;true&quot;
      /&gt;
  </code></pre>
  <p>At first I overrode a method like this</p>
  <pre><code> @Override 
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
     int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
     int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
     width = parentWidth/2;
     height = 210*width/498;
     this.setMeasuredDimension(width, height);
     this.setLayoutParams(new *ParentLayoutType*.LayoutParams(width,height));
     super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }
  </code></pre>
  <p>According to <a href="http://developer.android.com/guide/topics/ui/how-android-draws.html" rel="nofollow noreferrer">Android developer</a> onMeasure calls twice. On it's first call I receive parentWidth == 480 and parentHeight == 320. But on second call I receive parentWidth == 240 and parentHeight == 101.
  It turns out, that the size is changing twice.
  I've solve this problem using bool flag and change dimension only on first call.</p>
  <pre><code>@Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      if (flag){
          flag=false;
          int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
          int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
          width = parentWidth/2;
          height = 210*width/498;
      }
      this.setMeasuredDimension(width, height);
  }
  </code></pre>
  <p>But I think that it's a wrong way.
  How can I do it right?</p>