Derpeus
Derpeus

Reputation: 31

OnMeasure getting wrong height (need height of available space)

enter image description here

I am trying to scale a custom view based on the height and width of the available space. So it should fill the space of the height of the green arrow and of the width. But the pixels i get back from heightMeasureSpec = View.MeasureSpec.getSize(heightMeasureSpec); is 1080, which is the size of my display.

How can i get the height of my available space, so of the layout with id: "test"

Layout:

  <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFCCCCCC"
    android:orientation="vertical"
    tools:context=".MainActivity" >
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:scaleType="centerCrop"
    android:contentDescription="@string/background"
    android:src="@drawable/background" />
    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical">
    <include
    android:id="@+id/tool_bar"
    layout="@layout/tool_bar" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <LinearLayout
            android:id="@+id/test"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <nl.bla.test.customview
                android:id="@+id/test2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#FFFFFFFF" />
        </LinearLayout>
    </LinearLayout>
   </LinearLayout>
   </RelativeLayout>

onMeasure method of the custom view:

   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    widthMeasureSpec = View.MeasureSpec.getSize(widthMeasureSpec);
    heightMeasureSpec = View.MeasureSpec.getSize(heightMeasureSpec);


    float f = Math.min(widthMeasureSpec / 1280.0F, heightMeasureSpec / 720.0F);
    Log.i("float f = ", "" + widthMeasureSpec + " " + heightMeasureSpec) ;
    setMeasuredDimension(1280, 720);
    setPivotX(0.0F);
    setPivotY(0.0F);
    setScaleX(f);
    setScaleY(f);

}

Upvotes: 1

Views: 1301

Answers (1)

Derpeus
Derpeus

Reputation: 31

figured it out, this gives me the right height and width:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
float f = (float) Math.min(width / 1280.0, height / 720.0);
Log.i("OnMeasureTest",""+height);
setMeasuredDimension(1280, 720);
setPivotX(0.0F);
setPivotY(0.0F);
setScaleX(f);
setScaleY(f);

}

Upvotes: 1

Related Questions