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
谢谢阅读,烦请关注:

后续将持续更新!!

相关推荐
coder_pig4 小时前
🤡 公司Android老项目升级踩坑小记
android·flutter·gradle
死就死在补习班5 小时前
Android系统源码分析Input - InputReader读取事件
android
死就死在补习班5 小时前
Android系统源码分析Input - InputChannel通信
android
死就死在补习班5 小时前
Android系统源码分析Input - 设备添加流程
android
死就死在补习班5 小时前
Android系统源码分析Input - 启动流程
android
tom4i5 小时前
Launcher3 to Launchpad 01 布局修改
android
雨白6 小时前
OkHttpClient 核心配置详解
android·okhttp
淡淡的香烟6 小时前
Android auncher3实现简单的负一屏功能
android
RabbitYao6 小时前
Android 项目 通过 AndroidStringsTool 更新多语言词条
android·python
RabbitYao6 小时前
使用 Gemini 及 Python 更新 Android 多语言 Excel 文件
android·python