Wednesday, September 28, 2011

Android pre-defined Animation Resources

In the last exercise "Implement simple Fade-in Animation" and "Handle Animation event, AnimationListener", we create our own animation (fadein.xml and fadeout.xml). Actually, Android come with some pre-defined Animation Resources include android.R.anim.fade_in and android.R.anim.fade_in. Such that we can omit the creation of /res/anim/fadein.xml and /res/anim/fadeout.xml.

This exercise have the almost same effect as in the last exercise "Handle Animation event, AnimationListener".

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
</LinearLayout>


package com.exercise.AndroidAnimation;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class AndroidAnimationActivity extends Activity {

ImageView image;
Animation animationFadeIn, animationFadeOut;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView)findViewById(R.id.image);

animationFadeIn = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
animationFadeOut = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
animationFadeIn.setAnimationListener(animationInListener);
animationFadeOut.setAnimationListener(animationOutListener);
image.startAnimation(animationFadeIn);
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
image.clearAnimation();
}

AnimationListener animationInListener
= new AnimationListener(){

@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
image.startAnimation(animationFadeOut);
}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}};

AnimationListener animationOutListener
= new AnimationListener(){

@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
image.startAnimation(animationFadeIn);
}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}};
}


Download the files.

next:
- Implement slide-in and slide-out animation



No comments:

Post a Comment