New Backdrop API, deprecate liquid-glass module
This commit is contained in:
1 parent
3e66f84d9b
commit
bbca9c23d4
42 files changed
+1195
-2091
No files matched your search
@@ -34,6 +34,103 @@ implementation("com.github.Kyant0:AndroidLiquidGlass:<version>")
|
||||
|
||||
### Examples
|
||||
|
||||
### After 1.0.0-alpha09
|
||||
|
||||
We use the new `Backdrop` API introduced in 1.0.0-alpha09 to implement the glass effect.
|
||||
You can do more with the new API, not only the glass effect, but also the blur effect, magnifier, etc.
|
||||
|
||||
#### Basic example
|
||||
|
||||
```kotlin
|
||||
val backdrop = rememberLayerBackdrop(backgroundColor = Color.White)
|
||||
|
||||
Box {
|
||||
// backdrop content used to fill the backdrop layer
|
||||
Box(Modifier.backdrop(backdrop))
|
||||
|
||||
// icon button with glass effect
|
||||
Box(
|
||||
Modifier
|
||||
// [optional] drop shadow effect
|
||||
.backdropShadow(CircleShape)
|
||||
// draw the backdrop with glass effect
|
||||
.drawBackdrop(backdrop) {
|
||||
// set the shape
|
||||
shape = CircleShape
|
||||
|
||||
// apply visual effects
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
saturate()
|
||||
blur(2f.dp)
|
||||
// here is the glass effect
|
||||
refraction(height = 8f.dp.toPx(), amount = size.minDimension)
|
||||
}
|
||||
|
||||
// [optional] draw a brilliant highlight
|
||||
drawHighlight()
|
||||
}
|
||||
.clickable {}
|
||||
.size(48.dp),
|
||||
contentAlignment = Alignment.Center
|
||||
) {}
|
||||
}
|
||||
```
|
||||
|
||||
#### Advanced example
|
||||
|
||||
```kotlin
|
||||
val backdrop = rememberLayerBackdrop(backgroundColor = Color.White)
|
||||
|
||||
Box {
|
||||
// backdrop content used to fill the backdrop layer
|
||||
Box(Modifier.backdrop(backdrop))
|
||||
|
||||
// [optional] scale if needed
|
||||
val originalScale = 1f
|
||||
|
||||
// icon button with glass effect
|
||||
Box(
|
||||
Modifier
|
||||
// [optional] scale if needed
|
||||
.graphicsLayer {
|
||||
scaleX = originalScale
|
||||
scaleY = originalScale
|
||||
}
|
||||
// [optional] drop shadow effect
|
||||
.backdropShadow(CircleShape)
|
||||
// draw the backdrop with glass effect
|
||||
.drawBackdrop(backdrop) {
|
||||
// set the shape
|
||||
shape = CircleShape
|
||||
|
||||
// apply visual effects
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
saturate()
|
||||
blur(2f.dp)
|
||||
// here is the glass effect with dispersion
|
||||
refractionWithDispersion(height = 8f.dp.toPx(), amount = size.minDimension)
|
||||
}
|
||||
|
||||
// [optional] draw custom content on the backdrop layer
|
||||
onDrawBackdrop { drawBackdrop ->
|
||||
// apply the inverse scale to the backdrop content
|
||||
scale(1f / originalScale, 1f / originalScale, Offset.Zero) {
|
||||
drawBackdrop()
|
||||
}
|
||||
}
|
||||
|
||||
// [optional] draw a scrim with a brilliant highlight
|
||||
onDrawSurfaceWithHighlight { drawRect(Color.White.copy(alpha = 0.3f)) }
|
||||
}
|
||||
.clickable {}
|
||||
.size(48.dp),
|
||||
contentAlignment = Alignment.Center
|
||||
) {}
|
||||
}
|
||||
```
|
||||
|
||||
### Pre 1.0.0-alpha09 (exclusive)
|
||||
|
||||
```kotlin
|
||||
val providerState = rememberLiquidGlassProviderState(
|
||||
// if the providing content has any transparent area and there is a background behind the content, set the
|
||||
@@ -92,36 +189,6 @@ Box(
|
||||
)
|
||||
```
|
||||
|
||||
### Tips
|
||||
|
||||
- You should not nest `LiquidGlassProvider` and `LiquidGlass`:
|
||||
|
||||
```kotlin
|
||||
LiquidGlassProvider(providerState) {
|
||||
LiquidGlass(providerState) {}
|
||||
}
|
||||
```
|
||||
|
||||
Instead, use a parent layout:
|
||||
|
||||
```kotlin
|
||||
Box {
|
||||
LiquidGlassProvider(providerState) {}
|
||||
LiquidGlass(providerState) {}
|
||||
}
|
||||
```
|
||||
|
||||
- Apply the scale correctly:
|
||||
|
||||
```kotlin
|
||||
Modifier
|
||||
.scale(scaleX, scaleY)
|
||||
.liquidGlass(
|
||||
// `pivot = Offset.Zero` is required
|
||||
transformBlock = { scale(1f / scaleX, 1f / scaleY, Offset.Zero) }
|
||||
)
|
||||
```
|
||||
|
||||
## Comparing with iOS
|
||||
|
||||
iOS device: iPhone 16 Pro Max (emulator), using [GlassExplorer](https://github.com/ktiays/GlassExplorer)
|
||||
|
||||
Reference in new issue
Block a user