Saturday, 22 June 2013

Frame Animation

1. MainXml File

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


  <ImageView
      android:id="@+id/Image"
       android:layout_gravity="center_horizontal"
      android:layout_width="wrap_content"
      android:background="@drawable/ar1"
      android:layout_height="wrap_content"/>
  <Button
      android:id="@+id/startFAButtonId"
      android:layout_width="wrap_content"
      android:layout_gravity="center_horizontal"
      android:layout_height="wrap_content"
      android:text="Start Animation"
      />
</LinearLayout>

2. Java File

package com.example.frame;

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 MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b = (Button)this.findViewById(R.id.startFAButtonId);
        b.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                animate();
            }
        });

    }
       private void animate() {
          ImageView imgView = (ImageView)findViewById(R.id.Image);
          imgView.setVisibility(ImageView.VISIBLE);
          imgView.setBackgroundResource(R.drawable.animation_frame);

          AnimationDrawable frameAnimation =  (AnimationDrawable) imgView.getBackground();

          if (frameAnimation.isRunning()) {
             frameAnimation.stop();
          }
          else {
             frameAnimation.stop();
             frameAnimation.start();
          }
       }
 }


3. Place animation_frame.xml file in drawable folder

<?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/ar1" android:duration="1000" />
   <item android:drawable="@drawable/ar2" android:duration="1000" />
   <item android:drawable="@drawable/ar3" android:duration="1000" />
   <item android:drawable="@drawable/arc" android:duration="1000" />
   <item android:drawable="@drawable/ac1" android:duration="1000" />
    <item android:drawable="@drawable/ac2" android:duration="1000" />
     <item android:drawable="@drawable/ac3" android:duration="1000" />
      <item android:drawable="@drawable/ac4" android:duration="1000" />
</animation-list>

And place necessary photos in drawable folder.

No comments:

Post a Comment