2

在下面的佈局xml中,我創建了具有兩個子視圖的水平LinearLayoutLinearLayout孩子應該動態地伸展它的寬度,ImageView的寬度取決於父母的身高。現在如何重新測量layout_weight?

<LinearLayout android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal"> 

    <LinearLayout android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:orientation="vertical"> 

     <!-- Some children ... --> 

    </LinearLayout> 

    <MyAspectRatioImageView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:adjustViewBounds="true" /> 

</LinearLayout> 

,我想要做到的,是這樣一個結果:

manipulated screenshot

但我實際上得到的是:

original screenshot

這是因爲在我的自定義ImageView我覆蓋onMeasure保持1:1的縱橫比(簡化):

override def onMeasure(widthMeasure: Int, heightMeasure: Int) 
{ 
    super.onMeasure(widthMeasure, heightMeasure) 

    setMeasuredDimensions(height, height) 
} 

現在我需要父視圖重新計算新的ImageView寬度的寬度共享。我怎樣才能做到這一點?

回答

1

現在我解決了這個問題,執行onMeasure,不叫super。它高度專注於所描述的場景,並可能在別處使用時輕易破解。代碼也在Scala中,對此很抱歉。

override def onMeasure(widthMeasure: Int, heightMeasure: Int) 
{ 
    (Option(getDrawable), (getMode(widthMeasure), getMode(heightMeasure))) match 
    { 
     // No Drawable 
     case (None, _) => setMeasuredDimensions(0, 0) 
     // Width and height are known 
     case (_, (EXACTLY, EXACTLY)) => 
     { 
      // Determine which axis to adjust for the ratio 
      val (width, height) = ratio.getDominance match 
      { 
       case 0 => (getSize(widthMeasure), (getSize(widthMeasure) * ratio.getValue).toInt) 
       case 1 => ((getSize(heightMeasure) * ratio.getValue).toInt, getSize(heightMeasure)) 
      } 

      setMeasuredDimensions(width, height) 

      // This call makes this whole thing work in first place 
      if(getAdjustViewBounds) 
      { 
       getLayoutParams.width = width 
       getLayoutParams.height = height 
      } 
     } 
     // Only height dimension is known, adjust width to ratio 
     case (_, (_, EXACTLY)) => 
     { 
      val height = getSize(heightMeasure) 
      setMeasuredDimensions((height * ratio.getValue).toInt, height) 
     } 
     // Only width dimension is known, adjust height to ratio 
     case (_, (EXACTLY, _)) => 
     { 
      val width = getSize(widthMeasure) 
      setMeasuredDimensions((width * ratio.getValue).toInt, width) 
     } 
     case (Some(drawable), (UNSPECIFIED, UNSPECIFIED)) => 
     { 
      val (width, height) = ratio.getDominance match 
      { 
       case 0 => (drawable.getIntrinsicWidth, (drawable.getIntrinsicWidth * ratio.getValue).toInt) 
       case 1 => ((drawable.getIntrinsicHeight * ratio.getValue).toInt, drawable.getIntrinsicHeight) 
      } 

      setMeasuredDimensions(width, height) 
     } 
     case (Some(drawable), (UNSPECIFIED, _)) => 
     { 
      setMeasuredDimensions(
       (drawable.getIntrinsicWidth * ratio.getValue).toInt, 
       drawable.getIntrinsicWidth 
      ) 
     } 
     case (Some(drawable), (_, UNSPECIFIED)) => 
     { 
      setMeasuredDimensions(
       (drawable.getIntrinsicHeight * ratio.getValue).toInt, 
       drawable.getIntrinsicHeight 
      ) 
     } 
     case _ => super.onMeasure(widthMeasure, heightMeasure) 
    } 
} 

https://github.com/Taig/Toolbelt/blob/master/src/main/scala/com/taig/android/widget/image/AspectRatio.scala