I am making a picture gallery app. I current have a imageview with a text view at the bottom. Currently it is just semitransparent. I want to make it fade in, wait 3 second, then fade out 90%. Bringing focus to it or loading a new pic will make it repeat the cycle. I have read thru a dozen pages and have tried a few things, no success. All I get is a fade in and instant fade out
The Fade In/Fade Out behavior is useful for introducing and removing animated elements. For example, you can apply the Fade In/Fade Out behavior to text that moves across the screen to make it fade into existence, and then fade away at the end of its duration.
A Fading TextView is a TextView that changes its content automatically every few seconds. If we want to design a beautiful interface than we can use Fading TextView. Add the support Library in your module's build. gradle file and add dependency in the dependencies section.
In android, Fade In and Fade Out animations are used to change the appearance and behavior of the objects over a particular interval of time. The Fade In and Fade Out animations will provide a better look and feel for our applications.
protected AlphaAnimation fadeIn = new AlphaAnimation(0.0f , 1.0f ) ; protected AlphaAnimation fadeOut = new AlphaAnimation( 1.0f , 0.0f ) ; txtView.startAnimation(fadeIn); txtView.startAnimation(fadeOut); fadeIn.setDuration(1200); fadeIn.setFillAfter(true); fadeOut.setDuration(1200); fadeOut.setFillAfter(true); fadeOut.setStartOffset(4200+fadeIn.getStartOffset());
Works perfectly for white backgrounds. Otherwise, you need to switch values when you instantiate AlphaAnimation
class. Like this:
AlphaAnimation fadeIn = new AlphaAnimation( 1.0f , 0.0f ); AlphaAnimation fadeOut = new AlphaAnimation(0.0f , 1.0f );
This works with black background and white text color.
Kotlin Extension functions for Guy Cothal's answer:
inline fun View.fadeIn(durationMillis: Long = 250) { this.startAnimation(AlphaAnimation(0F, 1F).apply { duration = durationMillis fillAfter = true }) } inline fun View.fadeOut(durationMillis: Long = 250) { this.startAnimation(AlphaAnimation(1F, 0F).apply { duration = durationMillis fillAfter = true }) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With