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

相关推荐
二流小码农30 分钟前
鸿蒙开发:个人开发者如何使用华为账号登录
android·ios·harmonyos
StarShip40 分钟前
Android View框架概览
android·计算机图形学
愤怒的代码41 分钟前
解析Android内存分析的指标
android·app
summerkissyou198744 分钟前
android-hardware/interfaces/automotive和hardware/libhardware/include/hardware区别
android
Evan芙1 小时前
mysql二进制部署以及多实例部署
android·数据库·mysql
走在路上的菜鸟2 小时前
Android学Dart学习笔记第二十三节 类-扩展类型
android·笔记·学习·flutter
百***78752 小时前
【技术教程】3步极速接入GPT-5.1:零门槛体验多模态AI能力
android·java·人工智能·gpt·opencv
走在路上的菜鸟3 小时前
Android学Dart学习笔记第二十一节 类-点的简写
android·笔记·学习·flutter
QQ12958455043 小时前
ThingsBoard-修改Android APP应用程序名和描述
android·物联网·iot
代码代码快快显灵3 小时前
Android项目架构深度解析
android