package com.wisewoods.eduandroid.view;

import android.content.Context;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

//等比例 图片控件
public class RatioImageView extends androidx.appcompat.widget.AppCompatImageView {

    private final float ratio = 16.0f / 9;

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

    public RatioImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RatioImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //1.获取自定义控件的宽度具体值
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        //2.获取高度值精确值
        int heightSize = (int) (widthSize / ratio + 0.5f);
        //3.用精确模式搭配精确值,生成高度的32位数
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
