谢谢关注!!
前言:上一篇文章主要介绍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
谢谢阅读,烦请关注:
后续将持续更新!!