浅尝:iOS的CoreGraphics和Flutter的Canvas

iOS的CoreGraphic

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

objc 复制代码
#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节点实现一个自定义的画布

javascript 复制代码
import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget 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()方法

javascript 复制代码
class FaceOutlinePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    // 先定义一个画笔paint实例对象
    final paint = Paint()
      ..style = PaintingStyle.stroke
      ..strokeWidth = 4.0
      ..color = Colors.indigo;

    // 画左边的眼睛,是一个矩形的,并且有圆角20
    canvas.drawRRect(
      RRect.fromRectAndRadius(Rect.fromLTWH(20, 40, 100, 100), Radius.circular(20)),
      paint,
    );
    // 画右边的眼睛,是圆形的
    canvas.drawOval(
      Rect.fromLTWH(size.width - 120, 40, 100, 100),
      paint,
    );
    
    // 画嘴巴
    // 先初始化一个路径Path
    final 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);
  }

  @override
  bool shouldRepaint(FaceOutlinePainter oldDelegate) => false;
}

输出的图形大概如下所示

相关推荐
晚烛10 小时前
实战前瞻:构建高可靠、低延迟的 Flutter + OpenHarmony 智慧交通出行平台
前端·javascript·flutter
ujainu小10 小时前
Flutter 权限管理实战手册:permission_handler 全平台适配与最佳实践
flutter
子榆.11 小时前
Flutter 与开源鸿蒙(OpenHarmony)工程化实践:CI/CD、性能监控与多端发布
flutter·开源·harmonyos
QuantumLeap丶11 小时前
《Flutter全栈开发实战指南:从零到高级》- 26 -持续集成与部署
android·flutter·ios
阿拉斯攀登12 小时前
电子签名:SpringBoot + 汉王 ESP560 的考核签名项目实施方案
springboot·canvas·电子签名·电子签·汉王
2501_9159184114 小时前
TCP 抓包分析在复杂网络问题中的作用,从连接和数据流层面理解系统异常行为
网络·网络协议·tcp/ip·ios·小程序·uni-app·iphone
晚烛14 小时前
实战前瞻:构建高安全、强协同的 Flutter + OpenHarmony 智慧金融移动银行平台(支持国产密码体系、生物认证与信创全栈适配)
安全·flutter·金融
子榆.14 小时前
Flutter 与开源鸿蒙(OpenHarmony)国际化与无障碍适配指南:打造真正包容的跨平台应用
flutter·华为·开源·harmonyos
子榆.15 小时前
Flutter 与开源鸿蒙(OpenHarmony)深度集成:从原理到实战进阶
flutter·华为·开源·harmonyos
二流小码农15 小时前
鸿蒙开发:个人开发者如何使用华为账号登录
android·ios·harmonyos