Android Learning 15--using (drawable) resources

Source: Internet
Author: User

1. Picture Resources

Picture resources is the simplest drawable resources, as long as the *.png, *.jpg*,. gif and other formats in the/res/drawable-xxx directory, the Android SDK will automatically load the image in the compilation app, and generate a reference to the resource in the R resource manifest class.

Android does not allow uppercase letters in the file name of the picture resource, and cannot start with a number.

Accessing Resources in Java: [.] R.drawable.

Accessing resources in XML: @[]drawable/file_name

In order to obtain the actual Drawable object in the program, the resources provide a drawable getdrawable (int id) method that takes the actual drawable object from the ID of the drawable resource in the R manifest class.

2. Statelistdrawable Resources

Statelistdrawable is used to organize multiple drawable objects. When using statelistdrawable as the background and foreground picture of the target component, the Drawable object displayed by the Statelistdrawable object automatically switches as the shape of the target component changes.

The root element of the XML file that defines the Statelistdrawable object is the element that can contain more than one element, which can specify the following properties:

Android:color or android:drawable: Specify a color or Drawable object

ANDROID:STATE_XXX: Specify a specific state

3. Layerdrawable Resources

A bit similar to statelistdrawable, layerdrawable can also contain an drawable array, so the system will draw them in the order of the array of these drawable objects, and the Drawable object with the largest index will be drawn on top.

The root element of the XML file that defines the Layerdrawable object is the element that can contain more than one element, which can specify the following properties:

Android:drawable: Specifies the Drawable object as one of the layerdrawable elements.

Android:id: Specifies an identity for the Drawable object

Android:buttom|top|button: They are used to specify a length value that specifies that the Drawable object is drawn to the specified location of the target component

4. Shapedrawable Resources

Shapedrawable is used to define a basic geometry (such as rectangles, circles, lines, etc.), the root element of the XML file that defines the shapedrawable is an element that can specify the following properties:

android:shape=["Rectangle" | " Oval "|" Line "|" Ring "]: Specifies what type of geometry is defined

5. Clipdrawable Resources

Clipdrawable represents a "picture fragment" that is intercepted from another bitmap. Defines the Clipdrawable object using elements in an XML file.

The following three properties can be specified:

Android:drawable: Specifies the intercepted source drawable object

Android:cliporientation: Specify the Intercept direction to set the horizontal intercept or vertical intercept

Android:gravity: Specifies the alignment of the Intercept

Use the Clipdrawable object to invoke the Setlevel (int level) method to set the size of the intercepted area. When level is 0 o'clock, the captured picture fragment is empty, and when level is 10000, the entire picture is truncated.

6. Animationdrawable Resources

Animationdrawable represents a single animation. The XML resource file that defines the tweened animation takes an element as the root element, within which you can specify the following 4 elements:

Alpha: Setting changes in transparency

Scale: Set the picture to zoom and change

Translate: Setting the image for displacement transformation

Rotate: Set picture to rotate

The XML resource that defines the animation should be placed under the/res/anim path, which is not included by default when using ADT to create an Android app, and developers need to create the path themselves.

The idea of defining a tweened animation is simple: Set the start state of a picture (including transparency, position, zoom ratio, rotation), and set the end state of the picture (including transparency, position, zoom ratio, rotation), and then set the duration of the animation, The Android system uses an animated effect to change the image from the start state to the end state.

Accessing the animation resource file in Java code: [.] R.anim.

Accessing an animation resource file in an XML file: @[:]anim/file_name

In order to get the actual animation object in Java code, you can call animationutils in the following ways: Loadanimation (Context ctx,int RESLD)

Android Learning 15--using (drawable) resources

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.