Tuesday, September 27, 2011

Handle Animation event, AnimationListener.



Create animation XML files.
/res/anim/fadein.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000">
</alpha>
</set>


/res/anim/fadeout.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="1000">
</alpha>
</set>


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, R.anim.fadein);
animationFadeOut = AnimationUtils.loadAnimation(this, R.anim.fadeout);
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:
- Android pre-defined Animation Resources

No comments:

Post a Comment