Android TutorialsNo Comments

default thumbnail
473 views.

Android supports three types of animation:

  • Frame-by-frame animation
    A series of frames is drawn one after the other at regular intervals by loading a series of Drawable resources one after the other.
  • Layout animation
    Animate views inside a container view such as lists and tables.
  • View animation
    Animate any general-purpose view.

The types #2 and #3 are a type of tweeting animation which involves the drawing in between the key drawings. The idea is that knowing the beginning state and ending state of a drawing allows us to vary certain aspect of the drawing in time. This varying aspect could be color, position, size, rotation, and so on. In other words, we tell Android to perform a series of simple transformations to the content of a View.

Both animation types can be used in any View object to provide simple rotating timers, activity icons, and other useful UI elements. Tweened animation is handled by android.view.animation. Frame-by-frame animation is handled by the AnimationDrawable class.

  1. Frame –by- frme animation :

ss

In Android, we can get frame-by-frame animation through a class in the graphics package called AnimationDrawable. We can tell from its name that it is like any other drawable that can work as background for any view. For example the background bitmaps are represented as Drawables.

The class AnimationDrawable, in addition to being a Drawable, can take a list of other Drawable resources like images and render them at specified intervals. This class is really a thin wrapper around the animation support provided by the basic Drawable class.

To make use of the AnimationDrawable class, let’s start with a set of Drawable resources in the /res/drawable subdirectory.

Example :

main.xml

frame_animation.xml

ss1

FrameAnimation.java

  1. Layout Animation
  • We’ll use layout animation with the ListView and GridView, which are the two most commonly-used controls in Android. Specifically, we’ll use layout animation to add visual effects to the way each item in a ListView or GridView is displayed. Actually, we can use this type of animation on all controls derived from a ViewGroup.
  • Layout animation works by applying tweening to each view that is part of the layout being animated. Here are four types of tweening animation:
  • Scale animation (growing or shrinking) : We use this type of animation to make a view smaller or larger either on x axis or on the y axis. We can also specify the pivot point around which we want the animation to take place.
  • Rotate animation (rotations): We use this to rotate a view around a pivot point by a certain number of degrees.
  • Translate animation (position changes): We use this to move a view along the x or y axis.
  • Alpha animation (transparency changes): We use this to change the transparency of a view.

Once we have a ListView, we can attach an animation to it so that each list item will go through that animation.

We can define both the individual animation and the mediator in XML files in the /res/anim subdirectory. Once we have the mediator XML file, we can use that file as an input to the ListView in its own XML layout definition.

main.xml

list_layout.xml

list_layout_controller.xml

alpha.xml

rotation.xml

scale.xml

translate_alpha.xml

Now that we have the layout needed for the activity, we can write the code for the activity to load the layout file so we can generate our UI.

Activity_Main.xml

 

Be the first to post a comment.

Add a comment