Android canvas and drawing-canvas and drawables (3)

Source: Internet
Author: User

This article translated from: http://developer.android.com/guide/topics/graphics/2d-graphics.html

Drawing

Android provides a custom 2D graphics class library for drawing graphics and images. The android. Graphics. drawable package can find a common class used to draw two-dimensional images.

This article describes how to use a drawable object to draw a graph and how to use a child class of the drawable class. For information about using a drawable object to draw frame animations, see the drawing animation documentation (http://developer.android.com/guide/topics/graphics/drawable-animation.html)

The drawable object is a general abstraction of drawing. You will find that many sub-classes of the drawable class are used to draw special types of graphics, including bitmapdrawable, shapdrawable, picturedrawable, and layerdrawable. Of course, you can also inherit these classes to define your own drawable objects with unique behaviors.

There are three methods to define and initialize a drawable object: 1. use an image saved in the project resource; 2. use an XML file that defines the properties of a drawable object; 3. use a common class constructor. Here we will focus on the first two technologies (using the constructor technology is no stranger to developers ).

Create a drawable object from the resource Image

Adding images to an application by referencing an image file in a project resource is a simple method. Supported image formats include: PNG (recommended), JPG (acceptable), and GIF (not recommended ). This technology is mainly used for the icons, logos of applications, or the images used in the game.

To use image resources, you only need to add the images to the res/drawable/directory of the project. The application code or XML layout will reference these images from there. The ID of the image resource is used no matter where the image is used. This ID is a file name without the file type Extension (for example, my_imagerefers to the my_image.png file ).

Note: image resources placed in the Res/drawable/directory can be automatically optimized and compressed into lossless images by the aapt tool during compilation. For example, a PNG Image of no more than 256 colors can be converted into an 8-bit PNG image with a color palette. In this way, images of the same quality are obtained, but less memory is required. Therefore, you need to understand the changes in the images placed in this directory during compilation. If you plan to read an image in the form of a bitstream to convert the image into a bitmap, you need to put the image in the Res/raw/folder, images in this folder will not be optimized.

Sample Code

The following code snippet demonstrates how to create an imageview object that uses the image to draw resources and add the object to the layout:

 
Linearlayout mlinearlayout;

Protectedvoid oncreate (bundle
Savedinstancestate ){
Super. oncreate (savedinstancestate );

// Create a linearlayout in which to add the imageview
Mlinearlayout
= Newlinearlayout (this );

// Instantiate an imageview and define its properties
Imageview I
= Newimageview (this );
I. setimageresource (R. drawable. my_image );
I. setadjustviewbounds (true );//
Set the imageview bounds to match the drawable's dimensions
I. setlayoutparams (newgallery. layoutparams (layoutparams. wrap_content,
Layoutparams. wrap_content ));

// Add the imageview to the layout and set the layout as the content View
Mlinearlayout. addview (I );
Setcontentview (mlinearlayout );
}

In another case, if you want to process image resources as drawable objects, you need to create a drawable object like the following code:

Resources res
= Mcontext. getresources ();
Drawable myimage
= Res. getdrawable (R. drawable. my_image );

Note: Each type of resource in the project can only be in one State, no matter how many objects are instantiated. For example, if you use the same image resource to instantiate two drawable objects, you only need to change one of the drawable objects (for example, transparency), and the other will also be affected. Therefore, when processing multiple instances of an image resource, you must use a tween animation to replace the direct drawable object transfer.

Example XML

The following XML snippet shows how to add a painting resource to an imageview element in the XML layout:


<Imageview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: tint = "#55ff0000"
Android: src = "@ drawable/my_image"/>

For more information about using project resources, see resource and asset documentation (http://developer.android.com/guide/topics/resources/index.html)

Create a drawable object from resource XML

Now you are familiar with the development principles of the android user interface. Therefore, we understand the powerful functions and flexibility of defining objects in XML. This method applies from the view object to the drawable object. If a drawable object is created, it does not depend on the application code or a variable defined in the user interface, but defines it in an XML file. This is a good choice. Even if you want to change the properties of a drawable object during user interaction with the application, you should also consider defining the object in XML so that once it is instantiated, you can modify its attributes at any time.

Once you use XML to define a drawable object, you need to save the definition in the Res/drawable/directory of the project, and then obtain and instantiate the object by calling the resources. getdrawable () method.

Any drawable subclass that provides the inflate () method can be defined in XML and instantiated by the application. Each drawable object that provides the XML Filling Capability uses special XML attributes to help define the attributes of the object. For information about how each drawable subclass is defined in XML, see the corresponding class documentation.

Example

The following XML defines a transitiondrawable object:


<Transitionxmlns: Android = "http://schemas.android.com/apk/res/android">
<Itemandroid: drawable = "@ drawable/image_expand">
<Itemandroid: drawable = "@ drawable/image_collapse">
</Transition>

This XML should be saved in the Res/drawable/expand_collapse.xml file. The following code will sample the transitiondrawable object and set it as the imageview object content:


Resources res
= Mcontext. getresources ();
Transitiondrawable transition
= (Transitiondrawable)
Res. getdrawable (R. drawable. expand_collapse );
Imageview Image
= (Imageview) findviewbyid (R. Id. toggle_image );
Image. setimagedrawable (Transition );

Then, this transition effect can be run using the following code:

Transition. starttransition (1000 );

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.