strange onMeasure() behavior

135 Views Asked by At

I have a complex layout which contains following view:

<PackPreview xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="436dp"
        android:layout_height="352dp"
        android:layout_gravity="center"
        android:background="@drawable/shadow_purple">
...

Notice fixed width/height.
Within PackPreview I have onMeasure function:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);// always 0
    boolean exactSize=MeasureSpec.getMode(heightMeasureSpec)==MeasureSpec.EXACTLY;//always true
    Log.d("pack",heightSize + "," + exactSize);
    int desiredWidth=(int) (heightSize * ViewConfig.IMAGE_ASPECT_RATIO);
    desiredWidth*=1.1;
    widthMeasureSpec=MeasureSpec.makeMeasureSpec(desiredWidth,MeasureSpec.EXACTLY);
    this.setMeasuredDimension(desiredWidth, heightSize);
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);
}

It slightly adjusts view's width.The problem is when I run it on low resolution screen it falls into infinite loop of onMeasure calls.In the console I see tons of this messages D/pack: 0,true.What's interesting is this doesn't happen on higher resolutions.Is there any explanation for this?

UPD:Just checked:same thing happens everywhere but on higher resolutions MeasureSpec.getSize returns non-zero values so visualy it looks ok.

1

There are 1 best solutions below

0
undefined On

Infinite onMeasure calling was due to this in onLayout:

fade.setLayoutParams(new FrameLayout.LayoutParams(r-l,(b-t)/2));

Why it ignores size in the layout is still a mistery.Temporarly workarounded with

if(heightSize==0)
    heightSize = ViewConfig.screenHeight;