You can set some basic effects, such as the fill color, anti-aliasing, stroke, and line connection style.
You can set the basic fill effect using a brush and set the basic stroke effect using a pen.
You can set the basic fill color using a brush and use the brush to implement more complex drawing effects, such as the blend mode, shader effect, and filter effect. For details, see Complex Drawing Effects.
The following table lists the common APIs for setting drawing effects with a brush. For details about the usage and parameters, see drawing_brush and drawing_canvas.
| API | Description |
|---|---|
| OH_Drawing_Brush* OH_Drawing_BrushCreate(void) | Creates a brush object. |
| void OH_Drawing_CanvasAttachBrush(OH_Drawing_Canvas* canvas, const OH_Drawing_Brush* 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. |
| void OH_Drawing_BrushSetColor(OH_Drawing_Brush* brush, uint32_t color) | Sets the color for a brush. The color is used by the brush to fill in a shape and is represented by a 32-bit (ARGB) variable. |
| void OH_Drawing_BrushSetAntiAlias(OH_Drawing_Brush* brush, bool antiAlias) | Sets the anti-aliasing property for a brush. If set to true, the brush applies semi-transparent blurring to the edge pixels of a shape when drawing, making the shape edges smoother. |
| void OH_Drawing_CanvasDetachBrush(OH_Drawing_Canvas* canvas) | Removes the brush from the canvas. After the execution, the canvas does not use the previously set brush and is restored to the default fill effect. |
| void OH_Drawing_BrushDestroy(OH_Drawing_Brush* brush) | Destroys a brush object and reclaims the memory occupied by it. |
Call OH_Drawing_BrushCreate() to create a brush object.
// Create a brush object. OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
(Optional) Use the brush to set the basic drawing effect. You can select one or more of the following effects:
You can call OH_Drawing_BrushSetColor() to set the fill color.
// Set the fill color to red. uint32_t color = 0xffff0000; OH_Drawing_BrushSetColor(brush, color);
color is a 32-bit (ARGB) variable, for example, 0xffff0000.
You can call OH_Drawing_BrushSetAntiAlias() to enable the anti-aliasing effect to smooth the edges of the graphics.
// Enable the anti-aliasing effect. OH_Drawing_BrushSetAntiAlias(brush, true);
Call OH_Drawing_CanvasAttachBrush() to set a brush for the canvas. This API takes two parameters: (1) Canvas object, which must be created or obtained. For details, see Obtaining a Canvas and Displaying Drawing Results (C/C++); (2) brush object to be set. The canvas uses the brush style and color to fill the shape.
// Set the brush of the canvas. OH_Drawing_CanvasAttachBrush(canvas, brush);
Draw primitives as required. For details, see Drawing Primitives.
If no fill effect is required, you can use OH_Drawing_CanvasDetachBrush() to remove the brush. The input parameter is the Canvas object.
// Remove the brush from the canvas. OH_Drawing_CanvasDetachBrush(canvas);
When the brush is no longer needed, destroy the brush object using OH_Drawing_BrushDestroy().
// Destroy objects. OH_Drawing_BrushDestroy(brush);
You can set the basic stroke color using a pen and use the pen to implement more complex drawing effects, such as the blend mode, path effect, shader effect, and filter effect. For details, see Complex Drawing Effects.
The following table lists the common APIs for setting drawing effects with a pen. For details about the usage and parameters, see drawing_pen and drawing_canvas.
| API | Description |
|---|---|
| OH_Drawing_Pen* OH_Drawing_PenCreate(void) | Creates a pen object. |
| void OH_Drawing_CanvasAttachPen(OH_Drawing_Canvas* canvas, const OH_Drawing_Pen* pen) | Attaches a pen to a canvas so that the canvas can use the style and color of the pen to outline a shape. |
| void OH_Drawing_PenSetColor(OH_Drawing_Pen* pen, uint32_t color) | Sets the color for a pen. The color is used by the pen to outline a shape. |
| void OH_Drawing_PenSetWidth(OH_Drawing_Pen* 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. |
| void OH_Drawing_PenSetAntiAlias(OH_Drawing_Pen* pen, bool antiAlias) | Enables or disables anti-aliasing for a pen. Anti-aliasing makes the pixels around the shape edges semi-transparent. |
| void OH_Drawing_PenSetCap(OH_Drawing_Pen* pen, OH_Drawing_PenLineCapStyle capStyle) | Sets the line cap style for a pen. |
| void OH_Drawing_PenSetJoin(OH_Drawing_Pen* pen, OH_Drawing_PenLineJoinStyle joinStyle) | Sets the line join style for a pen. |
| void OH_Drawing_CanvasDetachPen(OH_Drawing_Canvas* canvas) | Removes the pen from the canvas. After this operation, the canvas no longer draws the outline of the graphics shape and restores the default stroke effect. |
| void OH_Drawing_PenDestroy(OH_Drawing_Pen* pen) | Destroys a pen object and reclaims the memory occupied by the object. |
Call OH_Drawing_PenCreate() to create a pen object.
// Create a pen object. OH_Drawing_Pen* pen = OH_Drawing_PenCreate();
Use the pen to set specific stroke effects (you can choose one or more of the following effects).
You can call OH_Drawing_PenSetColor() to set the pen color, which is used to outline a shape.
// Set the pen color to red. uint32_t color = 0xffff0000; OH_Drawing_PenSetColor(pen, color);
color is a 32-bit (ARGB) variable, for example, 0xffff0000.
You can call OH_Drawing_PenSetWidth() to set the pen width.
// Set the pen width to 50 pixels. float width = 50.0; OH_Drawing_PenSetWidth(pen, width);
width indicates the pixel value of the pen width.
You can call OH_Drawing_PenSetAntiAlias() to set anti-aliasing for the pen to make the drawing edge smoother.
// Set anti-aliasing for the pen. OH_Drawing_PenSetAntiAlias(pen, true);
You can call OH_Drawing_PenSetCap() to set the pen cap style.
// Set the pen cap style. OH_Drawing_PenSetCap(pen, LINE_ROUND_CAP);
The options of OH_Drawing_PenLineCapStyle are as follows.
| Line Cap Style | Description | Effect |
|---|---|---|
| LINE_FLAT_CAP | No cap style. Both ends of the line segment are cut off squarely. | ![]() |
| LINE_SQUARE_CAP | Square cap style. Both ends have a square, the width of which is the same as the line segment, and the height is half of the line segment width. | ![]() |
| LINE_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. | ![]() |
You can call OH_Drawing_PenSetJoin() to set the pen join style.
// Set the pen join style. OH_Drawing_PenSetJoin(pen, LINE_BEVEL_JOIN);
The options of OH_Drawing_PenLineJoinStyle are as follows.
| Join Style | Description | Effect |
|---|---|---|
| LINE_MITER_JOIN | The line join style is a sharp corner. | ![]() |
| LINE_ROUND_JOIN | The line join style is a round corner. | ![]() |
| LINE_BEVEL_JOIN | The line join style is a flat corner. | ![]() |
Call OH_Drawing_CanvasAttachPen() to attach the pen to the canvas. This API accepts two parameters: one is the canvas object. Ensure that you have created or obtained the canvas. For details, see Obtaining a Canvas and Displaying Drawing Results (C/C++). The other parameter is the pen object to set. The canvas will use the configured pen style and color to draw graphics outlines.
// Attach the pen to the canvas. OH_Drawing_CanvasAttachPen(canvas, pen);
Draw primitives as required. For details, see Drawing Primitives.
If no stroke effect is required, you can use OH_Drawing_CanvasDetachPen() to remove the pen. 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++).
// Remove the pen from the canvas. OH_Drawing_CanvasDetachPen(canvas);
When the pen is no longer needed for stroke, destroy the pen object using OH_Drawing_PenDestroy().
// Destroy objects. OH_Drawing_PenDestroy(pen);