Android把源Bitmap中心缩放到固定宽高的尺寸,Kotlin

Android把源Bitmap中心缩放到固定宽高的尺寸,Kotlin

如果源Bitmap最大的宽高大于指定的maxLength,则把源Bitmap的最大一个边(宽或高),中心缩放,否则,直接返回源Bitmap:

Kotlin 复制代码
    fun fitCenterTo(srcBmp: Bitmap, targetW: Int, targetH: Int, maxLength: Int): Bitmap {
        var bmp: Bitmap?

        val max = Math.max(srcBmp.width, srcBmp.height)
        if (max > maxLength) {
            var w: Int
            var h: Int
            var scale: Float

            if (srcBmp.width > srcBmp.height) {
                scale = srcBmp.width.toFloat() / maxLength

                w = maxLength
                h = (srcBmp.height.toFloat() / scale).roundToInt()
            } else {
                scale = srcBmp.height.toFloat() / maxLength

                w = (srcBmp.width.toFloat() / scale).roundToInt()
                h = maxLength
            }

            bmp = srcBmp.scale(w, h, false)
            return bmp
        } else {
            return srcBmp
        }
    }

一般处理场景:把宽高中某一条边或者两条边均大于maxLenght的源Bitmap,放到maxLenght的正方形格子。

Android Matrix画布Canvas缩放scale,Kotlin_kotlin canvas-CSDN博客文章浏览阅读638次,点赞9次,收藏7次。文章浏览阅读9.6k次。文章浏览阅读1.8k次。/*Java代码 将Drawable转化为Bitmap */ Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth();Android Material Design :LinearLayoutCompat添加分割线divider_linearlayout 分割线-CSDN博客。_kotlin canvashttps://blog.csdn.net/zhangphil/article/details/135114661Android Bitmap裁剪/压缩/缩放到限定的最大宽高值,Kotlin_bitmap缩放到指定大小-CSDN博客文章浏览阅读1.7k次,点赞18次,收藏21次。文章介绍了如何在Android应用中使用Kotlin处理Bitmap,包括裁剪到最大尺寸、压缩和按指定宽高缩放。内容涵盖了如何获取Bitmap的尺寸,以及在ImageView中优化加载大图片时的性能和内存管理,使用了`ThumbnailUtils`进行图片变换和缩略图制作。https://blog.csdn.net/zhangphil/article/details/134693021

相关推荐
不吃凉粉4 小时前
Android Studio USB串口通信
android·ide·android studio
zhangphil4 小时前
android studio设置大内存,提升编译速度
android·android studio
编程乐学5 小时前
安卓非原创--基于Android Studio 实现的天气预报App
android·ide·android studio·课程设计·大作业·天气预报·安卓大作业
纳于大麓7 小时前
Kotlin基础语法
linux·windows·kotlin
大熊的瓜地7 小时前
Android automotive 框架
android·android car
私人珍藏库8 小时前
[Android] Alarm Clock Pro 11.1.0一款经典简约个性的时钟
android·时钟
消失的旧时光-194310 小时前
ScheduledExecutorService
android·java·开发语言
小糖学代码10 小时前
MySQL:14.mysql connect
android·数据库·mysql·adb
怪兽201412 小时前
请谈谈什么是同步屏障?
android·面试
帅锅锅00713 小时前
SeLinux 全面详解
android·linux