Android Jetpack Compose中奖两张图片合并成一张 Bitmap

谢谢关注!!

前言:华为开发学习专栏。如需了解谢谢查阅: http://t.csdnimg.cn/01PQ2

一、直接上代码

1、可裁剪:cropBitmapCenter

2、根据 left、top参数设置覆盖在上面的 bitmap的位置:

left = 15

top = 8

Kotlin 复制代码
@Composable
fun MergeImageToBitmap(type: Int, imageResOne: Int, imageResTwo: Int) :Bitmap{
    // 获取上下文
    val context = LocalContext.current

    // 等待图片加载完成
    val bitmap1 = BitmapFactory.decodeResource(context.resources, imageResOne)
    val bitmap2 = BitmapFactory.decodeResource(context.resources, imageResTwo)
    var left = 15
    var top = 8
    var croppedBitmap = cropBitmapCenter(bitmap1, (bitmap2.width / 6) * 4, bitmap2.height - 20)
    var croppedBitmap2 = cropBitmapCenter(bitmap2, (bitmap2.width / 7) * 5, bitmap2.height)
    when (type) {
        0 -> {
             left = 15
             top = 8
            croppedBitmap = cropBitmapCenter(bitmap1, (bitmap2.width / 6) * 4, bitmap2.height - 20)
            croppedBitmap2 = cropBitmapCenter(bitmap2, (bitmap2.width / 7) * 5, bitmap2.height)
        }

        1 -> {
             left = 60
             top = 60
            croppedBitmap = cropBitmapCenter(bitmap1, (bitmap2.width /7) * 4, (bitmap2.width / 7) *4)
            croppedBitmap2 = cropBitmapCenter(bitmap2, (bitmap2.width / 5) * 4, (bitmap2.width / 5) * 4)
        }

        2 -> {
            left = 30
            top = 23
            croppedBitmap = cropBitmapCenter(bitmap1, (bitmap2.width)-60 , ((bitmap2.height/7) * 4)-15)
            croppedBitmap2 = cropBitmapCenter(bitmap2, (bitmap2.width) , (bitmap2.height/ 6) * 4)
        }
    }


    // 确保两个 Bitmap 有相同的宽度和高度,或者根据需要调整
    val width = maxOf(bitmap1.width, bitmap2.width)
    val height = maxOf(bitmap1.height, bitmap2.height)

    // 创建一个新的 Bitmap 来容纳合并后的图像
    val mergedBitmap = Bitmap.createBitmap(bitmap2.width, bitmap2.height, Bitmap.Config.ARGB_8888)

    // 使用 Canvas 将两个 Bitmap 绘制到合并的 Bitmap 上
    Canvas(mergedBitmap).apply {
        drawBitmap(croppedBitmap, left.toFloat(), top.toFloat(), null)
        drawBitmap(croppedBitmap2, 0f, 0f, null) // 在第一个图像下方绘制第二个图像
    }

//    val viewModel = viewModel<BaseViewMode>(factory = MyViewModelFactory)
//    viewModel.setBitmap(mergedBitmap)
   return mergedBitmap
}

@Composable
fun cropBitmapCenter(bitmap: Bitmap, desiredWidth: Int, desiredHeight: Int): Bitmap {
    // 计算原始Bitmap的中心点
    val centerX = bitmap.width / 2
    val centerY = bitmap.height / 2
    // 计算裁剪区域的左上角坐标
    val left = centerX - desiredWidth / 2
    val top = centerY - desiredHeight / 2
    // 创建裁剪区域
    val cropRect = Rect(left, top, left + desiredWidth, top + desiredHeight)
    // 创建一个新的Bitmap对象,用于存放裁剪后的图像
    val croppedBitmap = Bitmap.createBitmap(desiredWidth, desiredHeight, bitmap.config)
    // 开始裁剪
    val canvas = Canvas(croppedBitmap)
    canvas.drawBitmap(bitmap, cropRect, Rect(0, 0, desiredWidth, desiredHeight), null)
    // 返回裁剪后的Bitmap
    return croppedBitmap
}
HarmonyOs开发更新中:http://t.csdnimg.cn/vVSkV

谢谢阅读,烦请关注:

后续将持续更新!!

相关推荐
Android小码家43 分钟前
framework&framework-minus-apex&android.jar的区别
android·python·jar
技术风向解码者1 小时前
安卓跨端选型:为什么优先看 Kuikly
android
科小怪1 小时前
大肥鱼桌宠下载(2026最新版):在安卓桌面上养一条会走会弹会碎碎念的胖鱼
android
qq3621967051 小时前
安卓安装包完整性校验:怎么确认下载的 APK 没被动过手脚
android
FLJwu1 小时前
《运动相机硬件防护选型实战》专栏|第二篇(PPTC 自恢复保险丝)
android·经验分享·数码相机·1024程序员节·运动相机
驰骋工作流12 小时前
流程时效考核的表结构:从待办时限到 WF_CH 考核结果驰骋BPM驰骋低代码低代码工作流引擎
android·低代码·rxjava
传奇开心果编程13 小时前
【Flutter踩坑排雷】第7课:巨型 build 函数坑|全部 UI 写在同一个 build,维护难、隐性异常
android·flutter·ios·移动开发·跨平台·dart·声明式ui
方白羽17 小时前
Android 双开环境底层剖析与风控检测落地
android·安全·app
weixin_5633714717 小时前
Android Studio 2024 版 Device Explorer 打开方法
android·ide·android studio
AIsoft_868920 小时前
职场音频文件怎么整理?录音APP有哪些用法
android·音视频