Animation and Its Subclasses

Animation extends from Object and some known direct subclasses are as follows which have been described shortly.
  • AlphaAnimation: An animation that controls the alpha level of an object. Useful for fading things in and out.
  • AnimationSet: Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform.
  • RotateAnimation: An animation that controls the rotation of an object. This rotation takes place in the X-Y plane. You can specify the point to use for the center of the rotation, where (0,0) is the top left point. If not specified, (0,0) is the default rotation point.
  • ScaleAnimation: An animation that controls the scale of an object. You can specify the point to use for the center of scaling.
  • TranslateAnimation: An animation that controls the position of an object.

One big disadvantage of Animation and its sub-classes is that you can animate only basic properties like rotation, scale, alpha and position (e.g., not a background color) and these tools are restricted to View's sub-classes only. One more issue you face if you use Animation is that it animates a View's pixel only, not a View itself, e.g., you apply TransitionAnimation to your object, but it stays clickable in the previous location, if not to specify different behavior.
Usage:
We should create an instance of Animation class and override applyTransformation() function of it appropriately.

anim2 = new Animation(){

       @Override
      protected void applyTransformation(float interpolatedTime, Transformation transformation){

          //update property value
          //set the new value to targetView
          //invalidate the view
      }
    };

Then we can set some parameters of the Animation instance and finally run the animation by syntax

targetView.startAnimation(anim2)

The following examples devote to discuss Animation class itself and providing some examples of it. The first example shows how we can create one instance of Animation and use it to make a simple animation on a EditText.

Example 1: Follow the below steps:
Step 1- Create a new Android Studio project and select an Empty Activity template.
Step 2- In activity_main.xml file add an EditText and a Button like below.


Step 3- Edit the MainActivity.java file as below.


Here a new instance of Animation has been instantiated and the function applyTransformation() has been override to produce suitable functionality for making the EditText animating.
Now just run the app on an emulator a real device.


Example 2: Here we animate a circle inside the PieView created in the topic Custom View in Android. We want to draw a circle with an animation that draws the circle by angle (refer to end of the example to see the animation).

Step 1- Create a new Android Studio project and select an Empty Activity template.
Step 2- Follow the steps in Custom View in Android to provide a new custom view called PieView like below. Consider that here we added a circle with violet color to convert the circle to a ring, just for having more pretty view. Also suitable getter and setter methods for angle variable have been involved. The PieView.java file is like this:


Step 3- Now we create a new java class called CircleAnimation which extends Animation class and overrides applyTransformation() function like below:


Step 4- In the current step, we can instantiate an instance of CircleAnimation in MainActivity.java and start the animation.


Now all things are ready to run the animation.

Comments