Animated views

Tutorials with advanced 'difficulty' and more Lines of Code.

Animated views

Postby paller » Tue Apr 22, 2008 10:31 pm

Click here for the original blog entry.

I have to admit, I am aesthetically challenged. Creating a nice GUI is not my game and I respected those who could design such things. Because of this limitation of mine, I generally look down on glittering UIs with contempt. But even I was moved - even if only a bit - by Android's animation capabilities.

Android is able to animate any View object and each and every View has a startAnimation() method that launches animation. Animations exist as independent objects that can be applied to the Views. Animation objects can even be populated from XML resources that live in the res/anim directory.

You can download the example program from here.

Let's look at the animation file below. This file can be found as res/anim/magnify.xml in the example program bundle.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <scale xmlns:android=""
  3.    android:fromXScale="0.5"
  4.    android:toXScale="2.0"
  5.    android:fromYScale="0.5"
  6.    android:toYScale="2.0"
  7.    android:pivotX="0%"
  8.    android:pivotY="50%"
  9.    android:startOffset="0"
  10.    android:duration="400"
  11.    android:fillBefore="true" />
Parsed in 0.001 seconds, using GeSHi

This is a scaling animation. The attributes mean the followoing:

* The content of the view is actually shrinken first (fromXScale, fromYScale=0.5) to half before it starts to grow to double its standard size and 4 times of the initial size (0.5->2.0). This is done both in X and Y axis.
* The pivot point from which the object grows is 0% in the X axis. This means that the leftmost points of the object will stay in place and the object grows rightward to double size. Meanwhile, the pivot point on the Y axis is the middle of the object (50%), this means that the object will grow up and down from its center line.
* The animation starts immediately (startOffset=0) and finishes in 400 msec.

Once we have an animation object, we can apply it to any view. In the example program, I created a simple list and applied the animation to the TextView list elements whenever one list element is selected (either by moving the selection with the arrow keys or clicking at list elements). Two points are worth noting:

* Just because we animate, the list layout is not recalculated. Hence the generous top and bottom paddings around the list elements. There is enough space provided for the TextView to grow.
* In the list row layout (res/layout/row.xml), the layout_width is set to fill_parent. This is seemingly random choice but actually, the program does not work well with wrap_content as layout_width. Whenever a list element is selected, its color changes to highlight. If the width is wrap_content, then the size of the TextView (hence the higlighted screen area) is just the length of the text. When the animation starts, the text grows past this size and the end of the text becomes unreadable. This is very important therefore to allow the list row to occupy the entire available width because then the entire row will be highlighted and the animation cannot grow larger than the highlighted area.
Posts: 29
Joined: Mon Dec 31, 2007 2:33 am
Location: London


Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests