使用FrameLayout来适配ImageViw按比例展示
来源:互联网 发布:中国人民航空大学知乎 编辑:程序博客网 时间:2024/05/22 19:26
一、布局文件:
<com.hsc.googleplay.ui.view.RatioLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:ratio="2.43"> <ImageView android:id="@+id/iv_pic" android:src="@drawable/subject_default" android:layout_width="match_parent" android:layout_height="match_parent"/></com.hsc.googleplay.ui.view.RatioLayout>
再创建attrs.xml文件:
<resources> <declare-styleable name="RatioLayout"> <attr name="ratio" format="float"/> </declare-styleable> </resources>
/** * 自定义控件,按比例来决定布局高度 做了让View支持wrap_content与padding * Created by 15827 on 2017/5/22. */public class RatioLayout extends FrameLayout { private float mRatio; public RatioLayout(Context context) { super(context); } public RatioLayout( Context context, AttributeSet attrs) { super(context, attrs); //获取属性值 //当自定义属性时,系统会自动生成属性相关id,此id通过R.styleable来引用 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatioLayout); // id = 属性名_具体属性字段名称 //系统自动生成RatioLayout_ratio,根据两个name值 mRatio = typedArray.getFloat(R.styleable.RatioLayout_ratio, -1);//宽高比例值 typedArray.recycle();//回收,提高具体性能 //LogUtils.i("ratio=" + mRatio); //布局中的app:ratio=“2.43” 来自这里 也可以利用图片的真实宽高 } public RatioLayout( Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 1 获取宽度 // 2 根据宽度和比例ratio,计算控件的高度 // 3 重新测量控件 LogUtils.d("widthMeasureSpec: "+widthMeasureSpec); // MeasureSpec.AT_MOST; 至多模式, 控件有多大显示多大, wrap_content // MeasureSpec.EXACTLY; 确定模式, 类似宽高写死成dip, match_parent // MeasureSpec.UNSPECIFIED; 未指定模式. int widthMode = MeasureSpec.getMode(widthMeasureSpec);//获取宽度模式 int width = MeasureSpec.getSize(widthMeasureSpec); // 获取宽度值 int heightMode = MeasureSpec.getMode(heightMeasureSpec);//获取高度模式 int height = MeasureSpec.getSize(heightMeasureSpec); // 获取高度值 //宽度确定,高度不确定,mRatio合法,才计算高度值 if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY && mRatio > 0) { //图片宽度 = 控件宽度 - 左侧内边距 - 右侧内边距 int imageWidth = width - getPaddingLeft() - getPaddingRight(); //图片高度 = 图片宽度 / 宽高比例 int imageHeight = (int) (imageWidth / mRatio + 0.5f); // 控件高度 = 图片高度 + 上侧内边距 + 下侧内边距 height = imageHeight + getPaddingTop() + getPaddingBottom(); //根据最新的高度重新生成 heightMeasureSpec (高度模式是确定模式) heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); } //按照最新的高度测量控件 super.onMeasure(widthMeasureSpec, heightMeasureSpec); }}
阅读全文
1 0
- 使用FrameLayout来适配ImageViw按比例展示
- FrameLayout使用
- GCD加载图片并按比例拉伸展示
- 【FrameLayout】使用FrameLayout应该注意的地方
- 使用Framelayout+Fragment+tab方式展示首页时,个别fragment有时会出现空白情况的解决
- framelayout的使用
- FrameLayout的使用
- 使用FrameLayout实现遮罩层
- FrameLayout的使用进阶
- Android 布局 Framelayout 使用
- 使用FrameLayout实现遮罩层
- 在一定范围里,按比例缩放图片,优化展示效果
- 使用jquery按比例缩放图片
- 使用jquery按比例缩放图片
- 使用PIL按比例做缩略图
- LinearLayout使用weightSum按比例布局
- LinearLayout使用weightSum按比例布局
- FrameLayout
- ubuntu 安装 mysql navicat
- CodeForces
- 排序之希尔排序
- oracle杀掉外部连接进程
- Java的PermGen概念认识,以及PermGen space的错误解决方法
- 使用FrameLayout来适配ImageViw按比例展示
- 使用github保存项目的流程
- 常见错误清单(长期更新)
- JavaScript设计模式之中介模式
- 软件设计目标—基于简单工厂模式的计算器程序
- hdu1176免费馅饼
- 排序之堆排序
- java项目的一些配置,Eclispe中设置哪几项
- redis事务中的WATCH命令和基于CAS的乐观锁