Android drawable Application

Source: Internet
Author: User

the android SDK provides a powerful class drawable. What does the abstract class drawable represent and how to use it? Drawable is a very abstract concept. It is learned and understood by a simple Program . Let's take a look at a simple example and draw a picture using the sub-class shapedrawable of drawable, as shown below:
public class testview extends view {
private shapedrawable mdrawable;
Public testview (context) {
super (context);
int x = 10;
int y = 10;
int width = 300;
int Height = 50;
mdrawable = new shapedrawable (New ovalshape ();
mdrawable. getpaint (). setcolor (0xff74ac23);
mdrawable. setbounds (X, Y, x + width, Y + height);
}< br> protected void ondraw (canvas)
super. ondraw (canvas);
canvas. drawcolor (color. white); // draw a white background
mdrawable. draw (canvas);
}< BR >}< br> the running result of the program is shown as follows:

Brief Analysis:
  1. Create an ovalshape (an elliptic );
  2. Use the created ovalshape to construct a shapedrawable object mdrawable
  3. Set the mdrawable color;
  4. Set the mdrawable size;
  5. Draw mdrawable on the canvas of testview;

This simple example can help us understand what drawable is. drawable is a printable object. It may be a bitmapdrawable or a shapedrawable object ), it may also be a layer (layerdrawable). Based on the drawing requirements, we can create a corresponding callable object to treat this callable object as a canvas )", you can operate on the printable objects on the canvas, and finally display the printable objects on the canvas, which is somewhat similar to the memory canvas.

The above is just a simple example of using drawable, which does not reflect the powerful functions of drawable. The android SDK illustrates the main role of drawable: define various animations in XML, read XML as a drawable resource, and display animations through drawable. The following example uses transitiondrawable to create an android project and modify it based on the project. The modification process is as follows:
1. Remove textview from layout/Main. xml and add imagview as follows:
<Imageview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: tint = "#55ff0000 ″
Android: src = "@ drawable/my_image"/>

2. Create an XML file named expand_collapse.xml with the following content:
<? XML version = "1.0" encoding = "UTF-8"?>
<Transition xmlns: Android = "http://schemas.android.com/apk/res/android”>
<Item Android: drawable = "@ drawable/image_expand"/>
<Item Android: drawable = "@ drawable/image_collapse"/>
</Transition>
Three PNG images are required and stored in the res \ drawable directory. The three images are named my_image.png?image_expand.png=image_collapse.png respectively.

3. ModifyCode, The content is as follows:
Linearlayout mlinearlayout;
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Mlinearlayout = new linearlayout (this );
Imageview I = new imageview (this );
I. setadjustviewbounds (true );
I. setlayoutparams (new gallery. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content ));
Mlinearlayout. addview (I );
Setcontentview (mlinearlayout );
Resources res = getresources ();
Transitiondrawable transition =
(Transitiondrawable) res. getdrawable (R. drawable. expand_collapse );
I. setimagedrawable (Transition );
Transition. starttransition (10000 );
}

4. If the modification is correct, run the program and the result is as follows:
Initial Image

Picture in transition

Last Image

The on-screen animation shows the transition from image_expand.pngto image_collapse.png, which is a transition animation defined in expand_collapse.xml. After reading this example, do you have a better understanding of drawable? Here we provideSource codeFor download, you can learn about other drawable based on this example to deepen your understanding of drawable.

1. Image Resources

Image resources are the simplest drawable resources *. PNG ,*. JPG *. gif and other formats are placed in the/RES/drawable-xxx directory, and the android SDK will automatically load the image in the compilation application, and generate a reference to this resource in the r resource list class.

Android does not allow uppercase letters in image resource file names and cannot start with a number.

Access resources in Java: [<package>.] R. drawable. <file_name>

Access resources in XML: @ [<package_name:>] drawable/file_name

To obtain the actual drawable object in the program, resources provides the drawable getdrawable (int id) method. This method obtains the actual drawable object based on the ID of the drawable resource in the r list class.

2. statelistdrawable Resources

Statelistdrawable is used to organize multiple drawable objects. When statelistdrawable is used as the background and foreground image of the target component, the drawable object displayed by the statelistdrawable object will automatically switch with the change of the target component form.

The root element of the XML file that defines the statelistdrawable object is <selector ../>. This element can contain multiple <item.../> elements, which can specify the following attributes:

Android: color or Android: drawable: Specifies the color or drawable object.

Android: state_xxx: Specifies a specific status

3. layerdrawable Resources

Similar to statelistdrawable, layerdrawable can also contain a drawable array. Therefore, the system will plot the drawable objects in the array order, the drawable object with the largest index will be drawn at the top.

The root element of the XML file that defines the layerdrawable object is <layer-list... />. The element can contain multiple <item... /> element, which can specify the following attributes:

Android: drawable: Specifies the drawable object that is one of the layerdrawable elements.

Android: ID: specify an identifier for the drawable object

Android: buttom | top | button: they are used to specify a length value to draw the drawable object to the specified position of the target component.

4. shapedrawable Resources

Shapedrawable is used to define a basic geometric image (such as a rectangle, circle, line, etc.). The root element of the XML file that defines shapedrawable is <shape... /> element, which can specify the following attributes:

Android: Shape = ["rectangle" | "Oval" | "line" | "ring"]: specifies the type of ry.

5. clipdrawable Resources

Clipdrawable indicates an "image fragment" captured from other bitmaps ". Define the clipdrawable object in the XML file to use the <clip.../> element.

You can specify the following three attributes:

Android: drawable: Specifies the truncated source drawable object.

Android: cliporientation: Specifies the direction to intercept. You can set horizontal or vertical intercept.

Android: gravity: Specifies the alignment mode when intercepting

Use the clipdrawable object to call the setlevel (INT level) method to set the size of the intercepted area. When the level is 0, the captured image fragment is empty. When the level is 10000, the whole image is taken.

6. animationdrawable Resources

animationdrawable represents an animation. The XML resource file defining the compensation animation uses the element as the root element. The following four elements can be specified in this element:

alpha: sets the transparency change

scale: Set the scaling of the image.

translate: sets the image's displacement transformation.

rotate: sets the image to be rotated.

the XML resource that defines the animation should be placed in the/RES/anim path. This path is not included by default when an Android Application is created using ADT, the developer needs to create the path by himself.

it is easy to define the animation population: Set the starting state of an image, including transparency, position, scaling ratio, and rotation) and set the image end state (including transparency, position, zoom ratio, rotation), and then set the animation duration, the Android system uses an animation to change the starting and ending status of the image.

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

access the animation resource file in the XML file: @ [ :] anim/file_name

to obtain the actual animation object in Java code, call the following method of animationutils: loadanimation (context CTX, int resld)

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.