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

相关推荐
我命由我123452 分钟前
Android Jetpack Compose - ModalNavigationDrawer、NavigationRail、PullToRefreshBox
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
侠客工坊26 分钟前
大模型落地移动端:解析侠客工坊端侧 Agent 的零拷贝(Zero-Copy)屏幕感知与空间映射
android·人工智能
北漂Zachary31 分钟前
Laravel7.x核心特性全解析
android
Grackers1 小时前
Android Perfetto 系列 8:深入理解 Vsync 机制与性能分析
android
xinhuanjieyi1 小时前
php给30支NBA球队添加logo图标,做好对应关系
android·开发语言·php
jian110582 小时前
Android studio会自动下载高版本的tools
android·ide·android studio
Trouvaille ~2 小时前
【MySQL篇】内外连接:多表关联的完整指南
android·数据库·mysql·面试·后端开发·dql·内外连接
Zender Han2 小时前
VS Code 开发 Flutter 常用快捷键和插件工具详解
android·vscode·flutter·ios
wfsm2 小时前
安卓环境配置
android
于慨2 小时前
flutter安卓调试工具
android·flutter