openharmony 鸿蒙 arkts-apis-graphics-drawing-Brush

2026-08-25 浏览 (1)

Class (Brush)

Defines a brush, which is used to describe the style and color to fill in a shape.

NOTE

  • The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • This module uses the physical pixel unit, px.

  • This module operates under a single-threaded model. The caller needs to manage thread safety and context state transitions.

Modules to Import

import { drawing } from '@kit.ArkGraphics2D';

constructor12+

constructor()

A constructor used to create a Brush object.

System capability: SystemCapability.Graphics.Drawing

Example

import { drawing } from '@kit.ArkGraphics2D';

const brush = new drawing.Brush();

constructor12+

constructor(brush: Brush)

Copies a Brush object to create a new one.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
brushBrushYesBrush object to copy.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.

Example

import { common2D, drawing } from '@kit.ArkGraphics2D';

const brush = new drawing.Brush();
const brushColor: common2D.Color = { alpha: 255, red: 0, green: 255, blue: 0 };
brush.setColor(brushColor);
const newBrush = new drawing.Brush(brush);

setColor

setColor(color: common2D.Color) : void

Sets a color for this brush.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
colorcommon2D.ColorYesColor in ARGB format. The value of each color channel is an integer ranging from 0 to 255.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed.

Example

import { common2D, drawing } from '@kit.ArkGraphics2D';

const color : common2D.Color = { alpha: 255, red: 255, green: 0, blue: 0 };
const brush = new drawing.Brush();
brush.setColor(color);

setColor12+

setColor(alpha: number, red: number, green: number, blue: number): void

Sets a color for this brush. This API provides better performance than setColor and is recommended.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
alphanumberYesAlpha channel value of the color in ARGB format. The value is an integer ranging from 0 to 255. Any passed-in floating point number is rounded down.
rednumberYesRed channel value of the color in ARGB format. The value is an integer ranging from 0 to 255. Any passed-in floating point number is rounded down.
greennumberYesGreen channel value of the color in ARGB format. The value is an integer ranging from 0 to 255. Any passed-in floating point number is rounded down.
bluenumberYesBlue channel value of the color in ARGB format. The value is an integer ranging from 0 to 255. Any passed-in floating point number is rounded down.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed.

Example

import { drawing } from '@kit.ArkGraphics2D';

const brush = new drawing.Brush();
brush.setColor(255, 255, 0, 0);

setColor18+

setColor(color: number) : void

Sets a color for this brush.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
colornumberYesColor in hexadecimal ARGB format.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed.

Example

import { drawing } from '@kit.ArkGraphics2D';

const brush = new drawing.Brush();
brush.setColor(0xffff0000);

setColor4f20+

setColor4f(color4f: common2D.Color4f, colorSpace: colorSpaceManager.ColorSpaceManager|null): void

Sets the color and standard color gamut for this brush. The difference between this method and setColor is that the color gamut can be set separately.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
color4fcommon2D.Color4fYesColor in the ARGB format. The value of each color channel is a floating point number ranging from 0.0 to 1.0. Values above 1.0 default to 1.0, and values below 0.0 default to 0.0.
colorSpacecolorSpaceManager.ColorSpaceManager |nullYesStandard color gamut object. null indicates SRGB.

Example

import { common2D, drawing, colorSpaceManager } from "@kit.ArkGraphics2D";

const brush = new drawing.Brush();
let colorSpace = colorSpaceManager.create(colorSpaceManager.ColorSpace.BT2020_HLG);
let color4f:common2D.Color4f = {alpha:1, red:0.5, green:0.4, blue:0.7};
brush.setColor4f(color4f, colorSpace);

getColor12+

getColor(): common2D.Color

Obtains the color of this brush.

System capability: SystemCapability.Graphics.Drawing

Returns

TypeDescription
common2D.ColorColor of the brush.

Example

import { common2D, drawing } from '@kit.ArkGraphics2D';

const color : common2D.Color = { alpha: 255, red: 255, green: 0, blue: 0 };
const brush = new drawing.Brush();
brush.setColor(color);
let colorGet = brush.getColor();

getColor4f20+

getColor4f(): common2D.Color4f

Obtains the brush color. The difference between this method and getColor is that this method returns a floating point number.

System capability: SystemCapability.Graphics.Drawing

Returns

TypeDescription
common2D.Color4fColor of the brush.

Example

import { common2D, drawing, colorSpaceManager } from "@kit.ArkGraphics2D";

const brush = new drawing.Brush();
let colorSpace = colorSpaceManager.create(colorSpaceManager.ColorSpace.BT2020_HLG);
let color4f:common2D.Color4f = {alpha:1, red:0.5, green:0.4, blue:0.7};
brush.setColor4f(color4f, colorSpace);
let color = brush.getColor4f();

getHexColor18+

getHexColor(): number

Obtains the color of this brush.

System capability: SystemCapability.Graphics.Drawing

Returns

TypeDescription
numberColor, represented as a 32-bit unsigned integer in hexadecimal ARGB format.

Example

import { common2D, drawing } from '@kit.ArkGraphics2D';

let color : common2D.Color = { alpha: 255, red: 255, green: 0, blue: 0 };
let brush = new drawing.Brush();
brush.setColor(color);
let hex_color: number = brush.getHexColor();
console.info('getHexColor: ', hex_color.toString(16));

setAntiAlias

setAntiAlias(aa: boolean) : void

Enables anti-aliasing for this brush. Anti-aliasing makes the edges of the content smoother. If this API is not called, anti-aliasing is disabled by default.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
aabooleanYesWhether to enable anti-aliasing. The value true means to enable anti-aliasing, and false means the opposite.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.

Example

import { drawing } from '@kit.ArkGraphics2D';

const brush = new drawing.Brush();
brush.setAntiAlias(true);

isAntiAlias12+

isAntiAlias(): boolean

Checks whether anti-aliasing is enabled for this brush.

System capability: SystemCapability.Graphics.Drawing

Returns

TypeDescription
booleanCheck result. The value true means that anti-aliasing is enabled, and false means the opposite.

Example

import { drawing } from '@kit.ArkGraphics2D';

const brush = new drawing.Brush();
let isAntiAlias = brush.isAntiAlias();

setAlpha

setAlpha(alpha: number) : void

Sets an alpha value for this brush.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
alphanumberYesAlpha value. The value is an integer in the range [0, 255]. If a floating point number is passed in, the value is rounded down.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed.

Example

import { drawing } from '@kit.ArkGraphics2D';

const brush = new drawing.Brush();
brush.setAlpha(128);

getAlpha12+

getAlpha(): number

Obtains the alpha value of this brush.

System capability: SystemCapability.Graphics.Drawing

Returns

TypeDescription
numberAlpha value of the brush. The return value is an integer ranging from 0 to 255.

Example

import { drawing } from '@kit.ArkGraphics2D';

const brush = new drawing.Brush();
let alpha = brush.getAlpha();

setColorFilter

setColorFilter(filter: ColorFilter|null) : void

Sets a color filter for this brush.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
filterColorFilter |nullYesDefines a color filter. If null is passed in, the color filter is cleared.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.

Example

import { drawing } from '@kit.ArkGraphics2D';

const brush = new drawing.Brush();
let colorFilter = drawing.ColorFilter.createLinearToSRGBGamma();
brush.setColorFilter(colorFilter);

setMaskFilter12+

setMaskFilter(filter: MaskFilter|null): void

Adds a mask filter for this brush.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
filterMaskFilter |nullYesMask filter. If null is passed in, the mask filter is cleared.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.

Example

import { RenderNode } from '@kit.ArkUI';
import { common2D, drawing } from '@kit.ArkGraphics2D';

class DrawingRenderNode extends RenderNode {
  draw(context : DrawContext) {
    const canvas = context.canvas;
    const brush = new drawing.Brush();
    let maskFilter = drawing.MaskFilter.createBlurMaskFilter(drawing.BlurType.OUTER, 10);
    brush.setMaskFilter(maskFilter);
  }
}

setShaderEffect12+

setShaderEffect(shaderEffect: ShaderEffect|null): void

Sets the shader effect for this brush.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
shaderEffectShaderEffect |nullYesShaderEffect object. If null is passed in, the shader effect will be cleared.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.

Example

import { drawing } from '@kit.ArkGraphics2D';

const brush = new drawing.Brush();
let shaderEffect = drawing.ShaderEffect.createLinearGradient({x: 100, y: 100}, {x: 300, y: 300}, [0xFF00FF00, 0xFFFF0000], drawing.TileMode.REPEAT);
brush.setShaderEffect(shaderEffect);

setShadowLayer12+

setShadowLayer(shadowLayer: ShadowLayer|null): void

Sets a shadow layer for this brush. The shadow layer effect takes effect only when text is drawn.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
shadowLayerShadowLayer |nullYesImplements a shadow layer. If null is passed in, the shadow layer is cleared.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.

Example

import { RenderNode } from '@kit.ArkUI';
import { common2D, drawing } from '@kit.ArkGraphics2D';

class DrawingRenderNode extends RenderNode {
  draw(context : DrawContext) {
    const canvas = context.canvas;
    let font = new drawing.Font();
    font.setSize(60);

    let textBlob = drawing.TextBlob.makeFromString("hello", font, drawing.TextEncoding.TEXT_ENCODING_UTF8);
    let pen = new drawing.Pen();
    pen.setStrokeWidth(2.0);

    let pen_color : common2D.Color = {alpha: 0xFF, red: 0xFF, green: 0x00, blue: 0x00};
    pen.setColor(pen_color);
    canvas.attachPen(pen);
    canvas.drawTextBlob(textBlob, 100, 100);
    canvas.detachPen();

    let color : common2D.Color = {alpha: 0xFF, red: 0x00, green: 0xFF, blue: 0x00};
    let shadowLayer = drawing.ShadowLayer.create(3, -3, 3, color);
    pen.setShadowLayer(shadowLayer);
    canvas.attachPen(pen);
    canvas.drawTextBlob(textBlob, 100, 200);
    canvas.detachPen();

    let brush = new drawing.Brush();
    let brush_color : common2D.Color = {alpha: 0xFF, red: 0xFF, green: 0x00, blue: 0x00};
    brush.setColor(brush_color);
    canvas.attachBrush(brush);
    canvas.drawTextBlob(textBlob, 300, 100);
    canvas.detachBrush();

    brush.setShadowLayer(shadowLayer);
    canvas.attachBrush(brush);
    canvas.drawTextBlob(textBlob, 300, 200);
    canvas.detachBrush();
  }
}

setBlendMode

setBlendMode(mode: BlendMode) : void

Sets a blend mode for this brush. If this API is not called, the default blend mode is SRC_OVER.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
modeBlendModeYesBlend mode.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed.

Example

import { drawing } from '@kit.ArkGraphics2D';

const brush = new drawing.Brush();
brush.setBlendMode(drawing.BlendMode.SRC);

setImageFilter12+

setImageFilter(filter: ImageFilter|null): void

Sets an image filter for this brush.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
filterImageFilter |nullYesImage filter. If null is passed in, the image filter effect of the brush will be cleared.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types.

Example

import {drawing} from '@kit.ArkGraphics2D';

let brush = new drawing.Brush();
let imgFilter = drawing.ImageFilter.createBlurImageFilter(5, 10, drawing.TileMode.DECAL);
brush.setImageFilter(imgFilter);
brush.setImageFilter(null);

getColorFilter12+

getColorFilter(): ColorFilter

Obtains the color filter of this brush.

System capability: SystemCapability.Graphics.Drawing

Returns

TypeDescription
ColorFilterColor filter.

Example

import {drawing} from '@kit.ArkGraphics2D';

let brush = new drawing.Brush();
let setColorFilter = drawing.ColorFilter.createSRGBGammaToLinear();
brush.setColorFilter(setColorFilter);
let filter = brush.getColorFilter();   

reset12+

reset(): void

Resets this brush to the initial state.

System capability: SystemCapability.Graphics.Drawing

Example

import { drawing } from '@kit.ArkGraphics2D';

const brush = new drawing.Brush();
brush.reset();

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-drawing-gpu-context-h

openharmony 鸿蒙 capi-drawing-nativepixelmap-

openharmony 鸿蒙 capi-nativevsync-oh-nativevsync-expectedraterange

openharmony 鸿蒙 capi-drawing-oh-drawing-brush

openharmony 鸿蒙 capi-drawing-oh-drawing-image-info

openharmony 鸿蒙 capi-drawing-text-linetypography-h

openharmony 鸿蒙 capi-oh-nativeimage-oh-nativeimage

openharmony 鸿蒙 capi-drawing-typeface-h

openharmony 鸿蒙 capi-drawing-brush-h

openharmony 鸿蒙 capi-nativewindow

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/7hEX3Ne6