Quickly design and develop compelling user interfaces for your Android applications
Animations are an important element in the user interface design of a modern application. However, it's also easy to overuse animations in your designs. A general guideline for animation use in a non-game application is—only animate user interactions and notifications, and keep the duration short so that it doesn't impact the user's experience negatively. For a game, more animation is generally acceptable (or even expected).
Layout animations and transitions provide useful status information to the user. When using a screen transition you tell your user what has just happened, or what is about to happen. Different transitions signify different events to your users, knowing what transition to use for each different activity will let your users know what kind of action is about to be taken. Layout animations are an important part of your user feedback, leaving them out or using the wrong one in the wrong place can leave your users irritated, or slightly confused ("change dazed"). Using the right animations will improve user experience, and can even speed up their use of the application by giving them brief cues as to what they are expected to do next.
Any View or ViewGroup object in Android can have an animation attached to it. animations are generally defined as application resources in an XML file, and Android provides a few useful defaults in the android package. Android also includes several View classes which are designed specifically to handle animations. With these classes you will find that they have layout attributes which allow you to set a particular types of animations that will be used upon certain actions. However, animations are generally not specified in a layout file, instead they rely on the Java code to set and start Animation objects.
The main reason why animations are not normally specified as part of the layout XML is very simple—when should they run? Many animations can be used as a response to user input, letting the user know what's happening. Most animations will in some way or the other be triggered by a user's action (unless they are there to serve as a notification). Thus you will need to specify both—which animation to run on a widget, and the signal about when the animation should run. The default Android animations will begin animating immediately, while other animation structures may have a scheduled delay before they start.
We'll start of by creating a selector Activity and a simple NewsFeedActivity. In a news feed, we'll animate the latest headlines "in and out" using a timer. For this example we'll be working with some of the default animations provided by Android and driving the process mainly through the layout resources.
android create project -n AnimationExamples -p AnimationExamples
-k com.packtpub.animations -a AnimationSelector -t 3
<LinearLayout
android_orientation="vertical"
android_layout_width="fill_parent"
android_layout_height="fill_parent">
<Button android_id="@+id/news_feed"
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_layout_marginBottom="10dip"
android_text="News Feed"/>
<LinearLayout
android_orientation="vertical"
android_layout_width="fill_parent"
android_layout_height="fill_parent">"
<TextSwitcher
android_id="@+id/news_feed"
android_inAnimation="@android:anim/slide_in_left"
android_outAnimation="@android:anim/slide_out_right"
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_text=""/>
<string-array name="headlines">
<item>Pwnies found to inhabit Mars</item>
<item>Geeks invent "atoms"</item>
<item>Politician found not lying!</item>
<!-- add some more items here if you like -->
</string-array>
<activity android_name=".NewsFeedActivity" android_label="News
Feed" />
public class NewsFeedActivity
extends Activity implements Runnable {
private final Handler handler = new Handler();
private TextSwitcher newsFeed;
private String[] headlines;
private int headlineIndex;
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
headlines = getResources().getStringArray(R.array.headlines);
newsFeed = (TextSwitcher)findViewById(R.id.news_feed);
newsFeed.setFactory(new ViewFactory() {
public View makeView() {
return new TextView(NewsFeedActivity.this);
}
});
protected void onStart() {
super.onStart();
headlineIndex = 0;
handler.postDelayed(this, 3000);
protected void onStop() {
super.onStop();
handler.removeCallbacks(this);
public void run() {
newsFeed.setText(headlines[headlineIndex++]);
if(headlineIndex >= headlines.length) {
headlineIndex = 0;
}Animatng Widgets and Layouts
finally {
handler.postDelayed(this, 3000);
}
public class AnimationSelector
extends Activity implements OnClickListener {
setContentView(R.layout.main);
((Button)findViewById(R.id.news_feed)).
setOnClickListener(this);
public void onClick(final View view) {
switch(view.getId()) {
case R.id.news_feed:
startActivity(new Intent(this, NewsFeedActivity.class));
The TextSwitcher is an example of an animation utility View. In this case it's the perfect structure to swap between the news headlines, displaying one headline at a time and animating a transition between each of the texts. The TextSwitcher object creates two TextView objects (using the anonymous ViewFactory class). When you use the setText method, the TextSwitcher changes the text of the "of screen" TextView and animates a transition between the "on screen" TextView and the "of screen" TextView (with the new text content displayed).
The TextSwitcher class requires that you specify two animation resources for it to work with, in order to create its transition effect:
In the previous case, we made use of the default slide_in_left and slide_out_right animations. Both of these are examples of translation-based animations due to the fact that they actually alter the "on screen" position of the TextView objects in order to create their effect.