Skip to content

Instantly share code, notes, and snippets.

@cyril-tl
Last active May 14, 2023 19:07
Show Gist options
  • Save cyril-tl/0320b754a31b733897877c4fd6f0bd0e to your computer and use it in GitHub Desktop.
Save cyril-tl/0320b754a31b733897877c4fd6f0bd0e to your computer and use it in GitHub Desktop.
package com.example.myapp.animation
import androidx.compose.animation.core.*
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Divider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.example.myapp.R
@Composable
fun AnimatedLine(
boxSize: Dp = 100.dp,
animationDuration: Int = 2000
) {
val infiniteTransition = rememberInfiniteTransition()
val heightAnimation by infiniteTransition.animateValue(
initialValue = 0.dp,
targetValue = boxSize,
typeConverter = Dp.VectorConverter,
animationSpec = infiniteRepeatable(
animation = keyframes {
durationMillis = animationDuration
0.dp at 0 // ms
// 80.dp at 300 with FastOutLinearInEasing
boxSize at animationDuration / 2
0.dp at animationDuration with FastOutSlowInEasing
}
// Use the default RepeatMode.Restart to start from 0.dp after each iteration
)
)
Box(
modifier = Modifier
.size(boxSize),
// .border(1.dp, Color(0xFF000000), RoundedCornerShape(2.dp)),
) {
// color = MaterialTheme.colorScheme.background // Set background color
Image(
painter = painterResource(id = R.drawable.datamatrix_jojo),
contentDescription = "start scanning",
modifier = Modifier.size(boxSize),
)
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Spacer(modifier = Modifier.height(heightAnimation))
Divider(
thickness = 4.dp, color = Color.Red,
modifier = Modifier.width(boxSize - 6.dp)
)
}
}
}
@Preview
@Composable
fun AnimLinePreview() {
MaterialTheme() {
AnimatedLine()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment