KlassenKonstantin/ComposePhysicsLayout
关于
基于物理引擎的自定义 Compose 布局。
科学与工程可以做什么
- I don't think Compose was made to display hundreds of Composables at the same time. So maybe it's not a good idea to build a particle system out of this.
- In general, what is true for all of Compose is especially true for this Layout: Release builds perform way better than debug builds.
- State is not restored on config changes 😱.
- Currently there is no way to observe bodies / collosions / etc.
实测星标增长
- 24小时
- 0
- 7天
- -1
- 15天
- -1
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文Physics Layout
This library offers a dyn4j wrapper for Jetpack Compose.
🚧 Experimental 🚧
Before reaching version 1.0, this library is considered experimental, which means that there is no guaranteed backwards compatibility between versions. Signatures, interfaces, names, etc. may and will most likely change.
Sample App
https://user-images.githubusercontent.com/1836066/206856910-d2172e7e-64da-454e-99b9-8171cf5f5eeb.mov
Download
dependencies {
implementation 'io.github.klassenkonstantin:physics-layout:<version>'
}
How to use
To get started, create a PhysicsLayout and add arbitrary content to it. Add the physicsBody modifier to Composables that should be part of the physics simulation.
PhysicsLayout
@Composable
fun PhysicsLayout(
modifier: Modifier = Modifier,
shape: Shape? = RectangleShape,
scale: Dp = DEFAULT_SCALE,
simulation: Simulation = rememberSimulation(),
content: @Composable BoxScope.() -> Unit
)
shape: The shape of the outer border of thePhysicsLayoutscale: How many Dps should be considered one meter. Bodies shouldn't be smaller than one metersimulation: Does the mapping between layout and physics enginecontent: The arbitrary layout
physicsBody modifier
fun Modifier.physicsBody(
id: String? = null,
shape: Shape = RectangleShape,
bodyConfig: BodyConfig = BodyConfig(),
dragConfig: DragConfig? = null,
)
id: The id the body should have in the simulation. Useful for operations that act directly on bodies (not yet supported).shape: Describes the outer bounds of the body. Supported shapes are:bodyConfig: Configures properties of the bodydragConfig: Set aDragConfigto enable drag support, ornullto disable dragging
Clock
By default Simulation uses a default Clock which automatically starts running. To pause and resume a Clock, create an instance with rememberClock() and pass that to the Simulation.
Example usage
@Composable
fun SimpleScreen() {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
PhysicsLayout {
Card(
modifier = Modifier.physicsBody(
shape = CircleShape,
).align(Alignment.Center),
shape = CircleShape,
) {
Icon(
modifier = Modifier
.size(32.dp)
.padding(4.dp),
imageVector = Icons.Default.Star,
contentDescription = "Star",
tint = Color.White
)
}
}
}
}
This example adds a ball with a star in the center of the layout, which then starts falling to the ground.
Note: The
shapemust be set on both the body modifier and theCard.
Change gravity
If you need to change the gravity of the simulated world, use Simulation.setGravity
Caveats, notes, missing features
- I don't think Compose was made to display hundreds of Composables at the same time. So maybe it's not a good idea to build a particle system out of this.
- In general, what is true for all of Compose is especially true for this Layout: Release builds perform way better than debug builds.
- State is not restored on config changes 😱.
- Currently there is no way to observe bodies / collosions / etc.
- Not tested with scrolling containers
探索相关项目
加载中…