Android game development Tour 4 canvas and paint instances

Source: Internet
Author: User

Yesterday we mentioned the ondraw method in the details of the three views on the android game development journey. For detailed implementation, we will mainly talk about the use of canvas and painting objects for Android today.

The canvas class mainly implements the Screen Painting Process, including many practical methods, such as drawing a path, area, texture, painting point, draw line, and rendering text, the following are common methods of the canvas class. Of course, the android Development Network prompts that many methods have different overloaded versions, and the parameters are more flexible.

Void drawrect (rectf rect, paint) // draw a region. Parameter 1 is a rectf region.

Void drawpath (Path, paint) // draw a path. Parameter 1 is the path object.

 

 Void drawbitmap (Bitmap bitmap, rect SRC, rect DST, paint) // texture. Parameter 1 is our regular bitmap object, the second parameter is the source area (which is bitmap as prompted by android123), the third parameter is the target area (which should be in the canvas location and size), and the fourth parameter is the paint brush object, due to the possibility of scaling and stretching, the performance will be greatly compromised when the original rect is not equal to the target rect. 

 

Void drawline (float startx, float starty, float stopx, float stopy, paint) // draw a line. The parameter is the X axis position of the starting point, and the Y axis position of the two starting points, the X axis horizontal position at the end of the three parameters, the four Y axis vertical position, and the last parameter is the paint brush object.

Void drawpoint (float X, float y, paint) // specifies a painting point. The parameter is a horizontal X axis, the parameter is a vertical Y axis, and the third parameter is a paint object.
 
Void drawtext (string text, float X, float y, paint) // render the text. In addition to the above, the canvas class can also depict the text. The first parameter is string-type text, the parameter has two x axes, three y axes, and four is the paint object.

Void drawtextonpath (string text, Path, float hoffset, float voffset, paint) // draw text in the path, relative to the second parameter above, which is the path object

From the above, we can see that the canvas painting class is relatively simple and flexible, and there is usually no problem in implementing general methods. At the same time, we can design some effects by adding operations, however, careful netizens may find that the last parameter is a paint object. If we regard canvas as a painter, painting is our painting tool, such as paint brush, paint brush, paint and so on.

Common Methods of Painting:

Void setargb (int A, int R, int g, int B) sets the paint object color. Parameter 1 is the Alpha transparent channel.

Void setalpha (int A) sets the Alpha opacity in the range of 0 ~ 255

Void setantialias (Boolean aa) // specifies whether the image is anti-aliasing.

Void setcolor (INT color) // sets the color. Here, the color class defined in Android contains some common color definitions.
.
Void setfakeboldtext (Boolean fakeboldtext) // set the pseudo-bold text

Void setlineartext (Boolean lineartext) // sets the linear text.
 
Patheffect setpatheffect (patheffect effect) // sets the path effect
 
Rasterizer setrasterizer (Rasterizer) // sets the grating.
 
Shader setshader (shader) // sets the shadow.

Void settextalign (paint. Align align) // sets text alignment.

Void settextscalex (float scalex) // sets the text zoom factor. 1.0f is the original

Void settextsize (float textsize) // you can specify the font size.
 
Typeface settypeface (typeface) // sets the font. typeface includes the font type, width, skew, and color.

Void setunderlinetext (Boolean underlinetext) // set the underline
 
In the end, canvas and paint are directly used in ondraw.

@ Override
Protected void ondraw (canvas ){

Paint paintred = new paint ();

Paintred. setcolor (color. Red );

Canvas. drawpoint (11,3, paintred); // draw a red dot on coordinates 11,3
}

Turn: http://java-admin.javaeye.com/blog/722364

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.