Android Programming Tutorials

(Romina) #1
Now Your Friends Seem Animated

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="500"/>

These set up 500-millisecond alpha animations that fade out and fade in,


respectively.


Next, we need to declare Animation objects as data members in Patchy, to


hold these once we load them:


private Animation fadeOut=null;
private Animation fadeIn=null;

Then, we can use AnimationUtils to load our animation XML resources into


the Animation objects. Add the following statements sometime late in the


onCreate() method in Patchy:


fadeOut=AnimationUtils.loadAnimation(this, R.anim.fade_out);
fadeOut.setAnimationListener(fadeOutListener);
fadeIn=AnimationUtils.loadAnimation(this, R.anim.fade_in);

You will note that in addition to loading the animation resources, we


attached an AnimationListener to the fadeOut Animation. That way, we get


notified when the animation is done, so we can make the widget fully


"gone". Add the following implementation of fadeOutListener to Patchy:


Animation.AnimationListener fadeOutListener=new Animation.AnimationListener() {
public void onAnimationEnd(Animation animation) {
statusRow.setVisibility(View.GONE);
}
public void onAnimationRepeat(Animation animation) {
// not needed
}
public void onAnimationStart(Animation animation) {
// not needed
}
};

Now, all that remains is to use fadeOut and fadeIn in our


toggleStatusEntry() method:


269
Free download pdf