Monday, October 3, 2011

Animation on scale



Create /res/anim/enlarge.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<scale
android:fromXScale="0.1"
android:toXScale="3.0"
android:fromYScale="0.1"
android:toYScale="3.0"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="100" />
</set>


Create /res/anim/shrink.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<scale
android:fromXScale="3.0"
android:toXScale="0.1"
android:fromYScale="3.0"
android:toYScale="0.1"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="100" />
</set>


main.xml layout
<?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/image1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
<ImageView
android:id="@+id/image2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
<ImageView
android:id="@+id/image3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
</LinearLayout>


Main code
package com.exercise.AndroidAnimation;

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.ImageView;

public class AndroidAnimationActivity extends Activity {

ImageView image1, image2, image3;
Animation animationEnlarge, animationShrink;

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

animationEnlarge = AnimationUtils.loadAnimation(this,
R.anim.enlarge);
animationShrink = AnimationUtils.loadAnimation(this,
R.anim.shrink);
animationEnlarge.setAnimationListener(animationEnlargeListener);
animationShrink.setAnimationListener(animationShrinkListener);

image2.startAnimation(animationEnlarge);

}

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

AnimationListener animationEnlargeListener
= new AnimationListener(){

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

}

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

}

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

}};

AnimationListener animationShrinkListener
= new AnimationListener(){
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
image2.startAnimation(animationEnlarge);
}

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