Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

32. Property Animation


For an app to be functional, all you need to do is write your code correctly so that it does not crash. For
an app to be a joy to use, though, you need to give it more love than that. You need to make it feel like
a real, physical phenomenon playing out on your phone or tablet’s screen.


Real things move. To make your UI move, you animate its elements into new positions.


In this chapter, you will write an app called Sunset that shows a scene of the sun in the sky. When you
press on the scene, it will animate the sun down below the horizon, and the sky will change colors like
a sunset.


Building the Scene


The first step is to build the scene that will be animated. Create a new project called Sunset. Make sure
that your minSdkVersion is set to 19 and use the empty activity template. Name your main activity
SunsetActivity and add SingleFragmentActivity.java and activity_fragment.xml to your
project.


Now, build out your scene. A sunset by the sea should be colorful, so it will help to name a few colors.
Add a colors.xml file to your res/values folder, then add the following values to it:


Listing 32.1  Adding sunset colors (res/values/colors.xml)



#3F51B5
#303F9F
#FF4081
#fcfcb7
#1e7ac7
#ec8100
#05192e
#224869

Rectangular views will make for a fine impression of the sky and the sea. But people will not buy a
rectangular sun, no matter how much you argue in favor of its technical simplicity. So, in the res/
drawable/ folder, add an oval shape drawable for a circular sun called sun.xml.


Listing 32.2  Adding sun XML drawable (res/drawable/sun.xml)


<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">



Free download pdf