Tuesday 7 February 2012

Handle AnimationListener

With AnimationListener, we can monitor events of Animation, onAnimationEnd, onAnimationRepeat and onAnimationStart. It's a example modified from last exercise "Apply animation on Button". When any buttons clicked, animation will start on the image, and hide the buttons. When onAnimationEnd, the image disappear, and resume the buttons.

Handle AnimationListener




Modify from last exercise "Apply animation on Button", Keep anim_alpha.xml, anim_rotate.xml, anim_scale.xml and anim_translate.xml no change.

Modify main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/layer_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layer_buttons"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/translate"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Translate" />
<Button
android:id="@+id/alpha"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Alpha" />
<Button
android:id="@+id/scale"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Scale" />
<Button
android:id="@+id/rotate"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Rotate" />
</LinearLayout>

</FrameLayout>
</LinearLayout>


Main activity
package com.exercise.AndroidAnimButtons;

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

public class AndroidAnimButtonsActivity extends Activity {

LinearLayout layerImage;
LinearLayout layerButtons;

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

final Animation animTranslate = AnimationUtils.loadAnimation(this, R.anim.anim_translate);
final Animation animAlpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
final Animation animScale = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);

animTranslate.setAnimationListener(animationListener);
animAlpha.setAnimationListener(animationListener);
animScale.setAnimationListener(animationListener);
animRotate.setAnimationListener(animationListener);

Button btnTranslate = (Button)findViewById(R.id.translate);
Button btnAlpha = (Button)findViewById(R.id.alpha);
Button btnScale = (Button)findViewById(R.id.scale);
Button btnRotate = (Button)findViewById(R.id.rotate);
final ImageView image = (ImageView)findViewById(R.id.image);

layerImage = (LinearLayout)findViewById(R.id.layer_image);
layerButtons = (LinearLayout)findViewById(R.id.layer_buttons);
layerImage.setVisibility(View.INVISIBLE);
layerButtons.setVisibility(View.VISIBLE);

btnTranslate.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
layerImage.setVisibility(View.VISIBLE);
layerButtons.setVisibility(View.INVISIBLE);
image.startAnimation(animTranslate);
}});

btnAlpha.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
layerImage.setVisibility(View.VISIBLE);
layerButtons.setVisibility(View.INVISIBLE);
image.startAnimation(animAlpha);
}});

btnScale.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
layerImage.setVisibility(View.VISIBLE);
layerButtons.setVisibility(View.INVISIBLE);
image.startAnimation(animScale);
}});

btnRotate.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
layerImage.setVisibility(View.VISIBLE);
layerButtons.setVisibility(View.INVISIBLE);
image.startAnimation(animRotate);
}});
}

AnimationListener animationListener
= new AnimationListener(){

@Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
layerImage.setVisibility(View.INVISIBLE);
layerButtons.setVisibility(View.VISIBLE);
}

@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.

No comments:

Post a Comment