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

谢谢阅读,烦请关注:

后续将持续更新!!

相关推荐
触想工业平板电脑一体机2 小时前
【触想智能】工业安卓一体机在人工智能领域上的市场应用分析
android·人工智能·智能电视
2501_915921433 小时前
iOS 是开源的吗?苹果系统的封闭与开放边界全解析(含开发与开心上架(Appuploader)实战)
android·ios·小程序·uni-app·开源·iphone·webview
allk554 小时前
OkHttp源码解析(一)
android·okhttp
allk554 小时前
OkHttp源码解析(二)
android·okhttp
2501_915909067 小时前
原生 iOS 开发全流程实战,Swift 技术栈、工程结构、自动化上传与上架发布指南
android·ios·小程序·uni-app·自动化·iphone·swift
2501_915909067 小时前
苹果软件混淆与 iOS 代码加固趋势,IPA 加密、应用防反编译与无源码保护的工程化演进
android·ios·小程序·https·uni-app·iphone·webview
2501_916007477 小时前
苹果软件混淆与 iOS 应用加固实录,从被逆向到 IPA 文件防反编译与无源码混淆解决方案
android·ios·小程序·https·uni-app·iphone·webview
介一安全7 小时前
【Frida Android】基础篇6:Java层Hook基础——创建类实例、方法重载、搜索运行时实例
android·java·网络安全·逆向·安全性测试·frida
沐怡旸10 小时前
【底层机制】【Android】深入理解UI体系与绘制机制
android·面试
啊森要自信11 小时前
【GUI自动化测试】YAML 配置文件应用:从语法解析到 Python 读写
android·python·缓存·pytest·pip·dash