android - 将视图转换为位图数据以另存为图像 | 本机脚本
问题描述
方法一:
这是我的ts 代码
let makeImage = ViewModule.getViewById("makeImage");
makeImage.android.setDrawingCacheEnabled(true);
无法使用“getViewById”获取视图。
查看代码:
<StackLayout id="makeImage">
<Button text="Hi"></Button>
</StackLayout>
方法二:
const view = application.android.foregroundActivity.getWindow().getDecorView().getRootView();
view.setDrawingCacheEnabled(true);
const bmp = android.graphics.Bitmap.createBitmap(view.getDrawingCache());
let x = imageSourceModule.fromNativeSource(bmp);
imageData = x.toBase64String("png");
console.log(imageData);
Result: Able to get the screenshot of whole page of parent. Not a mentioned section.
解决方案
你可以试试nativescript-screenshot插件,它当然在内部使用与 Android 相同的方法,并且也为 iOS 提供了等效方法。
始终使用该loaded
事件来访问本机视图 ( .andorid
/ .ios
),以确保它已创建。
HTML
<StackLayout (loaded)="onLoaded($event)">
<Button text="Hi"></Button>
</StackLayout>
TS
onLoaded(event) {
const imageSource = plugin.getImage(args.object);
}
推荐阅读
- google-sheets - 为什么我的条件格式在 Google 表格中不起作用?简单的 COUNTIFS
- mysql - MySQL - 获取没有时区偏移的日期
- mysql - 从 3 个 mySQL 表中提取数据
- reactjs - 未捕获的类型错误:无法重新定义属性:UIRouter
- ruby - Sharetribe - 重定向到 projectname.lvh.me 和 500 内部服务器错误
- angular - Karma 测试 NullInjectorError: No provider for InjectionToken fileName
- php - Bootstrap 4 表 td 宽度尺寸自定义
- android - 片段管理 - 导航抽屉
- r - 原始数据在小鼠汇总估计中的作用(R 包)?
- opengl - 更新 SSBO 内容