the*_*e64 9 android kotlin android-jetpack-compose
目前正在使用Modifier#onGloballyPositioned
\'sLayoutCoordinates
来查找可组合项是否在视口中。
val configuration = LocalConfiguration.current\nModifier.onGloballyPositioned { layoutCoordinates ->\n val (width, height) = layoutCoordinates.size\n val (x1, y1) = layoutCoordinates.positionInRoot()\n val x2 = x1 + width\n val y2 = y1 + height\n val screenWidth = configuration.screenWidthDp.toPx\n val screenHeight = configuration.screenHeightDp.toPx\n val isFullyVisible = (x1 >= 0 && y1 >= 0) && (x2 <= screenWidth && y2 <= screenHeight)\n println("isFullyVisible : $id -> $isFullyVisible")\n}\n\n// To convert Dp to Px\nval Number.toPx\n get() = TypedValue.applyDimension(\n TypedValue.COMPLEX_UNIT_DIP,\n this.toFloat(),\n Resources.getSystem().displayMetrics\n )\n
Run Code Online (Sandbox Code Playgroud)\n它可以工作,但我想知道是否有\xe2\x80\x99s任何更好的内置API可用于执行此操作。如果不是,我是否需要对上述方法进行任何优化?
\n 归档时间: |
|
查看次数: |
1894 次 |
最近记录: |