June 30, 2014

Working with Animation

There are two ways to work with animation. Below chart shows various ways to animate
The two types of animation are,

  1. View Animation
  2. Property Animation
Let's look each of this type of animation in detail.

1. View Animation:

View animation is simple and Android provides three support classes. They are, 
  • TransitionDrawable
  • AnimationDrawable
  • Animation
    TransitionDrawable
This drawable is used to transit between two drawables on a single view. Create a TransitionDrawable class and start the animation as shown below.
R.drawable.shape_transition defines the transition between two drawables and this xml file looks like the following. Here you can see that the transition is between two shapes defined in an xml which can be easily replaced by two images.
    AnimationDrawable
This class is used to provide a slideshow kind of animation where you provide a set of drawables which is drawn to the screen in an imageView in a given sequence with the given time. The code below shows how the class must be created and used. Involes three steps.
First is to find the imageView in which the transition should be shown.
Second the tranisition is set as background to the imageView and
Thirdly, ref to the animation drawable is acquired and animation can be started/stopped
This is also a good time to discuss when to use onWindowFocusChanged() and onResume(). As a general rule, however, a resumed activity will have window focus... unless it has displayed other dialogs or popups that take input focus, in which case the activity itself will not have focus when the other windows have it. Likewise, the system may display system-level windows (such as the status bar notification panel or a system alert) which will temporarily take window input focus without pausing the foreground activity. Thus use onWindowFocusChanged() when you want an action to be performed even in scenarios where onResume() may not be called. Shown below is the code to start the animation in onWindowFocusChanged() method.

    Animation
This class helps us to animate a view by changing properties of the view. Multiple transformations can be applied conveniently using this method.
First define your animation file and store it in res/anim folder.
Now load the animation in the xml file into the current activity using the following calls
Finally start the animation using the startAnimation() callback as shown above

2. Property Animation:

Sometimes you want animate other objects other than view, for this purpose the Android provides the Property Animation framework. Helps to change generic properties of objects over time.

No comments:

Post a Comment