Skip to content

Instantly share code, notes, and snippets.

@LeonidIvankin
Created July 9, 2022 06:21
Show Gist options
  • Save LeonidIvankin/45ccb35b9ae80fbddcd2ffc2c0678c44 to your computer and use it in GitHub Desktop.
Save LeonidIvankin/45ccb35b9ae80fbddcd2ffc2c0678c44 to your computer and use it in GitHub Desktop.
package com.leonidivankin.draftandroid.articles.shimmering
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.core.*
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
class ShimmeringActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ShimmeringCompose(
modifier = Modifier
.width(300.dp)
.height(150.dp),
cornerRadius = 16.dp
)
}
}
}
@Composable
fun ShimmeringCompose(
modifier: Modifier,
cornerRadius: Dp
) {
val screenWidth = LocalConfiguration.current.screenWidthDp.dp
val translateAnim by rememberInfiniteTransition().animateFloat(
initialValue = 0f,
targetValue = screenWidth.px,
animationSpec = infiniteRepeatable(
animation = tween(durationMillis = 1500, easing = FastOutSlowInEasing)
)
)
val shimmerColorShades = listOf(Color(0xFFF37F19), Color(0xFF007CFF), Color(0xFFF37F19))
val brush = Brush.linearGradient(
colors = shimmerColorShades,
start = Offset(translateAnim, translateAnim),
end = Offset(translateAnim + 70.dp.px, translateAnim + 35.dp.px)
)
Box(modifier = modifier.background(brush = brush, shape = RoundedCornerShape(cornerRadius)))
}
val Dp.px: Float
@Composable
get() = with(LocalDensity.current) { this@px.toPx() }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment