Canvas path painting (required for Games)

Source: Internet
Author: User

CanvasClass mainly implements the Screen Painting Process, including many practical methods, such as drawing a path, area, texture, painting point, draw line, 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. The parameter is set to rectf.

 Void drawpath (Path, paint)// Draw a path with parameter 1 as the path object

 Void drawbitmap (Bitmap bitmap, rect SRC, rect DST, paint)// Texture. The parameter 1 is our regular bitmap object, and the parameter 2 is the source area (android123 indicates Bitmap ), parameter 3 is the target area (the position and size of the canvas should be used), and parameter 4 is the paint brush object, because the possibility of scaling and stretching is used, when the original rect is not equal to the target rect, the performance will be greatly reduced.

 Void drawline (float startx, float starty, float stopx, float stopy, paint)// Draw a line. The X-axis position of the starting point of the parameter, Y-axis position of the starting point of the parameter, X-axis horizontal position of the three ending points of the parameter, Y-axis vertical position of the parameter, and the last parameter is the paint brush object.

Void drawpoint (float X, float y, paint)// Painting point, parameter 1 Horizontal X axis, parameter 2 vertical Y axis, and third parameter is the paint object.
 
Void drawtext (string text, float X, float y, paint)// Render text. In addition to the above description, the canvas class can also depict text. Parameter 1 is string text, parameter 2 x axis, parameter 3 Y axis, and parameter 4 is the paint object.

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

Slave
From the above, we can see that the canvas painting class is relatively simple and flexible, and there is usually no problem in implementing the general method. At the same time, we can design some effects through superposition, but careful netizens may find the most
The next parameter is a paint object. If we regard canvas as a painter, painting is our painting tool, such as paint brush, paint brush, paint, etc.

PaintCommon Methods:

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.

[Java]View plaincopyprint?
  1. <Span style = "font-size: 16px;"> @ override
  2. Protected void ondraw (canvas ){
  3. Paint paintred = new paint ();
  4. Paintred. setcolor (color. Red );
  5. Canvas. drawpoint (11,3, paintred); // draw a red dot on coordinates 11,3
  6. } </Span>

 

For android game development or 2D drawing, the path can be described as powerful. In Photoshop, we may still remember how to use the pen tool to draw paths. The path class is located in Android. Graphics. Path,PathThe constructor method is relatively simple, as shown below:

Path cwj = New Path (); // Constructor

Next we will draw a closed prototype path. We use the addcircle method of the path class.

Cwj. addcircle (10, 10, 50, direction. CW); // parameter 1 is the horizontal position of the X axis, parameter 2 is the vertical position of the Y axis, the third parameter is the circle radius, and finally the direction of the drawing, Cw is the clockwise direction, the CCW direction is counter-clockwise.

Combined with the draw Methods drawpath and drawtextonpath in the canvas class mentioned by Android last time, we can continue to add them to ondraw.

Canvas. drawpath (cwj, paintpath); // android123 reminds you that paintpath is the paint color of the path. See the complete source code below.

Canvas. drawtextonpath ("android123-cwj", cwj, painttext); // draw the text to the path. The drawtextonpath parameters are as follows:

Public void drawtextonpath (string text, Path, float hoffset, float voffset, painting)
Parameter List:

Text is the text content to be drawn in the path.

Path: the path to which the text is drawn.

Hoffset distance from the path
Voffset is the upper and lower heights of the path. Here, the android Development Network prompts you that the parameter type is float floating point. It can be positive or negative except for the precision of 8 decimal places, when the positive text is in the path circle, if it is negative, it is outside the path circle.
Painting is still a painting object used to specify the color, Font, size, and other attributes of the text.

The Demo code for how to draw a path in our ondraw method is as follows:

[Java]View plaincopyprint?
  1. <Span style = "font-size: 16px;"> @ override
  2. Protected void ondraw (canvas ){
  3. Paint paintpath = new paint ();
  4. Paint painttext = new paint ();
  5. Paintpath. setcolor (color. Red); // The path is painted in red.
  6. Painttext. setcolor (color. Blue); // The text in the path is blue.
  7. Path pathcwj = New Path ();
  8. Pathcwj. addcircle (10, 10, 50, direction. CW); // The radius is 50px and the CW direction is clockwise.
  9. Canvas. drawpath (pathcwj, paintpath );
  10. Canvas. drawtextonpath ("android123-cwj", pathcwj, painttext); // draw text on the path
  11. } </Span>

Common Methods for path classes are as follows:

Void addarc (rectf oval, float startangle, float sweepangle) // Add a polygon to the path
 
Void addcircle (float X, float y, float radius, path. Direction DIR) // Add a circle to the path
 
Void addoval (rectf oval, path. Direction DIR) // Add an elliptical shape.

Void addrect (rectf rect, path. Direction DIR) // Add a region

Void addroundrect (rectf rect, float [] radii, path. Direction DIR) // Add a rounded Area
 
Boolean isempty () // determines whether the path is empty.

Void transform (matrix) // apply matrix transformation
 
Void transform (matrix, path DST) // apply the matrix transformation and place the result in the new path, that is, the second parameter.

You can use the patheffect class for advanced effects of paths.

 

[Java]View plaincopyprint?
    1. /**
    2. * Paint class Introduction
    3. *
    4. * Paint is the paint brush, which plays an extremely important role in the drawing process. The paint brush mainly stores the color,
    5. * Style and other painting information, specifying how to draw text and graphics, there are many ways to set the paint brush object,
    6. * Generally, there are two types: one is related to drawing, and the other is related to text drawing.
    7. *
    8. * 1. Drawing
    9. * Setargb (int A, int R, int g, int B );
    10. * Set the color of the painting. A indicates transparency, R, G, and B indicates the color value.
    11. *
    12. * Setalpha (int );
    13. * Set the transparency of the drawing.
    14. *
    15. * Setcolor (INT color );
    16. * Set the color to be drawn, which is represented by a color value. The color value includes transparency and RGB color.
    17. *
    18. * Setantialias (Boolean aa );
    19. * Setting whether to use the anti-aliasing function consumes a large amount of resources and slows down the drawing speed.
    20. *
    21. * Setdither (Boolean dither );
    22. * Setting whether to use image jitter processing will make the color of the drawn image smoother and fuller, and the image clearer
    23. *
    24. * Setfilterbitmap (Boolean filter );
    25. * If this parameter is set to true, the bitmap image is filtered out during the animation to accelerate the display.
    26. * Speed. This setting item depends on dither and xfermode settings.
    27. *
    28. * Setmaskfilter (maskfilter );
    29. * Set maskfilter. Different maskfilters can be used to implement filter effects, such as filtering and stereo *
    30. * Setcolorfilter (colorfilter );
    31. * Set the color filter to achieve the effect of changing colors that are not needed when drawing colors.
    32. *
    33. * Setpatheffect (patheffect effect );
    34. * Set the effect of the draw path, such as drawing a line.
    35. *
    36. * Setshader (shader );
    37. * Set the image effect. You can use shader to draw different gradient effects.
    38. *
    39. * Setshadowlayer (float radius, float dx, float dy, int color );
    40. * Set the shadow layer under the image to produce the shadow effect. radius indicates the shadow angle. dx and Dy indicate the distance between the shadow on the X and Y axes, and color indicates the shadow color.
    41. *
    42. * Setstyle (paint. Style );
    43. * Set the paint brush style to fill, fill_or_stroke, or stroke.
    44. *
    45. * Setstrokecap (paint. Cap CAP );
    46. * When the paint brush style is stroke or fill_or_stroke, set the image style of the brush, such as the circle style.
    47. * Cap. round, or square style cap. Square
    48. *
    49. * Setsrokejoin (paint. Join join );
    50. * Set the combination of various images during painting, such as smooth effects.
    51. *
    52. * Setstrokewidth (float width );
    53. * When the paint brush style is stroke or fill_or_stroke, set the width of the paint brush.
    54. *
    55. * Setxfermode (xfermode );
    56. * Sets the processing method for overlapping images, such as merge, intersection, or union. It is often used to make the eraser erasure effect.
    57. *
    58. * 2. Text Rendering
    59. * Setfakeboldtext (Boolean fakeboldtext );
    60. * Simulate bold text and set it to a small font.
    61. *
    62. * Setsubpixeltext (Boolean subpixeltext );
    63. * Setting this parameter to true will help display the text on the LCD screen.
    64. *
    65. * Settextalign (paint. Align align );
    66. * Set the alignment direction of the drawn text
    67. *
    68. * Settextscalex (float scalex );
    69. * You can set the zoom ratio on the X axis of the drawn text to stretch the text.
    70. *
    71. * Settextsize (float textsize );
    72. * Set the font size of the drawn text.
    73. *
    74. * Settextskewx (float skewx );
    75. * Set italic text. skewx is a skewed radian.
    76. *
    77. * Settypeface (typeface );
    78. * Set the typeface object, that is, the font style, including bold, italic, and linefeed, non-linefeed, etc.
    79. *
    80. * Setunderlinetext (Boolean underlinetext );
    81. * Set the underlined text effect.
    82. *
    83. * Setstrikethrutext (Boolean strikethrutext );
    84. * Set the effect of strikethrough.
    85. *
    86. */

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.