Android View设置宽高比

  • 直接上代码(代码可直接复制使用)
1、布局设置

ConstraintLayout 有个属性可以控制比例:layout_constraintDimensionRatio

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--宽高比5:4-->
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:src="@drawable/head_female_icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="5:4"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

《Android View设置宽高比》 image.png

2、代码设置
//根据宽高比设置控件宽高, 如设置宽高比为5:4,那么widthRatio为5,heightRatio为4
public static void setWidthHeightWithRatio(View view, int width, int widthRatio, int heightRatio) {
    if (width <= 0) width = view.getWidth();
    int height = width * heightRatio / widthRatio;
    ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
    if (layoutParams != null) {
        layoutParams.height = height;
        layoutParams.width = width;
        view.setLayoutParams(layoutParams);
    }
}
3、自定义控件式
  • 新建RatioRelativeLayout 继承RelativeLayout
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

/**
 * 自定义高宽比布局
 */
public class RatioRelativeLayout extends RelativeLayout {

    private float ratio;

    public RatioRelativeLayout(Context context) {
        super(context);
    }

    public RatioRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        //获取自定义属性值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatioRelativeLayout);
        ratio = typedArray.getFloat(R.styleable.RatioRelativeLayout_ratio, 0.0f);
    }

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //获取宽度的模式和尺寸
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        if (ratio != 0) {
            //根据宽高比ratio和模式创建一个测量值
            heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (widthSize * ratio), MeasureSpec.EXACTLY);
        }
        //必须调用下面的两个方法之一完成onMeasure方法的重写,否则会报错
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 设置高宽比
     *
     * @param ratio 宽高比(比如:高:宽 = 4:5,ratio=0.8)
     */
    public void setRatio(float ratio) {
        this.ratio = ratio;
    }
}

  • 在styles.xml文件中添加自定义属性
 <declare-styleable name="RatioRelativeLayout">
        <attr name="ratio" format="float" />
    </declare-styleable>
  • 使用
 <com.yate.jsq.widget.RatioRelativeLayout
        android:id="@id/common_relate_layout_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:ratio="1"/>      
    原文作者:淡淡_孩子气
    原文地址: https://www.jianshu.com/p/53f4c21c0f4c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞