将 ImageView 高度设置为宽度的 50%

Set ImageView Height 50% Of That's Width

我想将 ImageView 的高度设置为其宽度的 50%。 我该怎么做? 例如,如果 ImageView 的宽度等于 200dp,我希望它的高度等于 100dp。

我这样试过,但是不行。

int img_width = cover_img.getLayoutParams().width;
        int img_height = img_width/2;
        cover_img.getLayoutParams().width = img_width;
        cover_img.getLayoutParams().height = img_height;
        cover_img.requestLayout();

请帮帮我

更新: 我写了,我用这个 class 来做到这一点,这是将 ImageView 高度设置为宽度的 50% 的简单、简单和快速的方法。

public class RectImage extends ImageView
{
    public RectImage(Context context)
    {
        super(context);
    }

    public RectImage(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public RectImage(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
    }


    public RectImage(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
    {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(widthSize, widthSize/2);
    }
}

试试下面给出的方法

int img_width = cover_img.getDrawable().getIntrinsicWidth();
        int img_height = img_width/2;
        cover_img.getLayoutParams().width = img_width;
        cover_img.getLayoutParams().height = img_height;
        cover_img.requestLayout();

如果您使用 ConstraintLayout 作为根,您可以执行以下操作:

<androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:animateLayoutChanges="true"
            >

           <ImageView
                android:id="@+id/image_view"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:visibility="invisible"
                app:layout_constraintDimensionRatio="1:0.50"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                />

</androidx.constraintlayout.widget.ConstraintLayout>

主要围绕layout_constraintDimensionRatio这样就可以自动实现你想要的,估计也是最简单的了。

当然,请随意定位您的视图,并忽略此处的约束准则,我只是将它们放在那里作为完整示例。

如果使用"Constraint layout"

,您可以在xml中设置比率
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"> 
<ImageView
    android:id="@+id/cover_img"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="2:1" />
 </androidx.constraintlayout.widget.ConstraintLayout>

或像您的示例代码一样以编程方式设置。

//Your layout param type
 ViewGroup.LayoutParams lpm =  cover_img.getLayoutParams();
    int width = lpm.width;
    int height =width/2;
    lpm.height = height;
    cover_img.setLayoutParams(lpm);
    cover_img.requestLayout();