• No results found

EOverride

91Using resources

<?xml version="1.0" encoding="utf-8"?> <resources>

<style name="intro_blurb"> <item name="android:textSize">22sp</item> <item name="android:textColor">#ee7620</item> <item name="android:textStyle">bold</item> </style>

<style name="label">

<item name="android:textSize">18sp</item> <item name="android:textColor">#ffffff</item> </style>

<style name="edit_text">

<item name="android:textSize">16sp</item> <item name="android:textColor">#000000</item> </style>

. . . remainder of file omitted for brevity </resources>

The Android styles approach is a similar concept to using Cascading Style Sheets (CSS) with HTML. Styles are defined in styles.xml and then referenced from other resources or code. Each <style> element

B

has one or more <item> children that define a single setting

C

. Styles are made up of the various View settings: sizes, colors, margins, and such. Styles are very helpful because they facilitate easy reuse and the ability to make changes in one place. Styles are applied in layout XML files by associat- ing a style name with a particular View component, such as style="@style/ intro_blurb" (note that in this case style is not prefixed with the android:

namespace; it is a custom local style and not one provided by the platform).

Styles can be taken one step further and used as themes. While a style refers to a set of attributes applied to a single View element, themes refer to a set of attributes being applied to an entire screen. Themes can be defined in exactly the same <style>

and <item> structure as styles are. To apply a theme you simply associate a style with an entire Activity, such as: android:theme="@android:style/[stylename]".

Along with styles and themes, Android supports a specific XML structure for defin- ing arrays as a resource as well. Arrays are placed in source in res/values/arrays.xml and are helpful for defining collections of constant values, such as the cuisines we used to pass to our ArrayAdapter back in listing 3.1. Listing 3.10 shows how these arrays are defined in XML.

<?xml version="1.0" encoding="utf-8"?> <resources> <array name="cuisines"> <item>ANY</item> <item>American</item> <item>Barbeque</item> <item>Chinese</item> <item>French</item>

Listing 3.9 Values resource defining reusable styles, styles.xml

Listing 3.10 Arrays.xml used for defining cuisines and ratings

Use a <style> element

B

Use an

<item> element

C

Define <array> elements

B

<item>German</item> <item>Indian</item> <item>Italian</item> <item>Mexican</item> <item>Thai</item> <item>Vegetarian</item> <item>Kosher</item> </array> </resources>

Arrays are defined as resources using an <array> element with a name attribute

B

and include any number of <item> child elements

C

to define each array member. You can access arrays in code using the syntax shown in listing 3.1: String[] ratings= getResources().getStringArray(R.array.ratings).

Raw files and XML are also supported through resources. Using the res/raw and res/xml directories, respectively, you can package these file types with your applica- tion and access them through either Resources.openRawResource(int id) or

Resources.getXml(intid).

Going past simple values for strings, colors, and dimensions and more involved but still straightforward structures for styles, arrays, raw files, and raw XML, the next type of resources we need to explore are animations.

3.3.5 Providing animations

Animations are more complicated than other Android resources but are also the most visually impressive. Android allows you to define animations that can rotate, fade, move, or stretch graphics or text. While you don’t want to go overboard with a con- stantly blinking animated shovel, an initial splash or occasional subtle animated effect can really enhance your UI.

Animation XML files are placed in the res/anim source directory. There can be more than one anim file, and, as with layouts, you reference the respective animation you want by name/id. Android supports four types of animations:

■ <alpha>—Defines fading, from 0.0 to 1.0 (0.0 being transparent)

■ <scale>—Defines sizing, X and Y (1.0 being no change)

■ <translate>—Defines motion, X and Y (percentage or absolute)

■ <rotate>—Defines rotation, pivot from X and Y (degrees)

In addition, Android provides several attributes that can be used with any animation type:

■ duration—Duration in milliseconds

■ startOffset—Offset start time in milliseconds

■ interpolator—Used to define a velocity curve for speed of animation Listing 3.11 shows a very simple animation that can be used to scale a View.

<?xml version="1.0" encoding="utf-8"?>

<scale xmlns:android="http://schemas.android.com/apk/res/android"

Listing 3.11 Example of an animation defined in an XML resource, scaler.xml

93