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

后续将持续更新!!

相关推荐
沐怡旸3 小时前
【底层机制】垃圾回收(GC)底层原理深度解析
android·面试
whatever who cares3 小时前
android/java中gson的用法
android·java·开发语言
用户0273851840263 小时前
【Android】活动的正/异常生命周期和启动模式、标志位详解
android
nono牛5 小时前
MTK平台详解`adb devices`输出的序列号组成
android·linux·adb·智能手机
zhangphil5 小时前
Android通过SQL查询trace分析进程启动线程总数量
android
下位子5 小时前
『OpenGL学习滤镜相机』- Day3: 着色器基础 - GLSL 语言
android·opengl
bqliang5 小时前
Jetpack Navigation 3:领航未来
android·android studio·android jetpack
云存储小天使6 小时前
安卓蛙、苹果蛙为什么难互通?
android
陈大头铃儿响叮当8 小时前
Android Studio升级后,Flutter运行android设备报错
android·flutter·android studio
勤劳打代码8 小时前
isar_flutter_libs 引发 Namespace not specified
android·flutter·groovy