屏幕适配工具

</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>

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35556000/article/details/52366186
个人分类: 学习笔记
上一篇Fragment跳转之Fragment与Activity通信
下一篇简单实现网络的音频播放
想对作者说点什么? 我来说一句

andorid屏幕适配dimens生成工具

2017年03月30日 14KB 下载

Android 屏幕适配工具

2018年05月29日 20KB 下载

没有更多推荐了,返回首页

关闭
关闭