Sunday, 15 September 2013

Frame Animation demo program

 //Main Activity Program

package com.demo;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class FrameAnimationDemo extends Activity
{
    AnimationDrawable animation;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        ImageView image=(ImageView) findViewById(R.id.image);
        image.setBackgroundResource(R.drawable.frameanim);
        animation = (AnimationDrawable) image.getBackground();
     
        final Button bstart=(Button) findViewById(R.id.btnstart);
        bstart.setOnClickListener(new View.OnClickListener()
        {     
            public void onClick(View v)
            {
                animation.start();
            }
        });
     
        final Button bstop=(Button) findViewById(R.id.btnstop);
        bstop.setOnClickListener(new View.OnClickListener()
        {     
            public void onClick(View v)
            {
                animation.stop();
            }
        });      
    }
}

// main.xml coding
 <?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"
    />
<Button android:text="Start" android:id="@+id/btnstart" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Stop" android:id="@+id/btnstop" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<ImageView android:layout_height="wrap_content" android:id="@+id/image" android:layout_width="wrap_content"></ImageView>
</LinearLayout>


// frameanil.xml coding (this must be in drawable folder along with animation images)






<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
    <item android:drawable="@drawable/a" android:duration="100"/>
    <item android:drawable="@drawable/b" android:duration="100"/>
    <item android:drawable="@drawable/c" android:duration="100"/>
    <item android:drawable="@drawable/d" android:duration="100"/>
    <item android:drawable="@drawable/e" android:duration="100"/>
    <item android:drawable="@drawable/f" android:duration="100"/>
    <item android:drawable="@drawable/g" android:duration="100"/>
    <item android:drawable="@drawable/h" android:duration="100"/>
    <item android:drawable="@drawable/i" android:duration="100"/>
    <item android:drawable="@drawable/j" android:duration="100"/>
</animation-list>
    

//a,b,c,d,e,f,g,h,i,j are the images stored in drawable folder
//Manifest File Coding
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.demo"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".FrameAnimationDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

No comments:

Post a Comment