Android ImageView的Bitmap在scaleType情况下Bitmap顶部与底部RectF坐标,Kotlin

Android ImageView的Bitmap在scaleType情况下,Bitmap顶部与底部RectF坐标,Kotlin

通常,在ImageView设置scaleType后,Android会把原始图片通过缩放放在ImageView里面,例如:

XML 复制代码
    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@mipmap/p2" />

显示的结果:

ImageView的高度这里特意设置:

XML 复制代码
android:layout_height="match_parent"

撑满ImageView的高度,但ImageView里面展示的Bitmap宽度铺满了屏幕宽度,但Bitmap的高度和ImageView是不同的,顶部和底部有空隙。因为缩放的模式是:

XML 复制代码
        android:scaleType="fitCenter"

此时,如果想要获得ImageView里面通过scaleType已经缩放的Bitmap的top值或其他bottom是多少,则:

Kotlin 复制代码
    private fun getImageBounds(imageView: ImageView): RectF {
        val bounds = RectF()

        val drawable = imageView.drawable
        if (drawable != null) {
            imageView.imageMatrix.mapRect(bounds, RectF(drawable.bounds))
        }

        return bounds
    }

返回的RectF的top即ImageView里面的缩放后的Bitmap顶部距离ImageView顶部的值。

因为ImageView设置了高度为match_parent,且scaleType为fitCenter,导致里面的Bitmap顶部距离ImageView顶部有间隙。如果想消除这种间隙,也很简单,直接修改xml,把ImageView的高度从match_parent改为wrap_content:

XML 复制代码
    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@mipmap/p2" />

此时,ImageView里面的Bitmap移动最顶部,和ImageView的顶部一致了。

这样简单的处理,可以在有些场景下简化ImageView与ImageView里面的Bitmap宽高坐标位置计算。

Android把宽高均小于给定值的Bitmap放大到给定值,Kotlin-CSDN博客文章浏览阅读392次,点赞8次,收藏11次。文章浏览阅读5.3k次。《Android大图片之变换缩略图,以及对原始大图片按照指定宽、高裁剪成缩略图》在Android的ImageView加载图像资源过程中,出于性能和内存开销的需要,有时候需要把一个原始的超大图片按照一定比例等比例缩放成较小的缩略图,或者需要把原始的超大图片,裁剪成指定宽高值的较小图片,针对这种开发需求,可以使用Android SDK自身提供的工具类:ThumbnailUtils完成。假设拉伸放大到SIZE=2048。https://blog.csdn.net/zhangphil/article/details/134737075

Android通过scaleType裁剪缩放图片适配不同屏幕不同ImageView尺寸-CSDN博客文章浏览阅读6.1k次。Android通过scaleType裁剪缩放图片适配不同屏幕不同ImageView尺寸实验方法,先找一张不规则大图,尺寸:1024pix(宽) X 1465pix(高),原图如图:然后特意设置一个ImageView宽高均为600pix。观察不同scaleType配置下,Android对原图的裁剪缩放效果。左侧为代码,右侧为缩放和裁剪效果。(1)android:scaleType="fitXY"可...https://blog.csdn.net/zhangphil/article/details/80858726

相关推荐
BoomHe3 小时前
Now in Android 架构模式全面分析
android·android jetpack
二流小码农10 小时前
鸿蒙开发:上传一张参考图片便可实现页面功能
android·ios·harmonyos
鹏程十八少11 小时前
4.Android 30分钟手写一个简单版shadow, 从零理解shadow插件化零反射插件化原理
android·前端·面试
Kapaseker11 小时前
一杯美式搞定 Kotlin 空安全
android·kotlin
三少爷的鞋11 小时前
Android 协程时代,Handler 应该退休了吗?
android
火柴就是我1 天前
让我们实现一个更好看的内部阴影按钮
android·flutter
FunnySaltyFish1 天前
什么?Compose 把 GapBuffer 换成了 LinkBuffer?
算法·kotlin·android jetpack
砖厂小工1 天前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
张拭心1 天前
春节后,有些公司明确要求 AI 经验了
android·前端·人工智能
张拭心1 天前
Android 17 来了!新特性介绍与适配建议
android·前端