An Alpha Animation is animation that controls the alpha level of an object, i.e. fading it in and out. In Android, you can apply that fading effect to almost anything, from simple text, to images, buttons, check boxes, etc… Android has a few classes that can help you add that special effect to your programs, like AlphaAnimation andAnimationUtils.
Here’s an example on how to apply fading on any Android component subclass of View.
First, the XML resource. In the resources folder, we will create a tiny XML configuration file with the characteristics of the fading effect we want in an “anim” subfolder. So, under res/anim, here’s our alpha.xml:
We are choosing to have a very basic full fade in effect (alpha from 0 to 1) that lasts one second. The above can also be done directly in Java code:
Configuring the animation in resources or in code is ultimately a matter of preference. We will use the XML in this example. This is our class that does the above fading to any View (TextView, Button, etc..):
The runAlphaAnimation() method takes an Activity reference and a View id attribute (as set up in the View’s layout XML). We’re basically done. all we have to do now is call it from any one of our Activitites:
That’s all there is to it. The same basic technique seen here applies to all other special effects like translating, scaling or rotating components. The Animation classes have of course lots of other cool stuff, like controlling the z-ordering of the animated components, acceleration and repeat effects.