• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Basic Drawing Effects (C/C++)
2
3
4## When to Use
5
6During drawing, you can set basic effects, such as the fill color, anti-aliasing, outline, and line connection style.
7
8Set the basic filling effect by using the brush, and set the basic stroke effect by using the pen.
9
10
11## Filling Effects
12
13You can use the brush to set the basic fill color, or use the blending mode, shader effect, and filter effect to implement more complex drawing effects. For details, see [Complex Drawing Effects](complex-drawing-effect-c.md).
14
15
16### Available APIs
17
18The following table lists the common APIs for setting the drawing effect by using the brush. For details about the usage and parameters, see [drawing_brush](../reference/apis-arkgraphics2d/drawing__brush_8h.md).
19
20| Interface| Description|
21| -------- | -------- |
22| OH_Drawing_Brush\* OH_Drawing_BrushCreate (void) | Creates an **OH_Drawing_Brush** object.|
23| void OH_Drawing_CanvasAttachBrush (OH_Drawing_Canvas\*, const OH_Drawing_Brush\*) | Attaches a brush to a canvas so that the canvas can use the style and color of the brush to fill in a shape.|
24| void OH_Drawing_BrushSetColor (OH_Drawing_Brush\* , uint32_t color) | Sets the color for a brush. The color is used by the brush to fill in a shape.|
25| void OH_Drawing_BrushSetAntiAlias (OH_Drawing_Brush\* , bool) | Sets the anti-aliasing attribute of the brush. If this attribute is set to true, the brush performs translucent blur processing on the edge pixels of the image when drawing the image to make the image edges smoother.|
26| void OH_Drawing_CanvasDetachBrush (OH_Drawing_Canvas\*) | Removes the brush from the canvas. After this operation is performed, the canvas does not use the previously set brush and restores the default filling effect.|
27| void OH_Drawing_BrushDestroy (OH_Drawing_Brush\*) | Destroys an **OH_Drawing_Brush** object and reclaims the memory occupied by the object.|
28
29
30### How to Develop
31
321. Use the OH_Drawing_BrushCreate() API to create a brush object.
33
34   ```c++
35   OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
36   ```
37
382. Use the brush to set the basic drawing effect. (You can select one or more of the following effects.)
39
40   - You can use the OH_Drawing_BrushSetColor() interface to set the fill color.
41
42      ```c++
43      uint32_t color = 0xffff0000;
44      OH_Drawing_BrushSetColor(brush, color);
45      ```
46
47      color is a 32-bit (ARGB) variable, for example, 0xffff0000.
48
49   - You can call OH_Drawing_BrushSetAntiAlias() to enable the anti-aliasing effect to smooth the image edges.
50      ```c++
51      OH_Drawing_BrushSetAntiAlias(brush, true);
52      ```
53
543. Use the OH_Drawing_CanvasAttachBrush() interface to set the brush for the canvas. The interface accepts two parameters. One is the canvas object Canvas. Ensure that the canvas has been created or obtained. For details, see [Obtaining a Canvas and Displaying Drawing Results (C/C++)](canvas-get-result-draw-c.md). The other is the brush object to be set. The canvas will be filled with the configured brush style and color.
55
56   ```c++
57   OH_Drawing_CanvasAttachBrush(canvas, brush);
58   ```
59
604. Draw diagram elements as required. For details, see [Primitive Drawing](primitive-drawing-overview.md).
61
625. If the padding effect is not required, you can use OH_Drawing_CanvasDetachBrush() to remove it. The input parameter is the canvas object Canvas.
63
64   ```c++
65   OH_Drawing_CanvasDetachBrush(canvas);
66   ```
67
686. If the brush is not required for effect filling, call OH_Drawing_BrushDestroy() to destroy the Brush object in a timely manner.
69
70   ```c++
71   OH_Drawing_BrushDestroy(brush);
72   ```
73
74
75## Stroke Effects
76
77You can use the pen to set the basic stroke color, or use the blending mode, path effect, shader effect, and filter effect to implement more complex drawing effects. For details, see [Complex Drawing Effects](complex-drawing-effect-c.md).
78
79
80### Available APIs
81
82The following table lists the common APIs for setting the drawing effect using the pen. For details about the usage and parameters, see [drawing_pen](../reference/apis-arkgraphics2d/drawing__pen_8h.md).
83
84| Interface| Description|
85| -------- | -------- |
86| OH_Drawing_Pen\* OH_Drawing_PenCreate (void) | Creates an **OH_Drawing_Pen** object.|
87| void OH_Drawing_CanvasAttachPen (OH_Drawing_Canvas\* , const OH_Drawing_Pen\* ) | Attaches a pen to a canvas so that the canvas can use the style and color of the pen to outline a shape.|
88| void OH_Drawing_PenSetColor (OH_Drawing_Pen\* , uint32_t color) | Sets the color for a pen. The color is used by the pen to outline a shape.|
89| void OH_Drawing_PenSetWidth (OH_Drawing_Pen\* , float width) | Sets the width for a pen. The value **0** is treated as an unusually thin width. During drawing, the width of 0 is always drawn as 1 pixel wide, regardless of any scaling applied to the canvas. Negative values are also regarded as the value **0** during the drawing process.|
90| void OH_Drawing_PenSetAntiAlias (OH_Drawing_Pen\* , bool ) | Enables or disables anti-aliasing for a pen. Anti-aliasing makes the pixels around the shape edges semi-transparent.|
91| void OH_Drawing_PenSetCap (OH_Drawing_Pen\* , OH_Drawing_PenLineCapStyle) | Sets the line cap style for a pen.|
92| void OH_Drawing_PenSetJoin (OH_Drawing_Pen\* , OH_Drawing_PenLineJoinStyle) | Sets the line join style for a pen.|
93| void OH_Drawing_CanvasDetachPen (OH_Drawing_Canvas\*) | Removes the pen from the canvas. After this operation is performed, the canvas does not draw the outline of the shape and restores the default filling effect.|
94| void OH_Drawing_PenDestroy (OH_Drawing_Pen\*) | Destroys an **OH_Drawing_Pen** object and reclaims the memory occupied by the object.|
95
96
97### How to Develop
98
991. Use the OH_Drawing_PenCreate() interface to create a pen object.
100
101   ```c++
102   OH_Drawing_Pen* pen = OH_Drawing_PenCreate();
103   ```
104
1052. Use the OH_Drawing_CanvasAttachPen() interface to set the pen for the Canvas. The interface accepts two parameters. One is the canvas object Canvas. Ensure that the canvas has been created or obtained. For details, see [Obtaining a Canvas and Displaying Drawing Results (C/C++)](canvas-get-result-draw-c.md). The other is the pen object to be set. The canvas will use the configured pen style and color to draw the outline of the graph.
106
107   ```c++
108   OH_Drawing_CanvasAttachPen(canvas, pen);
109   ```
110
1113. Use the pen to set one or more of the following stroke effects.
112
113   - You can use the OH_Drawing_PenSetColor() interface to set the pen color, which is used for drawing the outline of a graph.
114      ```c++
115      uint32_t color = 0xffff0000;
116      OH_Drawing_PenSetColor(pen, color);
117      ```
118
119      color is a 32-bit (ARGB) variable, for example, 0xffff0000.
120
121   - You can call the OH_Drawing_PenSetWidth() interface to set the line width of the pen.
122
123      ```c++
124      OH_Drawing_PenSetWidth(pen, width);
125      ```
126
127      width indicates the pixel value of the line width.
128
129   - You can call OH_Drawing_PenSetAntiAlias() to set the anti-aliasing function of the pen to make the drawing edges smoother.
130      ```c++
131      OH_Drawing_PenSetAntiAlias(pen, true);
132      ```
133
134   - You can use the OH_Drawing_PenSetCap() interface to set the style of the pen line cap.
135
136      ```c++
137      OH_Drawing_PenSetCap(pen, OH_Drawing_PenLineCapStyle);
138      ```
139
140      OH_Drawing_PenLineCapStyle: The following lists the available cap styles.
141
142      | Wire cap style| Description| Diagram|
143      | -------- | -------- | -------- |
144      | FLAT_CAP | There is no cap style. Both ends of the line segment are cut off square.| ![Screenshot_20241130143725824](figures/Screenshot_20241130143725824.jpg) |
145      | SQUARE_CAP | Square cap style. Both ends have a square, the height of which is half of the width of the line segment, with the same width.| ![Screenshot_20241130143837975](figures/Screenshot_20241130143837975.jpg) |
146      | ROUND_CAP | Round cap style. Both ends have a semicircle centered, the diameter of which is the same as the width of the line segment.| ![Screenshot_20241130143949934](figures/Screenshot_20241130143949934.jpg) |
147
148   - You can call the OH_Drawing_PenSetJoin() API to set the pen corner style.
149
150      ```c++
151      OH_Drawing_PenSetJoin(pen, OH_Drawing_PenLineJoinStyle);
152      ```
153
154      The options of OH_Drawing_PenLineJoinStyle are as follows:
155      | Corner style| Description| Diagram|
156      | -------- | -------- | -------- |
157      | MITER_JOIN | The corner type is sharp corner.| ![image_0000002194025261](figures/image_0000002194025261.png) |
158      | ROUND_JOIN | Round corner.| ![image_0000002194110901](figures/image_0000002194110901.png) |
159      | BEVEL_JOIN | Beveled corner.| ![image_0000002158744158](figures/image_0000002158744158.png) |
160
1614. Draw diagram elements as required. For details, see [Primitive Drawing](primitive-drawing-overview.md).
162
1635. If the stroke effect is not required, you can use OH_Drawing_CanvasDetachPen() to remove it. The input parameter is the canvas object. Ensure that the canvas has been created or obtained. For details, see [Obtaining a Canvas and Displaying Drawing Results (C/C++)](canvas-get-result-draw-c.md).
164
165   ```c++
166   OH_Drawing_CanvasDetachPen(canvas);
167   ```
168
1696. If the stroke is not required, call OH_Drawing_PenDestroy() to destroy the Pen object in a timely manner.
170
171   ```c++
172   OH_Drawing_PenDestroy(pen);
173   ```
174