图片适配自定义控件

一,实现效果(注意图片本身的格式会有很大的影响)

当宽度match_parent,设置宽高的比例值,确定高度值

当高度match_parent,设置宽高的比例值,确定宽度.

写二个自定义属性,ration比值,相对于谁?

    <declare-styleable name="RatioLayout">
        <attr name="mRatio" format="float"></attr>
        <attr name="mRelative" format="enum">
            <enum name="width" value="0"></enum>
            <enum name="height" value="1"></enum>
        </attr>
    </declare-styleable>
二.代码实现
<pre name="code" class="java">/**
 * 支持宽高自适应的组件,把需要适配的view作为它的孩子即可
 */
public class RatioLayout extends FrameLayout {
    private float mRatio = 2.426f;//默认2.42
    public static final int RELATIVE_WIDHT = 0;
    public static final int RELATIVE_HEIGHT = 1;
    private int mRelative = RELATIVE_WIDHT;//默认宽度match

    public void setRatio(float ratio) {
        mRatio = ratio;
    }

    public void setRelative(int relative) {
        mRelative = relative;
    }

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

    public RatioLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        //从xml中获取自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatioLayout);
        mRatio = typedArray.getFloat(R.styleable.RatioLayout_mRatio,mRatio);
        mRelative = typedArray.getInt(R.styleable.RatioLayout_mRelative,RELATIVE_WIDHT);
        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //1.获取信息
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //模式对应关系,matchparent/60dp ---- EXACTLY; wrapcontent-----> MeasureSpec.AT_MOST
        if(widthMode == MeasureSpec.EXACTLY && mRelative == RELATIVE_WIDHT){
            //情形一:宽度填充父窗体,高度自适应
            //TODO:计算height,按照2.426的比例计算
            int totalHeight = (int) (width / mRatio);

            //2.测量孩子imageview
            int childWidth = width - getPaddingLeft() - getPaddingRight(); //让自定义view支持padding功能
            int childHeight = totalHeight - getPaddingTop() - getPaddingBottom();
            int childWidthSpec = MeasureSpec.makeMeasureSpec(childWidth,MeasureSpec.EXACTLY);
            int childHeightSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);
            measureChildren(childWidthSpec, childHeightSpec);
            //3.决定自己的宽高
            setMeasuredDimension(width, totalHeight);
        }else if(heightMode == MeasureSpec.EXACTLY  && mRelative == RELATIVE_HEIGHT){
            //情形二:高度固定,宽度自适应
            int totalWidth = (int) (height * mRatio);

            //2.测量孩子imageview
            int childWidth = totalWidth - getPaddingLeft() - getPaddingRight(); //让自定义view支持padding功能
            int childHeight = height - getPaddingTop() - getPaddingBottom();
            int childWidthSpec = MeasureSpec.makeMeasureSpec(childWidth,MeasureSpec.EXACTLY);
            int childHeightSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);
            measureChildren(childWidthSpec, childHeightSpec);
            //3.决定自己的宽高
            setMeasuredDimension(totalWidth, height);
        }else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            measureChildren(widthMeasureSpec,heightMeasureSpec);
        }
    }
}

三总布局

 
<pre name="code" class="java"> <com.example.administrator.textdemo.RatioLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:mRelative="height"
        app:mRatio="5"
        >
        <ImageView
            android:src="@drawable/a"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </com.example.administrator.textdemo.RatioLayout>


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值