harmony-utils之SnapshotUtil,截图相关工具类
【摘要】 harmony-utils之SnapshotUtil,截图相关工具类 harmony-utils 简介与说明harmony-utils 一款功能丰富且极易上手的HarmonyOS工具库,借助众多实用工具类,致力于助力开发者迅速构建鸿蒙应用。其封装的工具涵盖了APP、设备、屏幕、授权、通知、线程间通信、弹框、吐司、生物认证、用户首选项、拍照、相册、扫码、文件、日志,异常捕获、字符、字符串、数...
harmony-utils之SnapshotUtil,截图相关工具类
harmony-utils 简介与说明
harmony-utils 一款功能丰富且极易上手的HarmonyOS工具库,借助众多实用工具类,致力于助力开发者迅速构建鸿蒙应用。其封装的工具涵盖了APP、设备、屏幕、授权、通知、线程间通信、弹框、吐司、生物认证、用户首选项、拍照、相册、扫码、文件、日志,异常捕获、字符、字符串、数字、集合、日期、随机、base64、加密、解密、JSON等一系列的功能和操作,能够满足各种不同的开发需求。
picker_utils 是harmony-utils拆分出来的一个子库,包含PickerUtil、PhotoHelper、ScanUtil。
下载安装
ohpm i @pura/harmony-utils
ohpm i @pura/picker_utils
//全局初始化方法,在UIAbility的onCreate方法中初始化 AppUtil.init()
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
AppUtil.init(this.context);
}
API方法与使用
get 获取已加载的组件的截图,传入组件的组件id,找到对应组件进行截图
let pixelMap1 = await SnapshotUtil.get('snapshot_id1');
let pixelMap2 = SnapshotUtil.getSync('snapshot_id2');
createFromBuilder 在应用后台渲染CustomBuilder自定义组件,并输出其截图
SnapshotUtil.createFromBuilder(() => {
this.RandomBuilder()
}).then((pixelMap) => {
Utils.showSheetImg(pixelMap);
}).catch((err: BusinessError) => {
ToastUtil.showToast("组件生成图片异常!");
});
@Builder
RandomBuilder() {
Column() {
Text("Builder截图")
.fontSize(20)
.fontWeight(FontWeight.Bold)
.fontStyle(FontStyle.Italic)
.margin({ top: 20 })
Image($r("app.media.test_as4"))
.margin({ top: 30, bottom: 10 })
.padding(10)
.width('95%')
.backgroundColor(Color.Pink)
}
.backgroundColor(Color.Blue)
.padding(10)
.width('100%')
.aspectRatio(1)
.margin({ top: 50 })
.id("rBuilder")
}
snapshot 获取窗口截图,使用Promise异步回调
let pixelMap3 = await SnapshotUtil.snapshot();
onSnapshotListener 开启系统截屏事件的监听
SnapshotUtil.onSnapshotListener(() => {
ToastUtil.showToast("系统截图了!");
LogUtil.error("系统截图了!");
});
removeSnapshotListener 关闭系统截屏事件的监听
SnapshotUtil.removeSnapshotListener();
SnapshotUtil.removeSnapshotListener(snapshotCallBack);
示例代码
import { router } from '@kit.ArkUI';
import { MockSetup } from '@ohos/hamock';
import { KeyboardCallBack, KeyboardUtil, LogUtil, ToastUtil } from '@pura/harmony-utils';
import { TitleBarView } from '../../component/TitleBarView';
import { DescribeBean } from '../../model/DescribeBean';
/**
* 键盘工具类
*/
@Entry
@Component
struct Index {
private scroller: Scroller = new Scroller();
private controller: TextInputController = new TextInputController()
@State describe: DescribeBean = router.getParams() as DescribeBean;
private callback: KeyboardCallBack = (show, height) => {
LogUtil.error("onKeyboardListener-A: " + height);
if (show) {
ToastUtil.showToast(`键盘显示!高度为:${height}`);
} else {
ToastUtil.showToast(`键盘已关闭!`);
}
};
@MockSetup
mock() {
this.describe = new DescribeBean("KeyboardUtil", "键盘工具类");
}
build() {
Column() {
TitleBarView({ describe: this.describe })
Divider()
Scroll(this.scroller) {
Column() {
Button("show(),主动拉起键盘")
.btnStyle()
.onClick(() => {
KeyboardUtil.show("id_input_1000");
})
Button("hide(),关闭键盘")
.btnStyle()
.onClick(() => {
KeyboardUtil.hide();
})
Button("onKeyboardListener()")
.btnStyle()
.onClick(() => {
KeyboardUtil.onKeyboardListener((show, height) => {
LogUtil.error("onKeyboardListener-B: " + height);
if (show) {
ToastUtil.showToast(`键盘显示,高度为:${height}`);
} else {
ToastUtil.showToast(`键盘已关闭!`);
}
});
})
Button("removeKeyboardListener()")
.btnStyle()
.onClick(() => {
KeyboardUtil.removeKeyboardListener(); //移除所有
})
Button("onKeyboardListener()-指定")
.btnStyle()
.onClick(() => {
KeyboardUtil.onKeyboardListener(this.callback);
})
Button("removeKeyboardListener()-指定")
.btnStyle()
.onClick(() => {
KeyboardUtil.removeKeyboardListener(this.callback); //移除指定
})
TextInput({ placeholder: '请输入', controller: this.controller })
.margin({ top: 30 })
.width('90%')
.id("id_input_1000")
Blank().layoutWeight(1)
}
.margin({ top: 5, bottom: 5 })
}
.layoutWeight(1)
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Start)
.backgroundColor($r('app.color.main_background'))
}
}
@Styles
function btnStyle() {
.width('90%')
.margin({ top: 10, bottom: 5 })
}
创作不易,请给童长老点赞👍
https://github.com/787107497/harmony-utils
https://gitee.com/tongyuyan/harmony-utils
OpenHarmony三方库
童长老CSDN博客
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)