以ImageView为例 ,写一个宽高比控件,以及各种写法的应用场景

1 代码实现

1. ConstraintLayout 约束布局


设置宽度为父控件的50% :app:layout_constraintWidth_percent=“0.5”
宽高比16:9 app:layout_constraintDimensionRatio=“H,16:9”

    <ImageViewandroid:id="@+id/img"android:layout_width="0dp"android:layout_height="0dp"android:scaleType="centerCrop"android:src="@mipmap/ic_car"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintDimensionRatio="H,16:9"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintWidth_percent="0.5" />

2. 百分比布局

设置宽度为父控件的50% : app:layout_widthPercent=“50%”
宽度为高度的160%

    <ImageViewandroid:id="@+id/img"android:layout_width="0dp"android:layout_height="0dp"android:layout_centerInParent="true"android:scaleType="centerCrop"android:src="@mipmap/ic_car"app:layout_aspectRatio="160%"app:layout_widthPercent="50%" />

3. 自定义ImgeView 重写onMeasure()方法

设置宽度为父控件50%
宽高比80%,需要在代码构建成功后才能预览

 <com.zn.advanced.ui.view.RatioImageViewandroid:id="@+id/img"android:layout_width="0dp"android:layout_height="0dp"android:layout_centerInParent="true"android:scaleType="centerCrop"android:src="@mipmap/ic_car"app:ratio="0.7"app:width_ratio="0.5" />
    @Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int widthSize;if (widthRatio != 0) {//父控件ViewGroup viewGroup = (ViewGroup) this.getParent();//父控件宽度int width = viewGroup.getMeasuredWidth();//当前控件宽度widthSize = (int) ((float) width * widthRatio);widthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);} else {widthSize = MeasureSpec.getSize(widthMeasureSpec);}if (ratio != 0) {heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (widthSize * ratio), MeasureSpec.EXACTLY);}super.onMeasure(widthMeasureSpec, heightMeasureSpec);}

4.代码中设置

传入view 、宽 、比例,Android studio不能预览,只有在代码执行后才能看到效果

public static void setRatioWidthHeight(View view, int width, float ratio) {if (width <= 0) width = view.getWidth();int height = (int) ((float) width * ratio);ViewGroup.LayoutParams layoutParams = view.getLayoutParams();if (layoutParams != null) {layoutParams.height = height;layoutParams.width = width;view.setLayoutParams(layoutParams);}}

效率测试

我们在demo中测试一下我们的代码执行效率,考虑一下每种设置方式的应用场景 。
在activity的根布局,也就是测试ImageView的父布局,还有我们的ImageView的onMeasure中打上断点查看执行次数。

1.ConstraintLayout:

在ConstraintLayout 的onLayout()、onMeasure(),ImageView的onMeasure()方法打上断点log,执行

ConstraintLayout  onMeasure    width =1440  height=2708
ImageView         onMeasure    width =720 height=405
ConstraintLayout  onMeasure    width =1440  height=2708
ImageView         onMeasure    width =720 height=405
ConstraintLayout  onLayout

2.PercentRelativeLayout:

在PercentRelativeLayout的onLayout()、onMeasure(),ImageView的onMeasure()方法打上断点log,执行

PercentRelativeLayout onMeasure    width =1440  height=2708
ImageView onMeasure    width =720 height=2708
ImageView onMeasure    width =720 height=450
PercentRelativeLayout onMeasure    width =1440  height=2708
ImageView onMeasure    width =720 height=2708
ImageView onMeasure    width =720 height=450
PercentRelativeLayout onLayout

这里会发现,ImageView的onMesure()方法在父布局每次执行onMeasure()之后比在ConstraintLayout中多走了一次

3.自定义RatioImageView:

在RelativeLayout的onLayout()、onMeasure(),ImageView的onMeasure(),RatioImageView的onMeasure()方法打上断点log,执行

RelativeLayout onMeasure    width =1440  height=2708
RatioImageView onMeasure    width =0  height=0
ImageView onMeasure    width =0 height=0
RatioImageView onMeasure    width =0  height=0
ImageView onMeasure    width =0 height=0
RelativeLayout onMeasure    width =1440  height=2708
RatioImageView onMeasure    width =1008  height=504
ImageView onMeasure    width =1008 height=504
RatioImageView onMeasure    width =1008  height=504
ImageView onMeasure    width =1008 height=504
RelativeLayout onLayout

看起来跟上面的百分比差不多 ,因为继承了ImageView多走了一层onMeasure()主要是执行了RatioImageView的宽高计算影响不大

4.代码计算宽高并赋值:

在RelativeLayout的onLayout()、onMeasure(),ImageView的onMeasure()方法打上断点log,执行

RelativeLayout onMeasure    width =1440  height=2708
ImageView onMeasure    width =720 height=2708
ImageView onMeasure    width =720 height=576
RelativeLayout onMeasure    width =1440  height=2708
ImageView onMeasure    width =720 height=2708
ImageView onMeasure    width =720 height=576
RelativeLayout onLayout    changed =true l=0 t=0 r=1440 b=2708

跟上面的一样

综上来看,我们通过第一种方法设置的宽高比的View执行效率最高,其他方式效率差不多。
在实际应用中第一种应该最优先考虑,在RecyclerView的列表或其他条件中宽高可能要动态计算,这时候最后一种就比较合适了,根据实际情况选择吧 。

测试代码

Android 宽高比控件相关推荐

  1. android 动态修改控件的宽高

    今天,简单讲讲android如何动态设置控件的宽高. 这个其实也比较简单,但是之前做的时候出现了问题,代码里设置的宽高是dx,需要先转成dp.下面以RelativeLayout作为例子,简单记录一下. ...

  2. Android的WebView控件载入网页显示速度慢的究极解决方案

    Android的WebView控件载入网页显示速度慢的究极解决方案 [转载来源自http://hi.baidu.com/goldchocobo/] Android客户端中混搭HTML页面,会出现虽然H ...

  3. android 动态设置padding,Android动态设置控件大小以及设定margin以及padding值

    http://www.aichengxu.com/Java/73893.htm Android动态设置控件大小以及设定margin以及padding值,有需要的朋友可以参考下. 一.概述 在andro ...

  4. android double比较大小吗,Android双向选择控件DoubleSeekBar使用详解

    本文实例为大家分享了Android双向选择控件DoubleSeekBar的使用方法,供大家参考,具体内容如下 先看效果图 1.DoubleSlideSeekBar public class Doubl ...

  5. android 画布裁剪,一种基于Android系统对UI控件进行轮廓剪裁及美化的方法与流程...

    本发明涉及Android应用的技术领域,特别涉及一种基于Android系统对UI控件进行轮廓剪裁及美化的方法. 背景技术: 目前,随着智能电视的普及,Android应用层出不穷,而那些表现形式单一.传 ...

  6. android按钮控件常见问题,Android的基本控件和Activity的应用总结

    Android的基本控件 常用界面控件 TextView 显示文本信息 button 普通按钮 EditText 可编辑的文本框组件(输入框) ImageView 用于显示图片 ImageBUtton ...

  7. android 自定义view控件,Android 自定义View——自定义View控件

    Android给我们提供了大量的View控件,但这还是远远满足不了我们的要求,有时候开发所需要的控件形式是在Android提供的控件中是不存在,这就需要我们自己去定义一个.那么如何自定义控件? 学习自 ...

  8. 微信小程序手把手教你实现类似Android中ViewPager控件效果

    微信小程序手把手教你实现类似Android中ViewPager控件效果 前言 需求分析 头部TAB 滑动的内容部分 最终版本 尾巴 前言 在做Android开发的时候,ViewPager是开发者使用频 ...

  9. android的webview控件载入网页显示速度慢的究极解决方案,【转】Android的WebView控件载入网页显示速度慢的究极解决方案...

    Android客户端中混搭HTML页面,会出现虽然HTML内容载入完成,标题也正常显示,但是整个网页需要等到近秒(甚至更多)时间才会显示出来.研究了很久,搜遍了国外很多网站,也看过PhoneGap的代 ...

  10. android studio 画控件,Android Studio 基础控件使用

    TextView android:gravity="center" //文字对其方式 top bottom left right center android:textColor= ...

最新文章

  1. 2018年中国高被引学者榜单发布,清华入榜学者数位列高校第一
  2. [CTO札记]防盗版新思路:招安
  3. 合成复用原则java实例_第7节 合成复用原则
  4. arrays中copyof_为什么阿里巴巴开发手册明确说明 Arrays.asList() 不能使用其修改方法?...
  5. VS2015配置opencv教程(图文详解)
  6. Linux lvs 多端口组成
  7. 探探自动配对PHP_CentOS7 - 安装Apache HTTP Server和PHP
  8. MFC关键技术-消息映射机制
  9. 如何查看OpenCV源码
  10. .net vue漂亮登录界面_6个宝藏级Vue管理后台框架 必须收藏
  11. IT运维的五大基础知识
  12. windows功能正在搜索需要的文件_拥有高效的搜索工具Everything,可以丢开Windows的搜索功能了...
  13. 怎么在mac上更改移动硬盘的权限
  14. 论文撰写-LaTex 教程+模板
  15. CAD看图软件中如何将CAD图纸由天正T20版本转换为T3版本?
  16. Python+PyQt5构建电影天堂电影搜索工具
  17. 中国四级标准行政区划 JSON
  18. 如何设置qq说说展示时间_QQ空间说说新增定时发表功能
  19. 实践|美创助力“云学堂”数据安全风险管控建设
  20. 【LeetCode每日一题】【2021/12/8】689. 三个无重叠子数组的最大和

热门文章

  1. 市场调研中的定性/定量分析
  2. 本题要求实现一个判断素数的简单函数、以及利用该函数计算给定区间内素数和的函数。
  3. 市政管理学(试题及答案)汇总
  4. 了解数据分析师,转行数据分析师,成为数据分析师
  5. 为什么马云最佩服「唐僧团队」?
  6. 中兴新支点操作系统上的文件小贴士
  7. 图基(Tukey)检验
  8. vooc充电原理_OPPO手机充电技术——VOOC技术原理
  9. 要闻丨腾飞•新征程,人大金仓完成近亿元融资
  10. Photoshop栅格化图层到底什么意思,什么时候该用栅格化涂层