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

后续将持续更新!!

相关推荐
aningxiaoxixi13 分钟前
Android 之 audiotrack
android
枷锁—sha15 分钟前
【DVWA系列】——CSRF——Medium详细教程
android·服务器·前端·web安全·网络安全·csrf
Cao_Shixin攻城狮4 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
呼啦啦呼啦啦啦啦啦啦7 小时前
利用pdfjs实现的pdf预览简单demo(包含翻页功能)
android·javascript·pdf
idjl8 小时前
Mysql测试题
android·adb
游戏开发爱好者811 小时前
iOS App 电池消耗管理与优化 提升用户体验的完整指南
android·ios·小程序·https·uni-app·iphone·webview
人生游戏牛马NPC1号11 小时前
学习 Flutter (四):玩安卓项目实战 - 中
android·学习·flutter
星辰也为你祝福h13 小时前
Android原生Dialog
android
梁同学与Android14 小时前
Android ---【CPU优化】需要优化的原因及优化的地方
android
Misha韩14 小时前
React Native 基础tabBar和自定义tabBar - bottom-tabs
android·react native