Pages

Wednesday, 18 January 2012

FRAME ANIMATION


FRAME ANIMATION
/*
 A series of frames is drawn one after the other at regular intervals.
 For this create a xml which contains ImageView for showing the animation
 */

package com.animation;

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

public class FrameAnimationActivity extends Activity {

                AnimationDrawable frameAnimation;
                AnimationDrawable frameAnimation2;
                /** Called when the activity is first created. */
                @Override
                public void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

                                ImageView imgView = (ImageView) findViewById(R.id.Image);
                                ImageView imgView2 = (ImageView) findViewById(R.id.imageView1);

                                imgView.setVisibility(ImageView.VISIBLE);
                                imgView2.setVisibility(ImageView.VISIBLE);

                                imgView.setBackgroundResource(R.drawable.ani);
                                imgView2.setBackgroundResource(R.drawable.ani);
                               
                                frameAnimation = (AnimationDrawable) imgView.getBackground();
                                frameAnimation2 = (AnimationDrawable) imgView2.getBackground();
                                imgView.post(new Starter());
                                imgView2.post(new Starter());
                }

                public class Starter implements Runnable {

                                public void run() {
                                                // TODO Auto-generated method stub
                                                frameAnimation.start();
                                                frameAnimation2.start();
                                               
                                }

                }
}


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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ImageView
        android:id="@+id/Image"
        android:layout_width="100px"
        android:layout_height="100px"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/an2" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100px"
        android:layout_height="100px"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/an1" >
    </ImageView>

</LinearLayout>

No comments:

Post a Comment