Android 顶部对齐宽度撑满高度等比例缩放及限制最大最小高度

本文主要是介绍Android 顶部对齐宽度撑满高度等比例缩放及限制最大最小高度,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一 示例
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二 代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><com.example.test.ui.video.ui.image.topfit.TopFitImageViewandroid:id="@+id/imageView"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/purple_200"android:scaleType="fitXY" /><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="largeWidthStretch"android:text="2/1宽高比" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="equalWidthStretch"android:text="1/1宽高比" /></LinearLayout></FrameLayout>
package com.example.test.ui.video.ui.image.topfitimport android.content.Context
import android.graphics.Bitmap
import android.util.AttributeSetclass TopFitImageView : androidx.appcompat.widget.AppCompatImageView {constructor(context: Context?) : super(context!!)constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs)constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context!!,attrs,defStyleAttr)val minWHRatio = 1val maxWHRatio = 2override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {val drawable = drawableif (drawable != null) {val whRatio = drawable.intrinsicWidth.toFloat() / drawable.intrinsicHeight.toFloat()val width = MeasureSpec.getSize(widthMeasureSpec)var height = (width / whRatio).toInt()if (whRatio > maxWHRatio) {height = width / maxWHRatio} else if (whRatio < minWHRatio) {height = width}setMeasuredDimension(width, height)} else {super.onMeasure(widthMeasureSpec, heightMeasureSpec)}}override fun setImageBitmap(bm: Bitmap?) {if (bm != null && bm.width > 0 && bm.height > 0) {var height = bm.heightval whRatio = bm.width / bm.height
//            if (whRatio > maxWHRatio) {
//                height = bm.width / maxWHRatio
//            } elseif (whRatio < minWHRatio) {height = bm.width}super.setImageBitmap(Bitmap.createBitmap(bm, 0, 0, bm.width, height))return}super.setImageBitmap(bm)}}

这篇关于Android 顶部对齐宽度撑满高度等比例缩放及限制最大最小高度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

go动态限制并发数量的实现示例

《go动态限制并发数量的实现示例》本文主要介绍了Go并发控制方法,通过带缓冲通道和第三方库实现并发数量限制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录带有缓冲大小的通道使用第三方库其他控制并发的方法因为go从语言层面支持并发,所以面试百分百会问到

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

全面解析MySQL索引长度限制问题与解决方案

《全面解析MySQL索引长度限制问题与解决方案》MySQL对索引长度设限是为了保持高效的数据检索性能,这个限制不是MySQL的缺陷,而是数据库设计中的权衡结果,下面我们就来看看如何解决这一问题吧... 目录引言:为什么会有索引键长度问题?一、问题根源深度解析mysql索引长度限制原理实际场景示例二、五大解决

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

c/c++的opencv图像金字塔缩放实现

《c/c++的opencv图像金字塔缩放实现》本文主要介绍了c/c++的opencv图像金字塔缩放实现,通过对原始图像进行连续的下采样或上采样操作,生成一系列不同分辨率的图像,具有一定的参考价值,感兴... 目录图像金字塔简介图像下采样 (cv::pyrDown)图像上采样 (cv::pyrUp)C++ O