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

浅尝:iOS的CoreGraphics和Flutter的Canvas

iOS的CoreGraphic

基本就是创建一个自定义的UIView,然后重写drawRect方法,在此方法里使用UIGraphicsGetCurrentContext()来绘制目标图形和样式

#import <UIKit/UIKit.h>@interface MyGraphicView : UIView
@end@implementation MyGraphicView// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
// This method is called when a view is first displayed or when an event occurs that invalidates a visible part of the view.
- (void)drawRect:(CGRect)rect {CGRect rectangle = CGRectMake(0, 0, 320, 100);CGContextRef context = UIGraphicsGetCurrentContext();CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.0);CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 0.5);CGContextFillRect(context, rectangle);CGContextStrokeRect(context, rectangle);
}@end


Flutter的Canvas

这是main函数的主代码,在child节点实现一个自定义的画布

import 'package:flutter/material.dart';final Color darkBlue = Color.fromARGB(255, 18, 32, 47);void main() {runApp(MyApp());
}class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return MaterialApp(debugShowCheckedModeBanner: false,theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),home: Scaffold(// Outer white container with padding body: Container(padding: EdgeInsets.symmetric(horizontal: 40, vertical: 80),color: Colors.white,child: Container(child: CustomPaint(painter: FaceOutlinePainter()),),),),);}
}

自定义一个类继承自CustomPainter,然后重写paint()方法

class FaceOutlinePainter extends CustomPainter {@overridevoid paint(Canvas canvas, Size size) {// 先定义一个画笔paint实例对象final paint = Paint()..style = PaintingStyle.stroke..strokeWidth = 4.0..color = Colors.indigo;// 画左边的眼睛,是一个矩形的,并且有圆角20canvas.drawRRect(RRect.fromRectAndRadius(Rect.fromLTWH(20, 40, 100, 100), Radius.circular(20)),paint,);// 画右边的眼睛,是圆形的canvas.drawOval(Rect.fromLTWH(size.width - 120, 40, 100, 100),paint,);// 画嘴巴// 先初始化一个路径Pathfinal mouth = Path();// 然后从左移动到右边mouth.moveTo(size.width * 0.8, size.height * 0.6);// 然后画椭圆曲线mouth.arcToPoint(Offset(size.width * 0.2, size.height * 0.6),radius: Radius.circular(150),);mouth.arcToPoint(Offset(size.width * 0.8, size.height * 0.6),radius: Radius.circular(200),clockwise: false,);// 把嘴巴画的线路径添加到canvas上进行绘制canvas.drawPath(mouth, paint);}@overridebool shouldRepaint(FaceOutlinePainter oldDelegate) => false;
}

输出的图形大概如下所示
在这里插入图片描述

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

相关文章:

  • 网络安全黑客技术自学
  • 【文件读取/包含】任意文件读取漏洞 afr_3
  • 第四章:单例模式与final
  • 深入Android S(12.0) 探索 Android Framework 之 SystemServer 进程启动详解
  • 搜维尔科技:【软件篇】TechViz是一款专为工程设计的专业级3D可视化软件
  • android Handler
  • 【Ubuntu·系统·的Linux环境变量配置方法最全】
  • Django之模板层
  • 社区论坛小程序系统源码+自定义设置+活动奖励 自带流量主 带完整的搭建教程
  • 2023亚太杯数学建模C题思路解析
  • acme在同一台服务器上设置多个Ali_key实现自动ssl申请和续期
  • 乐观锁与悲观锁
  • 【算法】堆排序
  • 51单片机应用从零开始(三)
  • 如何在 Nginx Proxy Manager(NPM)上部署静态网站
  • http的几种方法
  • var、let、const关键字的特性,以及let、const暂时性死区的作用
  • IDEA 高分辨率卡顿优化
  • 【AIGC】一起学习prompt提示词(4/4)【经典】【15种提示词技巧】
  • Linux实战一天一个小指令--《文件管理/文件查找》
  • CocosCreator3.8神秘面纱 CocosCreator 项目结构说明及编辑器的简单使用
  • JJJ:python学习笔记
  • SpringSecurity6从入门到上天系列第七篇:讲明白SpringBoot的自动装配完善上篇文章中的结论
  • ClickHouse 原理解析之基础知识总结
  • 最小花费——最短路
  • Spark DataFrame join后移除重复的列
  • NextJS工程部署到阿里云linux Ecs
  • 汽车以太网IOP测试新利器
  • 高防IP是什么?如何隐藏源站IP?如何进行防护?
  • ElasticSearch---查询es集群状态、分片、索引