Android onMeasure change View's parameters twice
<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="@+id/imageview1"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:scaleType="fitEnd"
    android:baselineAlignBottom="true"
    android:src="@drawable/cardsmain"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    /&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">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>
