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

相关推荐
robotx3 小时前
安卓线程相关
android
消失的旧时光-19433 小时前
Android 面试高频:JSON 文件、大数据存储与断电安全(从原理到工程实践)
android·面试·json
dalancon4 小时前
VSYNC 信号流程分析 (Android 14)
android
dalancon4 小时前
VSYNC 信号完整流程2
android
dalancon4 小时前
SurfaceFlinger 上帧后 releaseBuffer 完整流程分析
android
用户69371750013845 小时前
不卷AI速度,我卷自己的从容——北京程序员手记
android·前端·人工智能
程序员Android6 小时前
Android 刷新一帧流程trace拆解
android
墨狂之逸才6 小时前
解决 Android/Gradle 编译报错:Comparison method violates its general contract!
android
阿明的小蝴蝶7 小时前
记一次Gradle环境的编译问题与解决
android·前端·gradle
汪海游龙7 小时前
开源项目 Trending AI 招募 Google Play 内测人员(12 名)
android·github