关闭

屏幕适配工具

标签: 屏幕适配的自定义组件屏幕适配工具
108人阅读 评论(0) 收藏 举报
分类:
</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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3535次
    • 积分:233
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:1篇
    • 译文:1篇
    • 评论:0条
    文章分类