-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from SuhasDissa/main
Added a shimmer effect for the wallpaper grid
- Loading branch information
Showing
3 changed files
with
101 additions
and
15 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
app/src/main/java/com/bnyro/wallpaper/ui/components/ShimmerGrid.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.bnyro.wallpaper.ui.components | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.grid.GridCells | ||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.unit.dp | ||
import com.bnyro.wallpaper.util.shimmer | ||
|
||
@Composable | ||
fun ShimmerGrid() { | ||
LazyVerticalGrid( | ||
columns = GridCells.Fixed(2) | ||
) { | ||
items(10) { | ||
Box( | ||
modifier = Modifier | ||
.height(300.dp) | ||
.padding(5.dp) | ||
.clip(RoundedCornerShape(10.dp)) | ||
.shimmer(600f) | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
app/src/main/java/com/bnyro/wallpaper/util/ShimmerEffect.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.bnyro.wallpaper.util | ||
|
||
import androidx.compose.animation.core.RepeatMode | ||
import androidx.compose.animation.core.animateFloat | ||
import androidx.compose.animation.core.infiniteRepeatable | ||
import androidx.compose.animation.core.rememberInfiniteTransition | ||
import androidx.compose.animation.core.tween | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.isSystemInDarkTheme | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.composed | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.graphics.Brush | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
|
||
/** | ||
* @param length Diagonal length of the element | ||
*/ | ||
fun Modifier.shimmer(length: Float): Modifier = composed { | ||
val transition = rememberInfiniteTransition(label = "shimmer transition") | ||
val offset by transition.animateFloat( | ||
initialValue = -100f, | ||
targetValue = length + 100f, | ||
animationSpec = infiniteRepeatable( | ||
animation = tween(1500), | ||
repeatMode = RepeatMode.Restart | ||
), | ||
label = "gradient offset" | ||
) | ||
val color = if (isSystemInDarkTheme()) Color.DarkGray else Color.LightGray | ||
val startOffset = -length / 4 + offset | ||
val endOffset = length / 4 + offset | ||
val brush = Brush.linearGradient( | ||
colors = listOf( | ||
color.copy(alpha = 0.2f), | ||
color.copy(alpha = 0.6f), | ||
color.copy(alpha = 0.2f) | ||
), | ||
start = Offset(x = startOffset, y = startOffset), | ||
end = Offset(x = endOffset, y = endOffset) | ||
) | ||
this.then(background(brush)) | ||
} | ||
|
||
@Preview | ||
@Composable | ||
private fun ShimmerPreview() { | ||
Box( | ||
modifier = Modifier | ||
.size(300.dp) | ||
.shimmer(400f) | ||
) | ||
} |