屏幕适配工具

</pre><pre name="code" class="java">自定义组件的代码

public class RatioView extends FrameLayout {

	private static final int RELATIVE_TO_WIDTH = 0;
	private static final int RELATIVE_TO_HEIGHT = 1;
	private final float ratio = 2.618f;
	private final int relation = 0;
	private float mRatio;
	private int mRelative;

	public RatioView(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	public RatioView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatioImageView);
		mRatio = typedArray.getFloat(R.styleable.RatioImageView_ratio, ratio);
		mRelative = typedArray.getInt(R.styleable.RatioImageView_relative, relation);
		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);

		// 2.根据需求设置相应的自适应模式及适应参数,计算相应的自适应宽或高
		if (mRelative == RELATIVE_TO_WIDTH && widthMode == MeasureSpec.EXACTLY) {
			// 宽度固定,高度自适应
			int parentHeight = (int) (width / mRatio);
			// 2.1计算测量孩子的宽高,封装成规格
			int childWidth = width - getPaddingLeft() - getPaddingRight();
			int childHeight = parentHeight - getPaddingTop() - getPaddingBottom();

			int childWidthSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
			int childHeightSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);
			// 2.2根据规格测量孩子
			measureChildren(childWidthSpec, childHeightSpec);
			// 3测量父容器自己
			setMeasuredDimension(width, parentHeight);
		} else if (mRelative == RELATIVE_TO_HEIGHT && heightMode == MeasureSpec.EXACTLY) {
			// 高度固定,宽度自适应
			int parentWidth = (int) (height * mRatio);
			// 2.1计算测量孩子的宽高,封装成规格
			int childWidth = parentWidth - getPaddingLeft() - getPaddingRight();
			int childHeight = height - getPaddingTop() - getPaddingBottom();

			int childWidthSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
			int childHeightSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);
			// 2.2根据规格测量孩子
			measureChildren(childWidthSpec, childHeightSpec);
			// 3测量父容器自己
			setMeasuredDimension(parentWidth, height);
		} else {
			// 自由宽高
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		}
	}

}

attr文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="RatioImageView">
        <attr name="ratio" format="float"></attr>
        <attr name="relative" format="enum">
            <enum name="width" value="0"></enum>
            <enum name="height" value="1"></enum>
        </attr>
    </declare-styleable>
</resources>

最后只需要在需要适配的位置引用xml代码

 <widget.RatioView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        app:relative="width" >

        <ImageView
            android:id="@+id/item_iv_pic"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/ic_launcher" />
    </widget.RatioView>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值