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

相关推荐
2501_9159090611 小时前
uni-app iOS 性能监控与调试全流程:多工具协作的实战案例
android·ios·小程序·https·uni-app·iphone·webview
魔鬼辣不够辣11 小时前
ADB图片上传轮播
android·adb·kotlin
踏雪羽翼11 小时前
Android 16k页面大小适配
android
用户0911 小时前
Kotlin后端开发指南
android·后端
狂浪天涯12 小时前
Android Security | User-Group-Others 权限模型
android
咖啡の猫13 小时前
Android开发-设计规范
android·设计规范
CYRUS STUDIO15 小时前
Frida + FART 联手:解锁更强大的 Android 脱壳新姿势
android·逆向
阿华的代码王国18 小时前
【Android】Retrofit2发起GET请求 && POST请求
android·retrofit2
私人珍藏库18 小时前
[Android] 京墨 v1.15.2 —— 古诗词文、汉语字典、黄历等查询阅读学习宝典(可离线)
android·学习·安卓