Wednesday 25 September 2013

Animations in Android

How to apply animations for the objects in android??


This are few animation xml files which should be stored in res/anim folder.

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:fillAfter="true" 
android:duration="7000"
/>
</set>

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="7000"
    />
</set>

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="2500"
/>
</set>

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:duration="2000"
       android:pivotX="50%" 
       android:pivotY="50%" 
       android:repeatCount="infinite"
  />  
</set>

translate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate  
android:fromXDelta="110" 
android:toXDelta="-120" 
android:duration="4000" 
android:fillAfter="true"
  android:fromYDelta="190" 
  android:toYDelta="0"
/>
</set>


All the above xml files should be stored in res/anim folder

Next now in
MainActivity.java

package com.sri.animation;

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 MainActivity extends Activity implements OnItemSelectedListener {
Spinner sp;
ImageView i;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp = (Spinner) findViewById(R.id.spinner1);
i = (ImageView) findViewById(R.id.imageView1);
sp.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long item) {
// TODO Auto-generated method stub
if (item == 1) {
Animation a = AnimationUtils.loadAnimation(this, R.anim.alpha);
i.startAnimation(a);
}
if (item == 2) {
Animation a = AnimationUtils.loadAnimation(this, R.anim.rotate);
i.startAnimation(a);
}
if (item == 3) {
Animation a = AnimationUtils.loadAnimation(this, R.anim.scale);
i.startAnimation(a);
}
if (item == 4) {
Animation a = AnimationUtils.loadAnimation(this, R.anim.spin);
i.startAnimation(a);
}
if (item == 5) {
Animation a = AnimationUtils.loadAnimation(this, R.anim.translate);
i.startAnimation(a);
}

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
}

activity_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" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/Animation" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_marginTop="100dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/facebook" />

</LinearLayout>

values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Animation</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

    <string-array name="Animation">
        <item>Select Item</item>
        <item>Alpha</item>
        <item>Rotate</item>
        <item>Scale</item>
        <item>Spin</item>
        <item>Translate</item>
    </string-array>

</resources>

Now run the application.

No comments:

Post a Comment