Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView animation - fade in, wait, fade out

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

like image 989
Guy Cothal Avatar asked Jul 12 '12 01:07

Guy Cothal


People also ask

How is use fade and 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.

How to fade TextView in android studio?

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.

What is Fade In Fade Out in Android?

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.


2 Answers

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.

like image 59
Guy Cothal Avatar answered Oct 12 '22 22:10

Guy Cothal


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     }) } 
like image 36
Bassam Helal Avatar answered Oct 12 '22 22:10

Bassam Helal