当前位置: 首页 > news >正文

HarmonyOS/OpenHarmony元服务开发-卡片使用自定义绘制能力

ArkTS卡片开放了自定义绘制的能力,在卡片上可以通过Canvas组件创建一块画布,然后通过CanvasRenderingContext2D对象在画布上进行自定义图形的绘制,如下示例代码实现了在画布的中心绘制了一个笑脸。

@Entry
@Component
struct Card {
private canvasWidth: number = 0;
private canvasHeight: number = 0;
// 初始化CanvasRenderingContext2D和RenderingContextSettings
private settings: RenderingContextSettings = new RenderingContextSettings(true);
private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);build() {
Column() {
Row() {
Canvas(this.context)
.margin('5%')
.width('90%')
.height('90%')
.onReady(() => {
console.info('[ArkTSCard] onReady for canvas draw content');
// 在onReady回调中获取画布的实际宽和高
this.canvasWidth = this.context.width;
this.canvasHeight = this.context.height;
// 绘制画布的背景
this.context.fillStyle = 'rgba(203, 154, 126, 1.00)';
this.context.fillRect(0, 0, this.canvasWidth, this.canvasHeight);
// 在画布的中心绘制一个红色的圆
this.context.beginPath();
let radius = this.context.width / 3
let circleX = this.context.width / 2
let circleY = this.context.height / 2
this.context.moveTo(circleX - radius, circleY);
this.context.arc(circleX, circleY, radius, 2 * Math.PI, 0, true);
this.context.closePath();
this.context.fillStyle = 'red';
this.context.fill();
// 绘制笑脸的左眼
let leftR = radius / 4
let leftX = circleX - (radius / 2)
let leftY = circleY - (radius / 3.5)
this.context.beginPath();
this.context.arc(leftX, leftY, leftR, 0, Math.PI, true);
this.context.strokeStyle = '#ffff00'
this.context.lineWidth = 10
this.context.stroke()
// 绘制笑脸的右眼
let rightR = radius / 4
let rightX = circleX + (radius / 2)
let rightY = circleY - (radius / 3.5)
this.context.beginPath();
this.context.arc(rightX, rightY, rightR, 0, Math.PI, true);
this.context.strokeStyle = '#ffff00'
this.context.lineWidth = 10
this.context.stroke()
// 绘制笑脸的嘴巴
let mouthR = radius / 2.5
let mouthX = circleX
let mouthY = circleY + (radius / 3)
this.context.beginPath();
this.context.arc(mouthX, mouthY, mouthR, Math.PI, 0, true);
this.context.strokeStyle = '#ffff00'
this.context.lineWidth = 10
this.context.stroke()
})
}
}.height('100%').width('100%')
}
}

 

http://www.lryc.cn/news/107443.html

相关文章:

  • SpringBoot引入MyBatisGenerator
  • JVM面试题--实践
  • 神经网络的搭建与各层分析
  • SQL-每日一题【1174. 即时食物配送 II】
  • MySQL学习记录:第一章 DQL语言
  • redis+token+分布式锁确保接口的幂等性
  • Vue模版语法
  • 新一代开源流数据湖平台Apache Paimon入门实操-上
  • ELK 企业级日志分析系统(一)
  • 2023-08-01力扣今日二题-Hard-DPLIS优先队列-好题
  • 并发 如何创建线程 多线程
  • 亚马逊鲲鹏系统是怎么引流的?
  • 第五章 Git
  • 无涯教程-Lua - 变量声明
  • vue3学习-组件基础、深入组件
  • 原型链污染分析
  • RF PCB的9条改进型建议
  • 网络安全(黑客)自学就业
  • uni-app选择器( uni-data-picker)选择任意级别
  • 网络入侵探测器Pi.Alert
  • Flask项目打包为exe(附带项目资源,静态文件)
  • 无代码开发(BIP旗舰版-YonBuilder)
  • 誉天程序员-瀑布模型-敏捷开发模型-DevOps模型比较
  • flutter:占位视图(骨架屏、shimmer)
  • 【雕爷学编程】MicroPython动手做(30)——物联网之Blynk 4
  • python-网络爬虫.BS4
  • C# 开发规范
  • 【uniapp 样式】使用setStorageSync存储历史搜索记录
  • git remote add origin详解
  • 附录1-将uni-app运行到微信开发者工具