drawable usage Analysis of graphic image processing in Android _android

Source: Internet
Author: User

This article illustrates the drawable usage of graphic image processing in Android. Share to everyone for your reference. Specifically as follows:

How to get the resources in res

Data Packet Package:android.content.res

Main Category: Resources

Its main interface is divided into the following three parts according to function:

Getxxxx ()

For example:

int getcolor (int id)
drawable getdrawable (int id)
String getString (int id) directly retrieves the resources stored in the Res
InputStream openrawresource (int id) gets the data stream of the resource, reads the resource data
void Parsebundleextras (Xmlresourceparser parser, Bundle outbundle) fetching data from an XML file
Resource provides the appropriate interface for each resource to obtain this resource, in addition to the direct acquisition of resources, but also provides additional data flow to obtain resources, which in future application development will be used frequently, then how to obtain the resources, as follows: Resource r = This.getcontext (). Getresources ();

Ii. How to get drawing objects in resources

Data Packet package:android.graphics.drawable

Main class: Drawable

Drawable is a virtual class, how to paint, you need to specifically analyze the drawable subclass, such as: bitmapdrawable

The main interfaces are as follows:

Bitmapdrawable ()
Bitmapdrawable (Bitmap Bitmap)
Bitmapdrawable (String filepath)
Bitmapdrawable (InputStream is)
void Draw (Canvas Canvas)
Final Bitmap Getbitmap ()
Final Paint Getpaint ()

Drawable is an abstract class, in bitmapdrawable we see the specific operation of the bitmap, looking at the Bitmapdrawable constructor, we will find that the Openrawresource () interface with the resource corresponds to the You can get a bitmap by using the following methods:

Resources R = This.getcontext (). Getresources ();
InputStream is = R.openrawresource (r.drawable.my_background_image);
Bitmapdrawable Bmpdraw = new Bitmapdrawable (is);
Bitmap bmp = Bmpdraw.getbitmap ();

Paint

Data Packet 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. The main definition: The style of the brush, brush size/color and so on.

Typeface

Data Packet Package:android.graphics

Introduction to the Android SDK: The typeface class specifies the typeface and intrinsic style of a font. The main definition is: Font.

Core Classes Display Resources

Data Packet Package:android.graphics

Main class: Canvas

Introduction to the Android SDK: the Canvas class holds the "draw" calls. To draw something, your need 4 basic components:a Bitmap to hold the pixels, A Canvas to host the draw calls (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 function of the structure, the main interface is divided into the following 3 parts:

Boolean clipxxxx () region Zone action: Difference INTERSECT REPLACE reverse_difference UNION XOR

void drawxxxx () drawing function

void rotate () void scale () void skew () void translate () Canvas action function
Region here needs special instructions: region is an area, the effective area of the canvas (Canvas), draw on the invalid area, without any changes to the canvas.

Drawable class

Drawable is a generic abstract class that is designed to tell you what you can draw. You'll find classes that extend a variety of drawings based on the Drawable class, see the table below, and of course you can inherit it to create your own drawing class.

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

Creating from a resource image file

A simpler way is to add a picture to your program, this file is then referenced through a resource file, supported by a PNG (preferred) JPG (acceptable) GIF (not recommended), which is obviously the preferred method for displaying an application's icon, and can also be used to display logos, The rest of the pictures can be used for example in games.

Put a picture resource, add your files to the res/drawable/directory in your project, from here, you can reference it to your code or your XML layout, that is, reference it can also use the resource number, such as you select a file as long as the suffix can be removed (for example: My_ Image.png refers to it is my_image).

Note: The SDK points out that in order to reduce the storage space of the picture, you may compress the picture while you build it, and if you don't want to be compressed, you can put the picture in the res/raw/directory.

Examples given in the SDK:

 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 it 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.W
  Rap_content, layoutparams.wrap_content));
  Add the ImageView to the layout and set the layout as the Content view Mlinearlayout.addview (i);
Setcontentview (mlinearlayout);
Get Drawable object: Resources res = mcontext.getresources (); 

drawable myimage = res.getdrawable (r.drawable.my_image); 

Note: Keeping one to each resource type ensures consistency in your project's state without worrying about having many different types of objects to instantiate it. For example, if you use the same image resource to instantiate two Drawable objects. Then modify a Drawables attribute (for example, Alpha), and unfortunately this effect will also appear on another object. So when dealing with multiple instance objects of the same resource, instead of directly converting to drawable, you should execute the Tween animation

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"/>

Creating from an XML file

By now, you should be more familiar with the Android principle of developing a user interface, so you should also understand the importance of defining an XML file for the purpose and flexibility of an object. This idea is used countless times for drawables.

If you want to create a Drawable object that does not depend on the exchange of variables or users, it should be a good idea to define it in XML. Even if you expect to change its properties in your application to increase the user experience. You should consider putting objects into XML because you can modify their properties at any time.

When you define a drawable in your XML, save the XML file to the Res/drawable directory in your engineering directory, and then retrieve and instantiate it by calling Resource.getdrawable (), passing it to the resource ID number in the XML file. Any drawable subclass supports the inflate method, which instantiates your program through XML. Any drawable supports XML extensions to use special XML attributes to help define attributes of an object, and you can view any drawable subclass document to see how to define an XML file.

The following defines a transitiondrawable:an extension of layerdrawables of that are intended to cross-fade between the one and second layer . It can be defined in a XML file with the <transition> element. Each drawable in the transition are defined in a nested <item>. See http://androidappdocs.appspot.com/reference/android/graphics/drawable/TransitionDrawable.html For more information about transitiondrawable.

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>

The following is instantiated and processed:

The public class Mainactivity extends activity {/** called the ' when ' is the ' The activity ' is a
  -a-created
  c 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 a dynamic two-dimensional picture, a Shapedrawable object may be of great help to you. By Shapedrawable, you can programmatically draw any image or style you think of.

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

The Shapedrawable class (like many other drawable types in the Android.graphics.drawable package) allows you to define the various properties of the drawable public method. Some properties you can need to adjust, including transparency, color filtering, opacity, color.

ninepatchdrawable

ninepatchdrawable painting is a scalable bitmap image, Android will automatically resize to accommodate the displayed content. An example is the Ninepatch for the background, using the standard Android button, the button must be scaled to accommodate the length of the changed characters

Ninepatchdrawable is a standard PNG image that includes an extra 1 pixel boundary, and you must save it with a suffix of. 9.png and remain in the engineering res/drawable directory.

This boundary is used to determine the scalable and static area of the image. You can draw one or more black 1 pixels on the left and top of the line to point out the retractable part (you can need a lot of retractable parts), its relative position is the same in the scalable part, so the large part is always very large.

You can also draw an optional drawable area on the right and bottom of the image (valid, inner margin lines). If your view object is set Ninepath to a background and then specifies a special view font, it will flex itself so that all the text adapts to the area (if any) that is designed according to the right and bottom lines, and of course the inner margin line is not included. Android can define a drawable area using the line on the left and the line above.

Let's clarify the two different lines, the left and top lines to define which pixels of the image are allowed to be copied when scaling. The bottom and right lines are used to define an image in a relative position, and the contents of the view are placed in it.

I hope this article will help you with your Android program.

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.