实现正方形布局SquareLayout的几种方法

2024-05-04 12:38

本文主要是介绍实现正方形布局SquareLayout的几种方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

下面介个方法都是复写 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
方法1

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));  // Children are just made to fill our space.  int childWidthSize = getMeasuredWidth();  int childHeightSize = getMeasuredHeight();  //高度和宽度一样  heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);  super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
}

方法2

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, widthMeasureSpec);/重写此方法后默认调用父类的onMeasure方法,分别将宽度测量空间与高度测量空间传入
super.onMeasure(widthMeasureSpec, heightMeasureSpec);/}

方法3

*/
public class SquareLayout extends FrameLayout {private int orientation = LinearLayout.HORIZONTAL;public SquareLayout(Context context) {super(context);}private void init(Context context, AttributeSet attrs, int defStyleAttr) {TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SquareLayout, defStyleAttr, 0);int index = typedArray.getInt(R.styleable.SquareLayout_square_orientation, -1);
//        int index = MyInputConnectionWrapper.getInt(com.android.internal.R.styleable.LinearLayout_orientation, -1);if (index >= 0) {setOrientation(index);}typedArray.recycle();}public SquareLayout(Context context, AttributeSet attrs) {super(context, attrs);init(context, attrs, 0);}public SquareLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs, defStyleAttr);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(orientation == LinearLayout.HORIZONTAL ? widthMeasureSpec : heightMeasureSpec, orientation == LinearLayout.HORIZONTAL ? widthMeasureSpec : heightMeasureSpec);super.onMeasure(widthMeasureSpec, heightMeasureSpec);//核心就是下面这块代码块啦
//        int width = orientation == LinearLayout.HORIZONTAL ? getMeasuredWidth() : getMeasuredHeight();if (orientation == LinearLayout.HORIZONTAL) {setMeasuredDimension(widthMeasureSpec, widthMeasureSpec);} else {setMeasuredDimension(heightMeasureSpec, heightMeasureSpec);}int width = getMeasuredWidth();int height = getMeasuredHeight();ViewGroup.LayoutParams lp = getLayoutParams();lp.height = orientation == LinearLayout.HORIZONTAL ? width : width;lp.width = orientation == LinearLayout.HORIZONTAL ? width : height;setLayoutParams(lp);}public void setOrientation(int orientation) {this.orientation = orientation;}}

我本人用第一种和第三种方法,不过都发现在某些情况下会出现毛病,第二种方法没用过

官方的方式

*/
public class SquareFrameLayout extends FrameLayout {public SquareFrameLayout(Context context) {super(context);}public SquareFrameLayout(Context context, AttributeSet attrs) {super(context, attrs);}public SquareFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public SquareFrameLayout(Context context, AttributeSet attrs,int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {final int widthSize = MeasureSpec.getSize(widthMeasureSpec);final int heightSize = MeasureSpec.getSize(heightMeasureSpec);if (widthSize == 0 && heightSize == 0) {// If there are no constraints on size, let FrameLayout measuresuper.onMeasure(widthMeasureSpec, heightMeasureSpec);// Now use the smallest of the measured dimensions for both dimensionsfinal int minSize = Math.min(getMeasuredWidth(), getMeasuredHeight());setMeasuredDimension(minSize, minSize);return;}final int size;if (widthSize == 0 || heightSize == 0) {// If one of the dimensions has no restriction on size, set both dimensions to be the// on that doessize = Math.max(widthSize, heightSize);} else {// Both dimensions have restrictions on size, set both dimensions to be the// smallest of the twosize = Math.min(widthSize, heightSize);}final int newMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);super.onMeasure(newMeasureSpec, newMeasureSpec);}
}

这篇关于实现正方形布局SquareLayout的几种方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/959216

相关文章

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

macOS无效Launchpad图标轻松删除的4 种实用方法

《macOS无效Launchpad图标轻松删除的4种实用方法》mac中不在appstore上下载的应用经常在删除后它的图标还残留在launchpad中,并且长按图标也不会出现删除符号,下面解决这个问... 在 MACOS 上,Launchpad(也就是「启动台」)是一个便捷的 App 启动工具。但有时候,应

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义

opencv图像处理之指纹验证的实现

《opencv图像处理之指纹验证的实现》本文主要介绍了opencv图像处理之指纹验证的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录一、简介二、具体案例实现1. 图像显示函数2. 指纹验证函数3. 主函数4、运行结果三、总结一、

Springboot处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

基于SpringBoot实现文件秒传功能

《基于SpringBoot实现文件秒传功能》在开发Web应用时,文件上传是一个常见需求,然而,当用户需要上传大文件或相同文件多次时,会造成带宽浪费和服务器存储冗余,此时可以使用文件秒传技术通过识别重复... 目录前言文件秒传原理代码实现1. 创建项目基础结构2. 创建上传存储代码3. 创建Result类4.

SpringBoot日志配置SLF4J和Logback的方法实现

《SpringBoot日志配置SLF4J和Logback的方法实现》日志记录是不可或缺的一部分,本文主要介绍了SpringBoot日志配置SLF4J和Logback的方法实现,文中通过示例代码介绍的非... 目录一、前言二、案例一:初识日志三、案例二:使用Lombok输出日志四、案例三:配置Logback一

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

Python+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专