接入全局取色
接入全局取色功能,用户可以使用手指或者手写笔操作取色器在屏幕上移动,在目标位置抬起手指/抬起手写笔,会生成该位置色值对应的图像信息。
场景介绍
在应用中拉起全局取色,效果如下:

支持获取当前屏幕上选中位置的色值和色域空间。
限制与约束
- 全局取色能力支持设备:Tablet、PC/2in1,并且从5.1.1(19)版本开始,新增支持设备:Phone。
- 设备不支持连接手写笔的话,无法使用全局取色能力。
接口说明
| 类名 | 接口名 | 说明 |
|---|---|---|
| imageFeaturePicker | pickForResult | 启动取色器。此API用于启动取色器,在取色器移动时不显示色值。 |
开发步骤
-
导入相关模块。
import { imageFeaturePicker } from '@kit.Penkit';import { BusinessError } from '@kit.BasicServicesKit'; -
构造全局取色能力。
@Entry@Componentstruct Index {@State message: string = 'Hello World';build() {Stack({ alignContent: Alignment.Center }) {Column() {Row() {Button() {Text('Call GlobalColorPicker from ets side').fontSize(18).fontWeight(FontWeight.Normal)}.width('50%').height('60vp').align(Alignment.Center).onClick((event) => {imageFeaturePicker.pickForResult(event.displayX, event.displayY).then((colorInfo: imageFeaturePicker.PickedColorInfo) => {if (colorInfo) {console.info('colorInfo=' + JSON.stringify(colorInfo));}}).catch((err: BusinessError) => {console.error(`pickForResult failed. Code is ${err.code}, message is ${err.message}`)})})}}.align(Alignment.Center)}.width('100%').height('100%')}}