Android Compose使用AndroidView,并将 AndroidView 保存成图片。

谢谢关注!!

前言:上一篇文章主要介绍HarmonyOs开发之---------容器组件使用 http://t.csdnimg.cn/r9Qd1

一、使用 AndroidView

javascript 复制代码
//images 是一个 uri数组
    var androidView by remember { mutableStateOf<View?>(null) }
     AndroidView(factory = {
                        FrameLayout(it)
                            .apply {
                                layoutParams = ViewGroup.LayoutParams(
                                    ViewGroup.LayoutParams.WRAP_CONTENT,
                                    ViewGroup.LayoutParams.WRAP_CONTENT
                                )
                                val composeVIew = ComposeView(context)
                                post {
                                    composeVIew.setContent {
                                        LazyColumn(
                                            modifier = Modifier
                                                .padding(top = 10.dp, bottom = 10.dp)
                                                .width(179.dp)
                                                .height(parentHeight.dp),
                                            horizontalAlignment = Alignment.CenterHorizontally,
                                            verticalArrangement = Arrangement.Center
                                        ) {
                                            items(images) { image ->
                                                val painter: Painter =
                                                    rememberAsyncImagePainter(image)
                                                Box(
                                                    modifier = Modifier
                                                        .padding(top = 5.dp, bottom = 5.dp)
                                                        .size(147.dp, 147.dp)
                                                        .clip(
                                                            RoundedCornerShape(10.dp)
                                                        )
                                                        .background(color = Color.Transparent)
                                                ) {
                                                    if (selectFilter != null) {
                                                        loadBitmapFromUri(context,image)?.let { it1 ->
                                                            Image(
                                                                bitmap = it1.asImageBitmap() ,
                                                                contentDescription = null,
                                                                modifier = Modifier.fillMaxSize(),
                                                                contentScale = ContentScale.Crop,
                                                                colorFilter = ColorFilter.colorMatrix(
                                                                    ColorMatrix(selectFilter!!)
                                                                )
                                                            )
                                                        }
                                                    } else {
                                                        loadBitmapFromUri(context,image)?.let { it1 ->
                                                            Image(
                                                                bitmap = it1.asImageBitmap(),
                                                                contentDescription = null,
                                                                modifier = Modifier.fillMaxSize(),
                                                                contentScale = ContentScale.Crop
                                                            )
                                                        }
                                                    }


                                                }

                                            }
                                        }

                                      
                                    }

                                }
                                this.addView(composeVIew, layoutParams)
                                androidView = this
                            }

                    })

二、将 AndroidView保存成 bitmap:val bitmap = androidView?.let { saveAndroidViewToBitmapTwo(it) }

javascript 复制代码
fun saveAndroidViewToBitmapTwo(view: View): Bitmap {
    view.measure(
        View.MeasureSpec.makeMeasureSpec(view.width, View.MeasureSpec.EXACTLY),
        View.MeasureSpec.makeMeasureSpec(view.height, View.MeasureSpec.EXACTLY)
    )
    view.layout(0, 0, view.measuredWidth, view.measuredHeight)

    val bitmap =
        Bitmap.createBitmap(view.measuredWidth, view.measuredHeight, Bitmap.Config.ARGB_8888)
    val canvas = android.graphics.Canvas(bitmap)
    view.draw(canvas)

    return bitmap
}

注意:当 AndroidView 中使用 Image组件是请注意,显示图片要使用 bitmap、或者资源文件,如果使用val painter: Painter = rememberAsyncImagePainter(image) //image是 Uri类型,会报错:java.lang.IllegalArgumentException: Software rendering doesn't support hardware bitmaps

如需了解更多请联系博主,本篇完。

下一篇:HarmonyOs开发之---------Video组件的使用、使用HTTP访问网络
HarmonyOs开发,学习专栏敬请试读订阅: http://t.csdnimg.cn/Btyoj
谢谢阅读,烦请关注:

后续将持续更新!!

相关推荐
唐诺1 小时前
Android Open Accessory (AOA) 协议完全解析
android·usb·aoa
我命由我123453 小时前
Android 开发 - 广播组件(标准广播、有序广播、静态注册广播、分钟到达广播、网络变更广播...)
android·java·开发语言·网络·java-ee·android studio·android-studio
A富得流油的咸鸭蛋3 小时前
安卓鸿蒙面试
android·面试·harmonyos
Coffeeee3 小时前
AGP9.0的主要变更项,给Gradle来一次大变样
android·前端·gradle
小孔龙3 小时前
Android GPU 渲染管线:一帧画面如何走上屏幕
android·性能优化·gpu
米码收割机4 小时前
【Android】Android 书澜图书馆信息管理系统(源码+文档)【独一无二】
android
恋猫de小郭4 小时前
KotlinLLM 开源 ,一个可以在运行时自己生成永久 Kotlin 代码的 Agent 库
android·前端·人工智能
游戏开发爱好者84 小时前
iOS应用加固方案全解析:源码加固与IPA在线加固对比
android·macos·ios·小程序·uni-app·cocoa·iphone
大鹏说大话4 小时前
PHP 异步处理方案:队列使用场景与 RabbitMQ 实操
android
molihuan5 小时前
最新 Paddle-Lite Android平台编译
android·ocr·paddle·推理·端侧·paddle lite