// This custom clipper help us achieve n-pointed star shape

class StarClipper extends CustomClipper {

/// The number of points of the star

final int points;

StarClipper(this.points);

// Degrees to radians conversion

double _degreeToRadian(double deg) => deg * (math.pi / 180.0);

@override

Path getClip(Size size) {

Path path = Path();

double max = 2 * math.pi;

double width = size.width;

double halfWidth = width / 2;

double wingRadius = halfWidth;

double radius = halfWidth / 2;

double degreesPerStep = _degreeToRadian(360 / points);

double halfDegreesPerStep = degreesPerStep / 2;

path.moveTo(width, halfWidth);

for (double step = 0; step < max; step += degreesPerStep) {

path.lineTo(halfWidth + wingRadius * math.cos(step),

halfWidth + wingRadius * math.sin(step));

path.lineTo(halfWidth + radius * math.cos(step + halfDegreesPerStep),

halfWidth + radius * math.sin(step + halfDegreesPerStep));

}

path.close();

return path;

}

// If the new instance represents different information than the old instance, this method will return true, otherwise it should return false.

@override

bool shouldReclip(CustomClipper oldClipper) {

StarClipper starClipper = oldClipper as StarClipper;

return points != starClipper.points;

}

}

  1. 现在您可以轻松绘制星星了,如下所示:

SizedBox(

height: 360,

width: 360,

child: ClipPath(

clipper: StarClipper(6),

child: Container(

height: 300,

color: Colors.amber,

),

),

),

完整示例


预览

此示例生成 4 种不同的星形:5 角星、6 角星、10 角星和 20 角星。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VF2prauV-1634091109338)(D:\study_manger\hugo_LoveIt_blog\content\post\flutter_tips\Flutter:使用 CustomClipper 绘制 N 角星.assets\Screen-Shot-2021-10-11-at-14.30.25.jpg)]

编码

main.dart 中的完整源代码和解释:

// main.dart

import ‘package:flutter/material.dart’;

import ‘dart:math’ as math;

// This custom clipper help us achieve n-pointed star shape

class StarClipper extends CustomClipper {

/// The number of points of the star

final int points;

StarClipper(this.points);

// Degrees to radians conversion

double _degreeToRadian(double deg) => deg * (math.pi / 180.0);

@override

Path getClip(Size size) {

Path path = Path();

double max = 2 * math.pi;

double width = size.width;

double halfWidth = width / 2;

double wingRadius = halfWidth;

double radius = halfWidth / 2;

double degreesPerStep = _degreeToRadian(360 / points);

double halfDegreesPerStep = degreesPerStep / 2;

path.moveTo(width, halfWidth);

for (double step = 0; step < max; step += degreesPerStep) {

path.lineTo(halfWidth + wingRadius * math.cos(step),

halfWidth + wingRadius * math.sin(step));

path.lineTo(halfWidth + radius * math.cos(step + halfDegreesPerStep),

halfWidth + radius * math.sin(step + halfDegreesPerStep));

}

path.close();

return path;

}

// If the new instance represents different information than the old instance, this method will return true, otherwise it should return false.

@override

bool shouldReclip(CustomClipper oldClipper) {

StarClipper starClipper = oldClipper as StarClipper;

return points != starClipper.points;

}

}

void main() {

runApp(const MyApp());

}

class MyApp extends StatelessWidget {

const MyApp({Key? key}) : super(key: key);

@override

Widget build(BuildContext context) {

return MaterialApp(

debugShowCheckedModeBanner: false,

title: ‘KindaCode.com’,

theme: ThemeData(

primarySwatch: Colors.indigo,

),

home: const MyHomePage(),

);

}

}

class MyHomePage extends StatefulWidget {

const MyHomePage({Key? key}) : super(key: key);

@override

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

有任何问题,欢迎广大网友一起来交流,分享高阶Android学习视频资料和面试资料包~

偷偷说一句:群里高手如云,欢迎大家加群和大佬们一起交流讨论啊!

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

roid)**

最后

有任何问题,欢迎广大网友一起来交流,分享高阶Android学习视频资料和面试资料包~

偷偷说一句:群里高手如云,欢迎大家加群和大佬们一起交流讨论啊!

[外链图片转存中…(img-8xxkzHPp-1712638640480)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐