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

相关推荐
青莲8431 天前
查找算法详解
android·前端
青莲8431 天前
排序算法详解
android·前端
zd2005721 天前
用摩斯密码「听」时间:一款安卓报时应用的诞生
android
不会写代码的猴子1 天前
Android17版本更新预览
android·android studio
用户41659673693551 天前
记一次深坑:RecyclerView + FlexboxLayoutManager 导致 canScrollVertically 误判的剖析与修复
android
Be for thing1 天前
Android 音频硬件(Codec / 喇叭 / 麦克风)原理 + 功耗与问题定位实战(手机 / 手表通用)
android·学习·智能手机·音视频
吉哥机顶盒刷机1 天前
S905L3A/L3AB芯片迎来安卓14新纪元:Sicha移植版固件深度评测与刷机指南
android·经验分享·刷机
一个天蝎座 白勺 程序猿1 天前
KingbaseES数据库MySQL兼容性解析:从TCO账本到“傻瓜式“迁移的密码
android·数据库·mysql·kingbasees
Be for thing1 天前
Android 存储硬件(RAM/UFS/eMMC)底层原理 + 性能 / 功耗测试实战
android·学习·智能硬件