当前位置: 首页 > news >正文

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.TopFitImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/purple_200"
        android:scaleType="fitXY" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="largeWidthStretch"
            android:text="2/1宽高比" />

        <Button
            android: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.topfit

import android.content.Context
import android.graphics.Bitmap
import android.util.AttributeSet


class 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 = 1
    val maxWHRatio = 2

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val drawable = drawable
        if (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.height
            val whRatio = bm.width / bm.height
//            if (whRatio > maxWHRatio) {
//                height = bm.width / maxWHRatio
//            } else
            if (whRatio < minWHRatio) {
                height = bm.width
            }
            super.setImageBitmap(Bitmap.createBitmap(bm, 0, 0, bm.width, height))
            return
        }
        super.setImageBitmap(bm)
    }

}

相关文章:

  • 从互联网到云计算再到 AI 原生,百度智能云数据库的演进
  • MySQL执行语句 Table ‘mysql.servers‘ doesn‘t exist
  • Python爬取酷我音乐
  • 贵州开放大学形成性考核 平时作业 参考试题
  • Error opening file for writing报错解决
  • RocketMQ-RocketMQ高性能核心原理节点(流程图)
  • C++基础 -42- STL库之list链表
  • python:六种算法(DBO、RFO、WOA、GWO、PSO、GA)求解23个测试函数(python代码)
  • 从零开始搭建企业管理系统(六):RBAC 权限管理设计
  • 阿里云docker加速
  • 代码随想录 96. 不同的二叉搜索树
  • el-table 表格多选(后端接口搜索分页)实现已选中的记忆功能。实现表格数据和已选数据(前端分页)动态同步更新。
  • 结合ColorUI组件开发微信小程序
  • 公有云迁移研究——AWS Route53
  • el-table全部选择和全部取消
  • 源码安装git
  • 蓝牙物联网对接技术难点有哪些?
  • 【clickhouse】ck远程访问另一个ck
  • SpringDataJPA基础
  • 管理空闲存储空间
  • 一热就出汗 VS 热死都不出汗的人,哪个更健康?
  • 泽连斯基称与特朗普通话讨论停火事宜
  • 波音公司计划于2027年交付新版“空军一号”飞机
  • 碧桂园服务:拟向杨惠妍全资持有的公司提供10亿元贷款,借款将转借给碧桂园用作保交楼
  • 人们为何热衷谈论八卦?
  • 山东滕州车祸致6人遇难,肇事司机已被刑事拘留