Typical classes: Introduction to common classes related to Android UI

Source: Internet
Author: User
I. Canvas Android. Graphics. Canvas

The canvas class is like drawing paper on a mobile phone. We can draw images or images on a canvas. Generally, when we use Android for painting, four components are required:
1. Bitmap: Contains pixels
2. Canvas canvas: contains the painting content and writes it to a bitmap.
3. Initial graphics: such as rect, bitmap, and text
4. Paint: used to describe the color and type of the above initial image.
The canvas class provides three constructor methods:
Public canvas (); construct a canvas object without parameters by default.
Public canvas (Bitmap bitmap); constructs a canvas object based on a bitmap.
Public canvas (GL); construct an object based on a GL
The following describes the methods provided by the canvas class:
Among the methods provided by the canvas class, many commonly used methods start with draw. the method that starts with draw is easy to understand, that is, drawing a graph into the canvas. For example, you can draw a bitmap to the canvas, fill the image with colors.


Ii. Paint Android. Graphics. Paint

The paint class contains information that can be used to draw ry, text, bitmap types and colors. If you think of the canvas class as a canvas, you can think of the paint class as a paint brush, you can draw images and texts of different colors and styles as needed.
The paint class has three constructor methods:
Public paint () constructs a default paint object
Public
Paint (INT flags); construct a paint object based on the specified flags.
Setflags () method to change
Public paint (paint) constructs a paint object based on the specified paint object.
The paint class provides many methods to set and obtain attributes of a paint object. For example:
Public int getcolor () Get the color value of the paint object
Public colorfilter getcolorfilter () to obtain the Color Filter
Public float gettextsize () to obtain the font size value
Public void setstyle (paint. Style) sets the paint type.

Android. Graphics. Color

This class defines some methods to create and convert color values. The color is expressed as an encapsulated value. The value consists of four bytes: Alpha, red, green, and blue. These values are not left-multiplied, that is to say, any transparency is only stored in the Alpha part, not in the color components. Each part is saved in the following sequence: (alpha <24) | (Red <16) | <green <8) | blue. the value range of each part is between 0 and 255. 0 indicates that this part does not work, and 100% indicates that takes effect. Therefore, the opaque black should be 0xff000000, And the opaque white should be 0 xffffffff.
The color class provides 12 constant values to represent different color values. In the development project, we can directly call these constant values to set the text, graphics, and other objects we want to modify.
Color class provides a construction method without parameters color ()
The color class provides some methods to create and convert color values as follows:
The three methods are used to return the red, green, and blue color of a constant value. The values are between 0 and, respectively, as shown below:
Public static red (INT color );
Public static Green (INT color );
Public static blue (INT color );
Public static int RGB (INT red, int green, int blue); input red, green, and blue to return an RGB color value.
You can refer to the android API for the following methods:
Public static int HSV tocolor (INT Alpha, float [] HSV );
Public static int HSV tocolor (float [] HSV );
Public static void rgbtohsv (INT red, int green, int blue, float [] HSV );
Public static int alpha (INT color );
Public static int argb (INT Alpha, int red, int green, int blue );
Public static colortohsv (INT color, float [] HSV );
Public static parsecolor (string colorstring );

Iv. typeface Android. Graphics. typeface

The typeface class defines the internal types of fonts and fonts. This class is used when the paint brush is set. For example, textsize, textskewx, and textscale are used to specify how text is displayed and measured during painting.

Typeface provides some constant values to represent some of its own attributes, such as bold, bold_italic, and italic. developers can use defaultfrophone sdntyle (INT style) to obtain the inherent typeface object. Readers can refer to the detailed API documentation and will not go into details here.

Next, let's take a look at the main methods of typeface.

The typeface class has no constructor. Generally, if a class has no constructor, it cannot be constructed to generate an object. In this case, a specific static method is usually used to obtain the object of this class. Typeface provides four static methods to indirectly obtain typeface objects:

Public static typeface create (typeface family, int style );

This method returns a typeface that matches an existing typeface System and the type is of the specified type. This method can be called if you want to obtain a typeface system that is similar to an existing one, but has different styles. The family parameter can be null. If it is null, the family parameter is selected from the default typeface system.

Public static typefaxe create (string familyname, int style );

This method returns a typeface object by using the name familyname and the specified type of the given glyph system. If the name of the given font system is null, we can use the getstyle () method to obtain the true attributes of the returned typeface object.

Public static typeface createfromasset (assetmanager Mgr, string path );

This method returns a typeface object by specifying the font data. The first parameter is the resource manager, and the second parameter is the specified font type.

Public static typeface defaultfrophone sdntyle (INT style); this method returns a typeface object of the specified type.

Typeface also provides the following three methods:

Public int getstyle (); this method returns the type attribute inherent in typeface.

Public final Boolean isbold (); returns true if getstyle () has a bold Group

Public final Boolean isitalic (); returns true if getstyle () has an italic bit Group

5. Path Android. Graphics. Path

Path class (a group of regions), including a variety of geometric figures such as line segments, quadratic curves, cubic curves, etc,

The canvas. drawpath () method can be called to draw the path to the drawing board or to fill out the drawing in the defined paint method, or to draw the image in the method specified by the paint.

6. rectf Android. Graphics. rectf and rect Android. Graphics. rect

The rectf class contains four single-precision floating point coordinates of a rectangle. A rectangle is represented by the coordinates of the top, bottom, and left sides of the rectangle. These coordinate value attributes can be directly accessed, and the width and height of the rectangle can be obtained using the width () and height () methods. Note: Most methods do not check whether these coordinate classifications are incorrect (that is, left <= right and top <= bottom ).

Rectf has four constructor methods:

Rectf () is used to construct a rectangle without parameters.

Rectf (float left, float top, float right, float bottom) constructs a rectangle with four parameters specified

Rectf (rect f r) constructs a rectf object based on the specified rectf object (the left coordinate of the object remains unchanged)

Rectf (rect R) constructs a rectf object based on the given rect object.

Rectf provides many methods. The following describes several methods:

Public Boolean contain (rectf R); determines whether a rectangle is in this rectangle. If it is within or equal to this rectangle, true is returned, similar methods include public Boolean contain (float left, float top, float right, float bottom) and Public Boolean contain (float X, float Y ).

Public void Union (float X, float y) updates the rectangle so that it contains the vertex of the rectangle itself and (x, y.

The methods provided by the rectf class are simple and easy to understand. I will not repeat them here.

Android. graphics. rect class, which is similar to Android. graphics. rectf is very similar. The difference is that the coordinates of the rect class are represented by an integer, while the coordinates of the rectf are represented by a single precision Floating Point. You must pay attention to it here.

7. Point Android. Graphics. Point

This class can be seen from the literal meaning that it is related to vertices and is an object class of vertices.

This class has two attributes: X coordinate and Y coordinate.

There are three constructors: Point (), point (int x, int y), point (point P)

The main methods are as follows:

Public void set (x, y); reset the coordinates of X and Y.

Public final void offset (INT dx, int Dy); returns a compensation value for the coordinates. The value can be positive or negative.

Public final void negate (); negative coordinate value.

The Point class is similar to Android. Graphics. pointf. The difference is that the former coordinate value type is int type, while the latter coordinate value is float type. In addition, the pointf class adds several more methods, such:

Public final float length (); returns the distance from (0, 0) to this point.

Public static float length (float X, float y); returns the distance from (0, 0) to (x, y.

E is not good. You can check it later.

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.