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

谢谢关注!!

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

一、使用 AndroidView

//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) }

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

后续将持续更新!!

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

No Promises﹉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值