自定义控件【按照宽高比例显示】

自定义控件

public class RatioLayout extends FrameLayout {

    // 按照宽高比例去显示
    private  float ratio = 1.43f; // 比例值

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

    public RatioLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RatioLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        /**参数1 命名控件 参数2 属性的名字 参数3 默认的值*/
        float  ratio = attrs.getAttributeFloatValue(
                "http://schemas.android.com/apk/res-auto","ratio",1.43f);
        setRatio(ratio);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        /**widthMeasureSpec 宽度的规则 包含了两部分 模式 值*/
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);/**模式*/
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);/**宽度大小*/
        int width = widthSize - getPaddingLeft() - getPaddingRight();/**去掉左右的padding*/

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);/**模式*/
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);/**长度大小*/
        int height = heightSize - getPaddingTop() - getPaddingBottom();/**去掉上下的padding*/

        if(widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY){
            height = (int) (width / ratio + 0.5f);/**修正一下 高度的值 让高度=宽度/比例,+0.5f是确保四舍五入*/
        }else if (widthMode != MeasureSpec.EXACTLY
                && heightMode == MeasureSpec.EXACTLY) {
             /**由于高度是精确的值 ,宽度随着高度的变化而变化*/
            width = (int) ((height * ratio) + 0.5f);
        }
        /**重新制作了新的规则*/
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(width + getPaddingLeft() + getPaddingRight(),MeasureSpec.EXACTLY);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height + getPaddingTop() + getPaddingBottom(),MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void setRatio(float ratio) {
        this.ratio = ratio;
    }
}

应用写好的自定义控件

| 在layout布局文件里应用
<com.appstore.utils.RatioLayout
            android:id="@+id/rl_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            schema:ratio="1.43">

            <ImageView
                android:id="@+id/item_icon"
                android:layout_width="match_parent"
                android:layout_height="@dimen/listView_imageItem_height"
                android:scaleType="fitXY"
                android:src="@drawable/ic_default" />
        </com.appstore.utils.RatioLayout>
| 控件里用自定义属性要添加声明
  • 在res文件夹下创建attrs.xml文件,写如下代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="com.appstore.utils.RatioLayout">
        <attr  name="ratio"  format="float"></attr>
    </declare-styleable>
</resources>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值