自定义固定宽高比例的Layout

本文介绍了一个自定义Android视图组件FiexedLayout,该组件能够保持固定的宽高比,通过设置demoWidth和demoHeight属性来控制整体的尺寸比例,并通过standard属性指定是以宽度还是高度为基准。

java文件

FiexedLayout.java


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import java.io.Serializable;



public class FiexedLayout extends RelativeLayout implements Serializable {

    private int mDemoHeight = -1;
    private int mDemoWidth = -1;
    private String mStandard = "w";

    public FiexedLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.FiexedLayout, defStyle, 0);
        if(a!=null){
            mDemoHeight = a.getInteger(R.styleable.FiexedLayout_demoHeight,-1);
            mDemoWidth = a.getInteger(R.styleable.FiexedLayout_demoWidth,-1);
            mStandard = a.getString(R.styleable.FiexedLayout_standard);
        }
    }

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

    public FiexedLayout(Context context) {
        this(context,null);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

        // Children are just made to fill our space.
        if(mStandard.length() > 0 && mDemoWidth != -1 && mDemoHeight != -1){
            if(mStandard.equals("w")){//以宽为标准
                int childWidthSize = getMeasuredWidth();
                widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize * mDemoHeight / mDemoWidth, MeasureSpec.EXACTLY);
            }else if(mStandard.equals("h")){//以高为标准
                int childheightSize = getMeasuredHeight();
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(childheightSize, MeasureSpec.EXACTLY);
                widthMeasureSpec = MeasureSpec.makeMeasureSpec(childheightSize * mDemoWidth / mDemoHeight, MeasureSpec.EXACTLY);
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

 

attrs.xml

    <declare-styleable name="FiexedLayout">
        <attr name="demoHeight" format="integer" />
        <attr name="demoWidth" format="integer" />
        <attr name="standard" format="string" />
    </declare-styleable>

xml布局中引用:

        <packageName.FiexedLayout
            android:id="@+id/main_users_main_img_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:demoHeight="490"
            app:demoWidth="640"
            app:standard="w"
            android:visibility="visible">

        </packageName.FiexedLayout>

 

其中demoHeight和demoWidth这两个属性不是最终效果显示的宽和高  ,而是用来控制整个view的宽高比

standard这个属性是控制标准的,"w"表示以宽为标准,高度变化,"h" 表示已高为标准宽度变化

本博客原地址:http://my.oschina.net/reone/blog/716238

转载于:https://my.oschina.net/reone/blog/716238

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值