Android Development (41 Blogs)

Frame Animation in Android

Last updated on Feb 09,2021 22.1K Views


It’s been said that Animation is the state of being alive. Something that gives you an illusion. In mobile application development too this has got its own significance. In this blog we will go through, how to use Frame Animation in android and the art of illusion.

In Android Frame Animation, you will be swapping frames repeatedly, so that it appears continuous to the human eye and we feel that it is animated. Frame is referred to an image. So to implement frame by frame animation in android, one needs to have set of images, which describes a motion.

Android logo-EdurekaNow let’s move on and see how to implement this using frame animation

Step 1- Create a drawable folder

Within it create an animation_list.xml file.
It includes :
A list of items that has the addresses of the frame 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/blank" android:duration="210" />
<item android:drawable="@drawable/logo" android:duration="210" />
<item android:drawable="@drawable/logo1" android:duration="210" />
<item android:drawable="@drawable/logo2" android:duration="210" />
<item android:drawable="@drawable/logo3" android:duration="210" />
<item android:drawable="@drawable/logo4" android:duration="210" />
<item android:drawable="@drawable/logo5" android:duration="210" />
<item android:drawable="@drawable/logo6" android:duration="210" />
<item android:drawable="@drawable/logofinal" android:duration="210" />
</animation-list>

Step 2: Create an activity_main.xml file
It Includes : An Image View


{/code]

<span style="font-size: 13px; text-align: center;">Here we are done with the xml part, see the image below for reference :-</span>
<p style="text-align: center;"><a href="https://www.edureka.co/blog/frame-animation-in-android/" target="_blank"><img class="aligncenter size-full wp-image-2382" title="XML part in Frame animation" alt="XML part in Frame animation" src="https://d1jnx9ba8s6j9r.cloudfront.net/blog/wp-content/uploads/2013/02/Project.jpg" width="845" height="603" /></a></p>
<strong>&nbsp;</strong>
<h2><span style="font-size: large;"><strong>Step 3- Outside the onCreate method :</strong></span></h2>
<strong></strong>Declare the Image View and Animation Drawable

[code lang="java"]
// Declaring an Image View and an Animation Drawable
ImageView view;
AnimationDrawable frameAnimation;
 

Step 3- Inside the OnCreate method:

  • Typecast the Image view
  • Typecast the Animation Drawable
  • Set the drawable background on the image view
// Typecasting the Image View
view = (ImageView) findViewById(R.id.imageAnimation);

// Setting animation_list.xml as the background of the image view
view.setBackgroundResource(R.drawable.animation_list);

// Typecasting the Animation Drawable
frameAnimation = (AnimationDrawable) view.getBackground();

Step 4- After the onCreate method :

The animation should only run when it is in focus that is when it is visible to the user. Hence define this method after the onCreate method.

// Called when Activity becomes visible or invisible to the user
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
      if (hasFocus) {
	// Starting the animation when in Focus
	frameAnimation.start();
	} else {
        // Stoping the animation when not in Focus
	frameAnimation.stop();
      }
}

So the final result will look like this :-
Frame animation in androidFrame animation in androidFrame animation in android

[dl url=”#” class=’eModal eModal-20′ title=”Download Code” desc=”” type=”” align=”” for=”download”]

Stay tuned for more tutorials to learn how to create Android widgets! Happy Learning!

Got a question for us? Please mention it in the comments section and we will get back to you.

edurekaRelated Posts:

Start your training on Android Development

Comments
4 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Frame Animation in Android

edureka.co