Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using fade in animation for a view

I want to have a View that initially is invisible and when I press a button, it becomes visible with a fade in animation. I'm using the AlphaAnimation for the fading effect. The problem is that if I make the view invisible the animation can't be seen.

like image 354
Gratzi Avatar asked Feb 23 '11 11:02

Gratzi


People also ask

Which animation property can be used to help create a fade effect?

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.

What is fade in animation?

The Fade In/Fade Out behavior lets you dissolve into and out of any object by ramping the opacity of the object from 0 percent to 100 percent at the start, and then back to 0 percent at the end.


3 Answers

Suppose you have an ImageView named imageView and an animation file your_fade_in_anim.xml inside your res\anim\ folder:

ImageView imageView = (ImageView) findViewById(R.id.imageView);
Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.your_fade_in_anim);
// Now Set your animation
imageView.startAnimation(fadeInAnimation);

Your XML

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha 
        android:fromAlpha="0.0"
        android:toAlpha="1.0" 
        android:interpolator="@android:anim/accelerate_interpolator" 
        android:duration="[duration (in milliseconds)]"
        android:repeatCount="infinite" />
</set>

Replace the brackets with your actual duration.

like image 110
Tanmay Mandal Avatar answered Sep 30 '22 20:09

Tanmay Mandal


Provide an AnimationListener to the Animation and make the View visible as soon as the Animation starts.

http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html

like image 34
Timo Ohr Avatar answered Sep 30 '22 20:09

Timo Ohr


Instead of the infinite repeat count and hiding/viewing your View, I suggest to just not repeat the animation and initially start with the alpha channel set to maximum. Then you can use:

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha 
        android:fromAlpha="0.0"
        android:toAlpha="1.0" 
        android:interpolator="@android:anim/accelerate_interpolator" 
        android:duration="[duration (in milliseconds)]"
        android:repeatCount="0" />
</set>

And you're done. No need for a Listener, hiding or showing. Just as simple.

like image 3
OrangeCMS Avatar answered Sep 29 '22 20:09

OrangeCMS