Create an application that will have spinner with list of animation names. On selecting animation name, that animation should affect on the images displayed below.

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"
>
<Spinner android:entries="@array/animation"
android:layout_width="match_parent" android:id="@+id/spinner1"
android:layout_height="wrap_content"></Spinner>
<ImageView android:src="@drawable/icon"
android:layout_width="wrap_content" android:id="@+id/imageView1"
android:layout_height="wrap_content"></ImageView>
</LinearLayout>
Anim>>alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0"
android:toAlpha="1.0"
android:duration="5000"
/>
</set>


anim>>rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate android:fromDegrees="0"
android:toDegrees="-360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000"/>
</set>


anim>>scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:pivotX="50%"
android:pivotY="50%"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="2.0"
android:toYScale="2.0"
android:duration="5000"
/>
</set>


anim>>spin.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate android:fromDegrees="0"
android:toDegrees="-360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000"
android:repeatCount="infinite"/>
</set>


anim>>translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0"
android:toXDelta="100"
android:fromYDelta="0"
android:toYDelta="100"
android:duration="5000" />
</set>


strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Tut_2_5Activity!</string>
<string name="app_name">Tut_2_5</string>
<string-array name="animation">
<item>Select Animation</item>
<item>Alpha</item>
<item>Spin</item>
<item>Rotate</item>
<item>Scale</item>
<item>Translate</item>
</string-array>
</resources>

Tut_2_5Activity.java
package com.tut2_5;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.Spinner;
public class Tut_2_5Activity extends Activity implements
OnItemSelectedListener {
/** Called when the activity is first created. */
Spinner sp;
ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sp=(Spinner)findViewById(R.id.spinner1);
sp.setOnItemSelectedListener(this);
img=(ImageView)findViewById(R.id.imageView1);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2,
long arg3) {
if(arg3==1)
{
Animation
anim=AnimationUtils.loadAnimation(this,R.anim.alpha);
img.startAnimation(anim);
}
if(arg3==2)
{
Animation
anim=AnimationUtils.loadAnimation(this,R.anim.spin);
img.startAnimation(anim);
}
if(arg3==3)
{
Animation
anim=AnimationUtils.loadAnimation(this,R.anim.rotate);
img.startAnimation(anim);
}
if(arg3==4)
{
Animation
anim=AnimationUtils.loadAnimation(this,R.anim.scale);
img.startAnimation(anim);
}
if(arg3==5)
{
Animation
anim=AnimationUtils.loadAnimation(this,R.anim.translate);
img.startAnimation(anim);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}


Output



No comments:

Post a Comment