I have been struggling on this for a few days. I implemented a blink animation code for TextView. However, I think I need to fix little bit of fromAlpha
toAlpha
. It seems gradually fade in and fade out if you just put fromAplha = 0.0f
and toAplha = 1.0f
. How can I make it without fade like flicker?
Please help me.
private fun View.blink(
times: Int = Animation.INFINITE,
duration: Long = 800L,
offset: Long = 20L,
minAlpha: Float = 0.0f,
maxAlpha: Float = 1.0f,
repeatMode: Int = Animation.ABSOLUTE,
) {
startAnimation(
AlphaAnimation(minAlpha, maxAlpha).also {
it.duration = duration
it.startOffset = offset
it.repeatMode = repeatMode
it.repeatCount = times
},
)
}
You can create a custom interpolator for the animation that uses a step function.
object StepInterpolator: BaseInterpolator() {
override fun getInterpolation(input: Float) =
round(input)
}
private fun View.blink(
times: Int = Animation.INFINITE,
duration: Long = 800L,
offset: Long = 20L,
minAlpha: Float = 0.0f,
maxAlpha: Float = 1.0f,
repeatMode: Int = Animation.ABSOLUTE,
) {
startAnimation(
AlphaAnimation(minAlpha, maxAlpha).apply {
duration = duration
startOffset = offset
repeatMode = repeatMode
repeatCount = times
interpolator = StepInterpolator
},
)
}