Category: Animation

Android Animation 1

Android basically provides two types of Animations:

1. Frame – by -Frame : This shows a sequence of picture in order. It enables developers to display the image to display, and then show like the slideshow. This type of animation first needs an animation-list element in the layout file containing a list of item elements specifying an ordered list of the different pictures to display. The one-shot attribute specifies whether the animation is played only once or repeatedly.

e.g. ../res/anim/animated.xml  – Animation list in XML file

<?xml version=”1.0″ encoding=”utf-8″?>
<animation-list xmlns:android=”http://schemas.android.com/apk/res/android&#8221; android:oneshot=”false”>
<item android:drawable=”@drawable/anddev1″ android:duration=”200″ />
<item android:drawable=”@drawable/anddev2″ android:duration=”200″ />
<item android:drawable=”@drawable/anddev3″ android:duration=”200″ />
</animation-list>

To display the frame-by-frame animation, set the animation to a view`s background:

ImageView im = (ImageView) this.findViewById(R.id.myanimated);
im.setBackgroundResource(R.anim.animated);
AnimationDrawable ad = (AnimationDrawable)im.getBackground();
ad.start();

After the view background is set, a drawable can be retrieved by calling getBackground() and casting it to AnimationDrawable.Then, calling the start() method starts the animation.

2. Tween Animation – These type of animation uses a different approach that creates an animation by performing a series of transformations on a single image. In Android, it provides access to the following classes that are the basis for all the animations:

  1. AlphaAnimation—Controls transparency changes
  2. RotateAnimation—Controls rotations
  3. ScaleAnimation—Controls growing or shrinking
  4. TranslateAnimation—Controls position changes

These four Animation classes can be used for transitions between activities, layouts, views and so on. All these can be defined in the layout XML file as <alpha>, <rotate>, <scale>, and <translate>. They all have to be contained within an AnimationSet <set>:

1. <alpha> attributes:

android:fromAlpha, android:toAlpha

The alpha value translates the opacity from 0.0 (transparent) to 1.0 (opaque).

2. <rotate> attributes:
android:fromDegrees,  android:toDegrees,  android:pivotX,  android:pivotY
The rotate specifies the angle to rotate an animation around a center of rotation defined as the pivot.

3. <scale> attributes:
android:fromXScale,  android:toXScale, android:fromYScale, android:toYScale, android:pivotX, android:pivotY
The scale specifies how to change the size of a view in the x-axis or y-axis. The pivot location that stays fixed under the scaling can also be specified.

4. <translate> attributes:
android:fromXDelta,  android:toXDelta, android:fromYDelta, android:toYDelta
The translate specifies the amount of translation to perform on a View.