Drawable usage analysis of graphics and image processing in Android

This paper illustrates the drawable usage of graphics and image processing in Android. Share with you for your reference. The details are as follows:

1、 How to obtain resources in Res

Package: android.content.res

Main category: Resources

The main interfaces are divided into the following three parts according to their functions:

getXXXX()

For example:

Int getcolor (int ID) drawable getdrawable (int ID) string getString (int ID) directly get the resource stored in res InputStream openrawresource (int ID) get the data stream of the resource and read the resource data void parsebundleextras (xmlresourceparser, parser, bundle outbundle) Obtain data from XML files. Resource provides a corresponding interface for each resource to obtain such resources. In addition to obtaining resources directly, it also provides obtaining resources in the form of data flow, which will be often used in future application development. How to obtain resources is as follows: Resources R = this. Getcontext(). Getresources(); 2、 How to obtain drawing objects in resources

Package: android.graphics.drawable

Main class: drawable

Drawable is a virtual class. How to draw a picture, you need to analyze the subclass of drawable, such as bitmapdrawable

Its 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. We can see the specific operation of bitmapdrawable in bitmapdrawable. After carefully looking at the constructor of bitmapdrawable, we will find that it corresponds to the openrawresource() interface in resource. You can obtain bitmaps through the following methods:

Paint

Package: android.graphics

Introduction in Android SDK: the paint class holds the style and color information about how to draw geometries, text and bitmaps. It mainly defines: brush style, brush size / color, etc.

Typeface

Package: android.graphics

Introduction in Android SDK: the typeface class specifies the typeface and intrinsic style of a font.

Core class display resources

Package: android.graphics

Main category: Canvas

Introduction in Android SDK: the canvas class holds the "draw" calls. To draw something, you 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 functions of the structure, the main interfaces are divided into the following three parts:

Boolean clipxxx() region operation: difference intersect replace reverse_ DIFFERENCE UNION XOR

Void drawxxxx() drawing function

Void rotate() void scale() void skew() void translate() canvas operation function region needs special instructions here: region is an area, that is, the effective area in the canvas. Drawing on the invalid area does not change the canvas.

Drawable class

Drawable is a general abstract class. Its purpose is to tell you what can be drawn. You will find that various drawing classes are extended based on the drawable class, as shown in the table below. 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 project resources, use an XML file to describe the drawable attribute, or construct it with a normal class. Next, we will discuss two technologies (construction is not the latest technology for a developer with development experience).

Create from resource image file

A relatively simple method is to add an image to your program, and then reference the file through the resource file. The supported file types are PNG (preferred) JPG (acceptable) GIF (not recommended). Obviously, this is the preferred method for displaying the icon of the application. It can also be used to display the logo. The other images can be used in games, for example.

Add a picture resource to the RES / drawable / directory of your project. From here, you can reference it to your code or your XML layout. In other words, you can also use the resource number. For example, if you select a file, just remove the suffix (for example, my_image.png refers to my_image).

Note: the SDK points out that in order to reduce the storage space of pictures, pictures may be compressed during build. If you don't want to be compressed, you can put the pictures in RES / raw / directory.

Examples given by SDK:

Note: keeping one to of each resource type can ensure the consistency of your project state, so you don't have to worry about many different types of objects to instantiate it. For example, if you use the same image resource to instantiate two drawable objects. Then modify the properties of a drawables (such as alpha), and unfortunately this effect will also appear on another object. Therefore, when processing multiple instance objects of the same resource, instead of directly converting to drawable, you should execute tweet animation

How to add resources to ImageView:

Create from XML file

By now, you should be familiar with developing a user interface according to the principles of Android. Therefore, you should also understand the importance of defining an XML file for the function and flexibility of objects. This concept has been used countless times in drawables

If you want to create a drawable object that does not depend on variables or user exchanges, it should be a good way 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 under your project directory, then retrieve and instantiate it by calling resource. Getdrawable(), and pass it the resource ID number in the XML file. Any subclass of drawable supports the inflate method, which will instantiate your program through XML. Any drawable supports the extension of XML to use special XML attributes to help define the attributes of objects. You can view any drawable subclass document to see how to define XML files.

A transitiondrawable is defined as follows: 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 needed < item >. For more information about transitiondrawable, see http://androidappdocs.appspot.com/ref erence/android/graphics/drawable/TransitionDrawable.html。

Define it in RES / drawable / expand_ collapse.xml:

The following instantiates and processes:

When you want to draw some dynamic two-dimensional pictures, a shapedrawable object may be of great help to you. Through shapedrawable, you can draw any image and style you think of through programming.

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

Shapedrawable class (like many other drawable types in android.graphics.drawable package) allows you to define various properties of drawable public methods. Some properties you can adjust as needed, including transparency, color filtering, opacity, color. NinePatchDrawable

Ninepatchdrawable draws a scalable bitmap image, and Android will automatically resize to accommodate the displayed content. An example is the ninepatch background, which uses standard Android buttons. The buttons must be retracted to accommodate characters with varying length

Ninepatchdrawable is a standard PNG image, which includes an additional 1 pixel boundary. You must save it with the suffix. 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 the image. You can draw one or more black pixels on the left and upper lines to indicate the scalable part (you can need many scalable parts). Its relative position is the same in the scalable part, so the large part is always large.

You can also draw an optional drawable area (effective, inner margin line) 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 will expand and contract itself to make all text adapt to the area designed according to the right line and bottom line (if any). Of course, the inner margin line is not included. Android can use the left line and the upper line to define a drawable area.

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

I hope this article will be helpful to your Android program design.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>