2

아래 레이아웃 xml에서 두 개의 자식 뷰가있는 가로 LinearLayout을 만듭니다. LinearLayout 아동은 너비를 동적으로 늘려야하는데, ImageView은 부모의 높이에 따라 너비가 있습니다.layout_weight를 다시 계산하는 방법은 무엇입니까?

manipulated screenshot

하지만 실제로 얻을 것은 :

original screenshot

이 있기 때문이다

<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> 

이제 내가 무엇을 달성하기 위해 노력하고있어이 같은 결과는 내 사용자 정의 ImageView 1 : 1 종횡비 (단순화)를 유지하기 위해 onMeasure을 오버런했습니다.

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

    setMeasuredDimensions(height, height) 
} 

이제 새로운 ImageView 너비를 고려하여 너비 공유를 다시 계산하려면 부모보기가 필요합니다. 어떻게해야합니까?

답변

1

지금은 super을 호출하지 않는 onMeasure 구현과 관련된 문제를 해결했습니다. 설명 된 시나리오에 중점을두고 다른 곳에서 사용하면 쉽게 손상 될 수 있습니다. 또한 코드는 스칼라에 있습니다. 죄송합니다.

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

관련 문제