【发布时间】:2022-01-11 21:52:44
【问题描述】:
我知道可以使用自定义视图而不是 Google 地图中的基本地图标记来执行this answer 中所述的操作。我很好奇是否可以使用 Compose 实现类似的效果?
由于ComposeView 是最终类,因此无法直接扩展它,所以我想有一个FrameLayout 可以将它添加为一个孩子。尽管这似乎导致了竞争条件,因为可组合项的绘制与普通 android Views 略有不同。
class MapMarkerView : FrameLayout {
constructor(context: Context) : super(context) {
initView()
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
initView()
}
private fun initView() {
val composeView = ComposeView(context).apply {
layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
}
composeView.setContent {
// this runs asynchronously making the the bitmap generation not include composable at runtime?
Text(text = "2345", modifier = Modifier.background(Color.Green, RoundedCornerShape(4.dp)))
}
addView(composeView)
}
}
我读过的大多数文章都有一些回调函数来生成像this 这样的位图,这在每个视图都是动态生成并且需要立即为地图转换为位图的用例中不一定有效。
【问题讨论】:
标签: android google-maps android-jetpack-compose