Software Development optimize

Pro tip: Use Android's ObjectAnimator class to flip a view

Follow the three steps in this Android animation tutorial to learn how to achieve a wrap around the y-axis.

6_Android_logo.png
Animators are a hot topic in the Android world and in my TechRepublic articles. As consumer-facing apps compete for eyeballs in the market SDK and platform vendors are throwing more eye candy at developers than one can easily keep up with. Slick transitions that used to require third-party libraries like NineOldAndroids and complicated custom animations using Android's 3D Camera class can now be pulled off with little effort.

I have spoken to a number of developers who agree with me that lately it's difficult just keeping up with what options are at our disposal. This tutorial shows how easily Android's ObjectAnimator class can achieve what was previously difficult animation: a wrap around the y-axis. If you prefer, you can download and import the entire project directly into Eclipse.

1. Create a new Android project. You need to target Android 3.1 (Honeycomb) or better.

2. In the /res folder, modify the activity_main.xml layout to center a single text view on the screen.

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000" >

    <TextView
        android:id="@+id/text_view"
        android:layout_centerInParent="true"
        android:textSize="36sp"
        android:textStyle="bold"
        android:textColor="#9600FF00"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

3. In the /src folder, open the MainActivity.java file. Load the animation in the on create override and apply it to the view.

MainActivity.java
package com.authorwjf.rotateonyaxis;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.animation.ObjectAnimator;
import android.app.Activity;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		View v = findViewById(R.id.text_view);
		ObjectAnimator animation = ObjectAnimator.ofFloat(v, "rotationY", 0.0f, 360f);
		animation.setDuration(3600);
		animation.setRepeatCount(ObjectAnimator.INFINITE);
		animation.setInterpolator(new AccelerateDecelerateInterpolator());
		animation.start();
	}

}

Believe it or not, we're done. Check out the demo video for yourself.


The key to knowing what you can do with the ObjectAnimator class is knowing what properties can be animated. It takes a bit of digging to find on the Android Developers site, but here is the list of properties you can play with.

If you enjoy working with Android animations, why not enter our code challenge? The deadline has been extended to Friday, December 6th.


About

William J Francis began programming computers at age eleven. Specializing in embedded and mobile platforms, he has more than 20 years of professional software engineering under his belt, including a four year stint in the US Army's Military Intellige...

0 comments