Chapter 7 cool effects-images and animations (2) and Chapter 7 cool effects

Source: Internet
Author: User

Chapter 7 cool effects-images and animations (2) and Chapter 7 cool effects
7.3 changeable multi-terminal-Drawable (drawing class)

Drawableresource is the most powerful and powerful resource in the androidsystem. You can directly use png1_jpg1_gif1_9.png and other images as resource files, or use multiple XML files as resource files.

The following describes various Drawable resources in detail.

7.3.1some common subclasses of drawable

The Drawable of the Android platform represents the resources that can be drawn on the screen. You can use getDrawable (int) to obtain the Drawable resource from the resource file, you can also use @ drawable in the XML resource file to reference A drawable resource. Drawable is a general abstract class. There are several drawing classes extended by the Drawable class:

L BitmapDrawable-Bitmap file,a bitmapimage file (.png).jpgor .gif ).

L NinePatchDrawable-Nine-Patch file, a PNG file with a telescopic connector, can be based on contenttelescopic image (.9.png ).

L StateListDrawable-State List, an XML file that references different Bitmap images for different States (for example, different images are used when a button is pressed ).

L PaintDrawable-Color: defines the resource in XML and specifies the Color of a rectangle (which can be included in a rounded corner.

L ShapeDrawable-Shape, an XML file that defines a geometric Shape, including color and gradient.

L AnimationDrawable-Animation.

 

Experience Sharing:

In Android, uppercase letters are not allowed in image resource file names and cannot start with a number. Otherwise, the resource index cannot be generated for the image in R. Pay special attention to this.

 

7.3.2BitmapDrawable

BitmapDrawable is converted into a bitmap. Generally, its resource files are stored in the res/drawable/directory, and the file names are used as resource IDs. For example:

Save an image in res/drawable/myimage.png. You can apply the image to a View in LayoutXML:

<ImageView

Android: layout_height = "wrap_content"

Android: layout_width = "wrap_content"

Android: src = "@ drawable/myimage"/>;

 

The following code obtains the image in Drawable mode:

Resources res = getResources ();

Drawable drawable = res. getDrawable (R. drawable. myimage );

7.3.3-NinePatchDrawable

NinePatchDrawable draws a scalable bitmap image. Android automatically adjusts the size to accommodate the displayed content. NinePatch is often used to set the View background. The biggest difference between NinePatch and a general image set as the background is that NinePatch can have an image that is not stretched.

Ninepatchdrawableis a standard PNG image with a quota of 1 RMB. Save it as .9.png and keep it in the res/drawable directory of the project. You can use the built-in Android tool (\ android-sdk \ tools \ draw9patch. bat) to create NinePatch. The boundary of one pixel is used to determine the scalable and static areas of the image.

The black line on the left represents the vertical stretch area of the image.

The black line above represents the horizontal stretch area.

The intersection of them indicates the area where both vertical and horizontal stretching can be performed.

The black line on the right represents the vertical area drawn by the content.

The black lines below represent the horizontal area of the content.

The intersection area is the area to be drawn by the content (android: padding effect ).

The following 7-1 may be easier to understand.

Figure 7-1 9 Figure Tool

 

Experience Sharing:

If the black lines on the bottom and right are not continuous during image creation, it will follow the leftmost (top) and rightmost (bottom) to calculate the content area. The line on the right and bottom is optional, and the line on the left and top cannot be omitted.

When the image is stretched, note that the part to be stretched is deformed. So what if we don't need to stretch, we just need to repeat the tile? BitmapDrawable can be used to achieve the following:

// Obtain the image

Bitmap bitmap = BitmapFactory. decodeResource (context. getResources (), resID );

// Convert to BitmapDrawable

BitmapDrawable bd = new BitmapDrawable (bitmap );

// Set the tile direction to X and Y (horizontal and vertical ).

Bd. setTileModeXY (TileMode. REPEAT, TileMode. REPEAT );

// Set the tile direction to X (horizontal)

// Bd. setTileModeX (TileMode. REPEAT );

// Set the image Jitter to true. Jitter can make the gradient smoother, so that there will be a broken line or color block to solve the problem, but will not make a transparent gradient black line disappear.

Bd. setDither (true );

// Set it as the background to achieve automatic tiled effect.

View. setBackgroundDrawable (bd );

 

7.3.4 Animated Image-StateListDrawable

StateListDrawable is a Drawable object defined in XML, which can present different images according to the state. For example, a Button has multiple States (pressed, focused, or other ). With StateListDrawable, You can provide different Button images for each status of the Button. You can also describe the status list in an XML file. Each <item> In the <selector> element represents each image, and each <item> uses different features to describe the timing of use. Each time the status changes, StateList will traverse from top to bottom, and the first item matching the current status will be used -- the selection process is not based on "best match ", only the first item that meets the minimum state standard.

The following is a simple example.

First look at the XML file, which is saved in res/drawable/selecter_btn.xml.

<? Xml version = "1.0" encoding = "UTF-8"?>

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

<Item android: state_pressed = "true"

Android: drawable = "@ drawable/button_pressed"/> <! -- Pressed -->

<Item android: state_focused = "true"

Android: drawable = "@ drawable/button_focused"/> <! -- Focused -->

<Item android: drawable = "@ drawable/button_normal"/> <! -- Default -->

</Selector>

 

Apply this Drawable to a View. The method used is the same as that used by Drawable.

<ImageView

Android: layout_height = "wrap_content"

Android: layout_width = "wrap_content"

Android: src = "@ drawable/selecter_btn"/>

7.3.5 another method for color filling-PaintDrawable

PaintDrawable is the color defined in XML and can be used as Drawable to fill the rectangular area (rounded corners can be included ). This Drawable behavior is like color filling.

Here is a simple example.

The XML file is saved in res/drawable/color_name.xml.

<? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>

<Drawable name = "solid_red"> # f00 </drawable>

<Drawable name = "solid_blue"> # 0000ff </drawable>

</Resources>

 

Apply this Drawable to a View.

<TextView

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: background = "@ drawable/solid_blue"/>

 

Experience Sharing:

Color Drawable is a simple resource. You can use the name feature to reference its value (no longer the name of an XML file ). Therefore, you can add multiple Color Drawable under the <resources> element in an XML file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.