Sunday, September 18, 2016

Frame Animation in Android @ Make a Movie Clip

Hi .... Dear All ....  Today I am uploading a superb example of Frame Animation.
In this example i have added multiple images which shows in a Frame by using  Animation.
         Frame animations build up animation from a series of still images that are shown in rapid succession, like a film reel.
To Add frame Animation I have five gif images in my drawable folder.
Also create a xml file named  frame_animation.xml as
<?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/p1" android:duration="1000" />
    <item android:drawable="@drawable/p2" android:duration="1000" />
    <item android:drawable="@drawable/p3" android:duration="1000" />
    <item android:drawable="@drawable/p4" android:duration="1000" />
    <item android:drawable="@drawable/p5" android:duration="1000" />
</animation-list>
A. The output will be like 
a. 




B. The main layout file is as 
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context="com.exam.ravi.frameanimation.MainActivity">
    <Button        android:id="@+id/startButton"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/start"        />
    <ImageView        android:contentDescription="@string/desc"

        android:id="@+id/animateImage"

        android:layout_width="300dp"

        android:layout_height="300dp"

        android:layout_gravity="center_horizontal"/>

</LinearLayout>
C. Java Code is as 
package com.exam.ravi.frameanimation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    Button button;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.startButton);
        button.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {
                     ImageView imgView = (ImageView)findViewById(R.id.animateImage);
                        imgView.setVisibility(ImageView.VISIBLE);
                        imgView.setBackgroundResource(R.drawable.frame_animation);

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

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

No comments:

Post a Comment