为什么80%的码农都做不了架构师?>>>   

可以设置最大宽高的FrameLayout

支持相对父控件的半分比设置

默认优先比例设置

不支持参数小于零

MaxLayout.java

import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.WindowManager;/*** Created by wangxingsheng on 2018/6/8.** 可以设置最大宽高的FrameLayout* 默认优先比例设置* 不支持参数小于零**/
public class MaxLayout extends FrameLayout {private float mMaxHeightRatio = -1f;// 优先级高private float mMaxHeight = -1f;// 优先级低private float mMaxWidthRatio = -1f;// 优先级高private float mMaxWidth = -1f;// 优先级低private int parentWidth;private int parentHeight;private boolean firstHeightRatio = true;//高默认优先比例设置private boolean firstWidthRatio = true;//宽默认优先比例设置public MaxLayout(Context context) {super(context);}public MaxLayout(Context context, AttributeSet attrs) {super(context, attrs);initAttrs(context, attrs);}public MaxLayout(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);initAttrs(context, attrs);}private void initAttrs(Context context, AttributeSet attrs) {TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MaxLayout);if(a!=null){firstHeightRatio = a.getBoolean(R.styleable.MaxLayout_first_ratio_height,true);firstWidthRatio = a.getBoolean(R.styleable.MaxLayout_first_ratio_width,true);mMaxHeightRatio = a.getFloat(R.styleable.MaxLayout_max_height_ratio, -1f);mMaxHeight = a.getDimension(R.styleable.MaxLayout_max_height, -1f);mMaxWidthRatio = a.getFloat(R.styleable.MaxLayout_max_width_ratio, -1f);mMaxWidth = a.getDimension(R.styleable.MaxLayout_max_width, -1f);a.recycle();}}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {initParentWH();initWH();int heightMode = MeasureSpec.getMode(heightMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);int maxHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize <= mMaxHeight ? heightSize : (int) mMaxHeight, heightMode);int widthMode = MeasureSpec.getMode(widthMeasureSpec);int widthSize = MeasureSpec.getSize(widthMeasureSpec);int maxWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize <= mMaxWidth ? widthSize : (int) mMaxWidth, widthMode);super.onMeasure(maxWidthMeasureSpec, maxHeightMeasureSpec);}/*** 计算需要设置的宽高*/private void initWH() {if((firstHeightRatio && mMaxHeightRatio > 0)|| (!firstHeightRatio && mMaxHeight < 0)){mMaxHeight = mMaxHeightRatio * parentHeight;}if((firstWidthRatio && mMaxWidthRatio > 0)|| (!firstWidthRatio && mMaxWidth < 0)){mMaxWidth = mMaxWidthRatio * parentWidth;}}/*** 获取父控件的宽高*/private void initParentWH() {ViewGroup parent = (ViewGroup) getParent();if(null != parent){parentWidth = parent.getWidth();parentHeight = parent.getHeight();}else {WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);DisplayMetrics dm = new DisplayMetrics();if (wm != null) {wm.getDefaultDisplay().getMetrics(dm);parentWidth = dm.widthPixels;parentHeight = dm.heightPixels;}}}
}

attrs.xml

    <declare-styleable name="MaxLayout"><attr name="max_height_ratio" format="float"/><attr name="max_height" format="dimension"/><attr name="max_width_ratio" format="float"/><attr name="max_width" format="dimension"/><attr name="first_ratio_height" format="boolean"/><attr name="first_ratio_width" format="boolean"/></declare-styleable>

转载于:https://my.oschina.net/reone/blog/1826781

Android自定义View——可以设置最大宽高的FrameLayout相关推荐

  1. Android 自定义view onMeasure() 方法剖析

    接着上一篇自定义view 相关的,揭秘 Android Graphics2D 实现动态效果之--invalidate()   内容的介绍,这一篇主要介绍下自定义view 中的 onMeasure()方 ...

  2. android 自定义view 高度,自定义View之宽高的设置,全网最详解

    今天给大家带来的是自定义View,然后如何设置他的宽高,经常用自定义view的程序猿肯定都知道我们在给自定义view设置wrap_content或者match_parent,view都会占满全屏,就想 ...

  3. android自定义弧度按钮,Android 自定义View 绘制六边形设置按钮

    今天逛酷安的时候,发现酷安的设置按钮(截图的右上角),是一个六边形 + 中心圆的图标,所以又是一个自定义View练习对象了.画圆很简单,知道半径即可,而重点就在画出六边形. 酷安截图.png 最终效果 ...

  4. android 自定义特效,Android自定义View之高仿QQ健康

    我们都知道自定义View一般有三种直接继承View.继承原有的控件对控件的进行修改.重新拼装组合,最后一种主要针对于ViewGroup.具体的怎么做不是本文的所涉及的内容(本文是基于第一种方式实现的) ...

  5. android设置自定义按钮,Android自定义View之元素按钮

    Android自定义View之元素按钮 之前在dribbble看到的三个元素的按钮,参考了设计的创意,添加了自己定义的动画效果来实现.先看效果 效果图 分别是水火电三个元素的按钮实现.其中电的实现最简 ...

  6. Android自定义view,圆形的TextView,并通过xml设置属性,AttributeSet中取值

    Android自定义view设置xml属性 一个圆形的自定义TextView,通过xml来设置背景颜色的属性 values/attrs <declare-styleable name=" ...

  7. android 高仿 探探卡片滑动,Android自定义View仿探探卡片滑动效果

    Android自定义View仿探探卡片滑动这种效果网上有很多人已经讲解了实现思路,大多都用的是RecyclerView来实现的,但是我们今天来换一种实现思路,只用一个自定义的ViewGroup来搞定这 ...

  8. android自定义View之(六)------高仿华为荣耀3C的圆形刻度比例图(ShowPercentView)

    为什么写这篇文章: 显示当前的容量所占的比例,表现当前计划的进度,一般都会采用百分比的方式,而图形显示,以其一目了然的直观性和赏心悦目的美观形成为了我们的当然的首选. 在图形表示百分比的方法中,我们有 ...

  9. Android自定义View:ViewGroup(三)

    自定义ViewGroup本质是什么? 自定义ViewGroup本质上就干一件事--layout. layout 我们知道ViewGroup是一个组合View,它与普通的基本View(只要不是ViewG ...

最新文章

  1. fastjson反序列化漏洞研究(下)
  2. 剑指offer面试题17. 打印从1到最大的n位数
  3. 廖雪峰python教程官网-廖雪峰老师官方爬虫教程,13个案例带你全面入门
  4. Linux移植libmodbus
  5. (干货,建议收藏)备战2021年软考中级网络工程师-01计算机硬件基础
  6. Java使用Excel的问题:自动跳过空字段、中文加拼音和时间处理错误的解决方法
  7. GD32E230按键软件消抖程序
  8. 注意:2021教资面试详细流程与技巧记牢了(含资料)
  9. JENKINS中maven使用jdk8和jdk11环境
  10. 每天一大杯可乐,会不会骨质酥松哇?
  11. VC++6.0安装、编译NTL类库
  12. ThreadPoolTaskScheduler实现动态管理定时任务
  13. mui 本地化后调试_MUI和LIP-如何用您的语言用中文编写和本地化Windows
  14. Wind River workbench小结
  15. 密码包含-数字、大小写、特殊字符-正则表达式
  16. BLOCK层代码分析(10)IO下发之IO下发函数总结
  17. USB3.0/3.1信号完整性分析仿真
  18. 两直线垂直,斜率乘积为-1的证明
  19. “高调做事,高调做人”?----关于排名和排序
  20. 河北欧格教育:商家开店铺怎么运营

热门文章

  1. 代数式的书写规范八种_中考数学一轮复习-第2节代数式与整式运算
  2. python异步和多线程_Python 异步 IO(asyncio)、多进程、多线程性能对比
  3. Flask-SQLAlchemy的基本使用
  4. 【分享】虹软人脸识别应用开发过程
  5. 【纪中集训2019.3.26】动态半平面交
  6. mac下设置redis开机启动方法
  7. Rhythmk 一步一步学 JAVA(2) : 操作 MYSQL 数据库
  8. 你是否觉得.Net Web Service 中asmx文件是多余的?
  9. Ext 遍历树的所有节点
  10. 如何在ASP.NET服务器控件库中嵌入JavaScript脚本文件 [适用于.NET 2.0]