Question
Eliminating Blinking Effect in Rotational Animation of List Elements
I'm working with a list of three elements, say [A, B, C]
, and I'm attempting to create a rotational animation with them. The sequence of this animation would be A B, B C, C A, A B, and so on, repeating indefinitely. The list can also have n number of objects and I want it to rotate infinitely.
However, I'm encountering a challenge in achieving a seamless transition between the elements. There seems to be a noticeable 'blinking' effect disrupting the smooth flow of the animation. I'm seeking advice on how to eliminate this issue, or suggestions for alternative methods to accomplish the same effect.
The desired effect I'm aiming for is illustrated below. But upon observation, you might notice that the animation isn't continuous and there's a certain 'blinking' effect occurring.
@Composable
fun FollowEventCardHome() {
val data = "{...}"
val eventData = Gson().fromJson(data, EventData::class.java)
var currentEvent by remember { mutableStateOf(eventData.events?.getOrNull(0)) }
var nextEvent by remember { mutableStateOf(eventData.events?.getOrNull(1)) }
var currentIndex by remember { mutableIntStateOf(0) }
var isAnimate by remember { mutableStateOf(true) }
val configuration = LocalConfiguration.current
val cardWidth = configuration.screenWidthDp.dp - 55.dp
if (isAnimate) {
StartEndInfo(
nextEvent = nextEvent,
currentEvent = currentEvent,
isAnimate = true,
isAnimationFinished = {
isAnimate = false
if (eventData.events?.size!! > 1) {
if (currentIndex == eventData.events?.size!! - 1) {
currentIndex = 0
} else {
currentIndex++
}
}
currentEvent = eventData.events?.getOrNull(currentIndex)
nextEvent = if (currentIndex < eventData.events?.size!!) {
eventData.events?.getOrNull(0)
} else {
eventData.events?.getOrNull(currentIndex + 1)
}
}
)
}
if(!isAnimate){
StartEndInfo(
nextEvent = nextEvent,
currentEvent = currentEvent,
isAnimate = true,
isAnimationFinished = {
isAnimate = true
if (eventData.events?.size!! > 1) {
if (currentIndex == eventData.events?.size!! - 1) {
currentIndex = 0
} else {
currentIndex++
}
}
currentEvent = eventData.events?.getOrNull(currentIndex)
nextEvent = if (currentIndex < eventData.events?.size!!) {
eventData.events?.getOrNull(0)
} else {
eventData.events?.getOrNull(currentIndex + 1)
}
}
)
}
}
@Composable
fun StartEndInfo(
nextEvent: Event? = null,
currentEvent: Event? = null,
isAnimate: Boolean,
isAnimationFinished: () -> Unit
) {
Row(
modifier = Modifier
.padding(
start = 16.dp,
bottom = 16.dp
)
.fillMaxWidth()
) {
Column(modifier = Modifier.fillMaxSize()) {
StartEndWithProgress(
nextEvent = nextEvent,
currentEvent = currentEvent,
isAnimate = isAnimate,
isAnimationFinished = isAnimationFinished
)
}
}
}
@Composable
fun StartEndWithProgress(
nextEvent: Event?,
currentEvent: Event?,
isAnimate: Boolean,
isAnimationFinished: () -> Unit
) {
val scalePreviousEvent = remember { Animatable(1f) }
val scaleCurrentEvent = remember { Animatable(0f ) }
LaunchedEffect(isAnimate) {
scalePreviousEvent.animateTo(
targetValue = 0f,
animationSpec = tween(durationMillis = 4000)
)
}
LaunchedEffect(key1 = isAnimate) {
scaleCurrentEvent.animateTo(
targetValue = 1f,
animationSpec = tween(durationMillis = 4000)
)
}
if(isAnimate && scalePreviousEvent.value == 0f || !isAnimate && scalePreviousEvent.value == 1f){
isAnimationFinished()
}
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 30.dp)
.padding(top = 150.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Box {
currentEvent?.start?.locationCode.let {
Text(
text = currentEvent?.start?.locationCode ?: "",
fontSize = 20.sp,
modifier = Modifier.graphicsLayer {
scaleX = 1f
scaleY = if (isAnimate) {
scaleCurrentEvent.value
} else 1f
transformOrigin = TransformOrigin(0.5f, 1f)
}
)
Text(
text = nextEvent?.start?.locationCode ?: "",
fontSize = 20.sp,
modifier = Modifier.graphicsLayer {
scaleX = 1f
scaleY = if (isAnimate) {
scalePreviousEvent.value
} else 0f
transformOrigin = TransformOrigin(0.5f, 0f)
}
)
}
}
Box {
currentEvent?.end?.locationCode.let {
Text(
text = currentEvent?.end?.locationCode ?: "",
fontSize = 20.sp,
modifier = Modifier.graphicsLayer {
scaleX = 1f
scaleY = if (isAnimate) { scaleCurrentEvent.value } else 1f
transformOrigin = TransformOrigin(0.5f, 1f)
}
)
}
Text(
text = nextEvent?.end?.locationCode ?: "",
fontSize = 20.sp,
modifier = Modifier.graphicsLayer {
scaleX = 1f
scaleY = if (isAnimate) {
scalePreviousEvent.value
} else 0f
transformOrigin = TransformOrigin(0.5f, 0f)
}
)
}
}
}