Defining Integer Resources in XML

Defining Integer Resources in XML

In addition to strings and Boolean values, you can also store integers as resources. Integer resources are defined in XML under the /res/values project directory and compiled into the application package at build time.Integer values are appropriately tagged with the <integer> tag and represent a name- value pair.The name attribute is how you refer to the specific integer programmatically, so name these resources wisely.To use the integer resource, you must load it using the Resource class.The following code accesses your application’s integer resource named numTimesToRepeat:Android applications can store RGB color values, which can then be applied to other screen elements.You can use these values to set the color of text or other elements, such as the screen background. Color resources are defined in XML under the /res/values proj- ect directory and compiled into the application package at build time.The example at the beginning of the chapter accessed a color resource. Color resources are simply integers.The following code retrieves a color resource calledDimension values are appropriately tagged with the <dimen> tag and represent a name- value pair. Dimension resources are defined in XML under the /res/values project di- rectory and compiled into the application package at build time.

Be cautious when choosing dimension units for your applications. If you are planning to tar- get multiple devices, with different screen sizes and resolutions, then you need to rely heav- ily on the more scalable dimension units, such as dp and sp, as opposed to pixels, points, inches, and millimeters.You can specify simple colored rectangles by using the drawable resource type, which can then be applied to other screen elements.These drawable types are defined in specific paint colors, much like the Color resources are defined.Drawable subclasses, such as ShapeDrawable. Drawable XML definition files are stored in the /res/drawable directory within your project along with image files.This is not the same as storing <drawable> resources, which are paintable drawables. PaintableDrawableThere are many additional drawable resource types that can be specified as XML resources. These special drawables correspond to specific drawable classes such as ClipDrawable and LevelListDrawable. For information on these specialized drawable types, see the An- droid SDK documentation.

Working with Images

Applications often include visual elements such as icons and graphics.Android supports several image formats that can be directly included as resources for your application.These image formats are shown in Table 6.5.These image formats are all well supported by popular graphics editors such as Adobe Photoshop, GIMP, and Microsoft Paint.The Nine-Patch Stretchable Graphics can be cre- ated from PNG files using the draw9patch tool included with the Android SDK under the /tools directory.All resources filenames must be lowercase and simple (letters, numbers, and underscores only). This rule applies to all files, including graphics.

Working with Nine-Patch Stretchable Graphics

Phone screens come in various dimensions. It can be handy to use stretchable graphics to allow a single graphic that can scale appropriately for different screen sizes and orienta- tions or different lengths of text.This can save you or your designer a lot of time in creat- ing graphics for many different screen sizes.Android supports Nine-Patch Stretchable Graphics for this purpose. Nine-Patch graphics are simply PNG graphics that have patches, or areas of the image, defined to scale appropriately, instead of scaling the entire image as one unit. Often the center segment is transparent.tool included with the Tools directory of the Android SDK.We talk more about compat- ibility and using Nine-Patch graphics in Chapter 25.Images resources are simply another kind of Drawable called a BitmapDrawable. Most of the time, you need only the resource ID of the image to set as an attribute on a user inter- face control.

There is also a special resource type called <selector>, which can be used to define differ- ent colors or drawables to be used depending on a control’s state. For example, you could define a color state list for a Button control: gray when the button is disabled, green whenit is enabled, and yellow when it is being pressed. Similarly, you could provide different draw- ables based on the state of an ImageButton control. For more information, see the Android SDK documentation regarding the color and drawable state list resources.Android supports frame-by-frame animation and tweening. Frame-by-frame animation involves the display of a sequence of images in rapid succession.Tweened animation in- volves applying standard graphical transformations such as rotations and fades upon a sin- gle image.Tweened animation sequences are not tied to a specific graphic file, so you can write one sequence and then use it for a variety of different graphics. For example, you can make moon, star, and diamond graphics all pulse using a single scaling sequence, or you can make them spin using a rotate sequence.

Cours gratuitTélécharger le document complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *