Graphic Images in Android-accessing image drawable

Source: Internet
Author: User

I. How to obtain resources in res
Package: Android. content. Res
Main Category: Resources
Its main interfaces are divided into the following parts by function:

Getxxxx ()

For example:

Int getcolor (int id)

Drawable getdrawable (int id)

String getstring (int id) directly obtains the resources stored in res.

Inputstream openrawresource (int id) obtains the data stream of the resource and reads the resource data.

Void parsebundleextras (xmlresourceparser parser, bundle outbundle) obtains data from the XML file

Resource provides corresponding interfaces for each type of resources to obtain such resources. In addition to directly obtaining resources, it also provides additional methods to obtain resources in the form of data streams, which will be used in future applications.ProgramResources are frequently used during development. How to obtain resources is as follows: Resources r = This. getcontext (). getresources ();

 

Ii. How to obtain drawing objects in resources
Package: Android. Graphics. drawable
Main Category: drawable
Drawable is a virtual class. You need to analyze the sub-class of drawable, for example, bitmapdrawable.
The main interfaces are as follows:

Bitmapdrawable ()

Bitmapdrawable (Bitmap bitmap)

Bitmapdrawable (string filepath)

Bitmapdrawable (inputstream is)

Void draw (canvas)

Final bitmap getbitmap ()

Final paint getpaint ()

Drawable is an abstract class. In bitmapdrawable, we can see the specific bitmap operations. After carefully reading the bitmapdrawable constructor, we can find that it is consistent with openrawresource () in the resource () you can obtain the bitmap using the following method:
Resources r = This. getcontext (). getresources ();
Inputstream is = R. openrawresource (R. drawable. my_background_image );
Bitmapdrawable BMP draw = new bitmapdrawable (is );
Bitmap BMP = BMP draw. getbitmap ();

Paint

Package: Android. Graphics

Introduction In the android SDK: The paint class holds the style and color information about how to draw geometries, text and bitmaps. It mainly defines the style, size, and color of the paint brush.

Typeface
Package: Android. Graphics
Introduction In Android SDK: the typeface class specifies the typeface and intrinsic style of a font. The main definition is font.

Core class display resources
Package: Android. Graphics
Main Category: canvas
Introduction In Android SDK: the canvas class holds the "Draw" CILS. to draw something, you need 4 basic components: a bitmap to hold the pixels, a canvas to host the draw CALS (writing into the bitmap), a drawing primitive (e.g. rect, path, text, bitmap), and a paint (to describe the colors and styles for the drawing ).
According to the structure function, the main interface is divided into the following three parts:

Boolean clipxxxx () region Region Operation: Difference intersect replace reverse_difference Union XOR

Void drawxxxx () plotting function

Void rotate () void Scale () void skew () void translate () canvas operation function

Region: Region is a region, that is, a valid region in the canvas. It is draw in an invalid region and has no changes to the canvas.

Drawable class
Drawable is a general abstract class that aims to tell you what can be drawn. You will find that various drawing classes are extended based on the drawable class. See the following table. Of course, you can inherit it to create your own drawing class.

 

There are three ways to define and instantiate a drawable: save an image to your project resources, use an XML file to describe the drawable attribute, or use a normal class to construct it. Here we will discuss two technologies (for a developer with development experience, the construction is not the latest technology ).

Create from resource Image File
A simple method is to add an image to your program, and then reference the file through the resource file. Supported file types include PNG (preferred) JPG (acceptable) GIF (not recommended). Obviously, this is the preferred method for displaying application icons. It can also be used to display logos, and other images can be used in games, for example.

Add an image resource to the res/drawable/directory of your project. From here, you can reference it to yourCodeOr in your xml layout, that is, you can reference it with the resource number. For example, if you select a file, you only need to remove the suffix (for example, my_image.png references it as my_image ).

Note: The SDK indicates that, in order to reduce the storage space of images, images may be compressed during build. If you do not want to be compressed, you can put the images in the Res/raw/directory.

SDK example:

Linearlayout mlinearlayout;

Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

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

// Instantiate an imageview and define its properties
Imageview I = new imageview (this );
I. setimageresource (R. drawable. my_image );
I. setadjustviewbounds (true); // set the imageview bounds to match the drawable's dimensions
I. setlayoutparams (new gallery. 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 );
} Obtain the drawable object:

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

Note: keeping one to one of each resource type ensures the consistency of your project status, so you don't have to worry about instantiating many different types of objects. For example, if two drawable objects are instantiated using the same image resources. Then modify the attributes of a drawables (such as alpha). Unfortunately, this effect will also appear on another object. Therefore, when processing multiple instance objects of the same resource, you should execute tween animation instead of directly converting to drawable.

How to add resources to imageview:
<Imageview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: tint = "#55ff0000"
Android: src = "@ drawable/my_image"/> Create
Now, you should be familiar with developing a user interface based on Android principles. Therefore, you should also understand the role and flexibility of defining an XML file for objects. This concept has been used countless times for drawables.

If you want to create a drawable object that does not depend on variables or user exchanges, defining it in XML should be a good method. Even if you want to change its attributes in your application to increase the user experience. You should consider placing the object in XML, because you can modify its attributes at any time.

When you define a drawable in your xml file, save the XML file to the res/drawable directory in your project directory, and then call resource. getdrawable () is used to retrieve and instantiate the resource ID in the XML file. Any drawable subclass supports the inflate method, which will instantiate your program through XML. Any drawable supports XML extension to use special XML attributes to help define object attributes. You can view any drawable subclass document to see how to define XML files.

The following defines a transitiondrawable: an extension of layerdrawables that is intended to cross-Fade between the first and second layer. it can be defined in an XML file with the <transition> element. each drawable in the transition is defined in a nested <item>. for more information about transitiondrawable, refer.

Define it in RES/drawable/expand_collapse.xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<Transition xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item Android: drawable = "@ drawable/pic1"/>
<Item Android: drawable = "@ drawable/pic2"/>
</Transition>
Instantiate and process the following:

Public class mainactivity extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Resources res = getresources ();
Transitiondrawable trans = (transitiondrawable) res. getdrawable (R. drawable. expand_collapse );
Imageview image = (imageview) findviewbyid (R. Id. imageview01 );
Image. setimagedrawable (trans );
Trans. starttransition (3000 );
}
} Shapedrawable
When you want to draw some dynamic two-dimensional images, a shapedrawable object may be of great help to you. Through shapedrawable, you can program to draw any image and style you think.

Shapedrawable inherits drawable, so you can call some functions in drawable, such as the view background, through setbackgrounddrawable. Of course, you can draw your image in a custom view layout, because shapedrawable has its own draw () method. You can create a subclass of the view during the view. ondraw () method to draw shapedrawable.

The shapedrawable class (like many other drawable types in the Android. Graphics. drawable package) allows you to define various attributes of the drawable public method. You can adjust some attributes, including transparency, color filtering, opacity, and color.

 

Ninepatchdrawable

Ninepatchdrawable draws a scalable bitmap image. Android automatically adjusts the size to accommodate the displayed content. In one example, ninepatch is used as the background. With the standard Android button, the button must be scaled to accommodate characters with varying length.

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.

This boundary is used to determine the scalable and static areas of an image. You can draw one or more black pixels on the left and above to point out the Scalable part (you can need a lot of scalable parts). The relative position is the same as that of the Scalable part, so the big part is always very big.

You can also draw an optional drawable area (valid, padding) on the Right and bottom of the image ). If your view object sets ninepath as the background and then specifies a special view font, it automatically scales all the text to adapt to the area designed based on the right and bottom lines (if any ), of course, the padding line is not included. Android can use the line on the left and the line above to define a drawable area.

Let's clarify these two different lines. The lines on the left and top define which pixels of the image can be copied during scaling. The bottom line and the right line are used to define an image in a relative position, and the View content is placed in it.

 

 

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.