Rect.fromCircle({ Offset center, double radius })
center | 圆弧中心点(相对于原点的偏移量) |
radius | 圆弧半径(double) |
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…color=Colors.redAccent
…invertColors=false;
Rect rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
canvas.drawArc(rect, 0.0, 2*pi/4, false, paint);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
Rect.fromLTRB(this.left, this.top, this.right, this.bottom)
left | 矩形最左边相对于原点(0,0)偏移量 |
top | 矩形最顶部相对于原点(0,0)偏移量 |
right | 矩形最右边相对于原点(0,0)偏移量 |
bottom | 矩形最底部相对于原点(0,0)偏移量 |
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…color=Colors.redAccent
…invertColors=false;
Rect rect=Rect.fromLTRB(0.0, 0.0, size.width, size.height/2);
canvas.drawArc(rect, 0.0, 2*pi/4, false, paint);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
Rect.fromCenter({ Offset center, double width, double height })
center | 矩形中心相对于原点偏移量 |
width | 矩形宽度 |
height | 矩形高度 |
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…color=Colors.redAccent
…invertColors=false;
Rect rect=Rect.fromCenter(center: Offset(size.width/2,size.height/2),width:size.width/2,height:size.height/2);
canvas.drawArc(rect, 0.0, 2*pi/4, false, paint);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
Rect.fromLTWH(double left, double top, double width, double height)
left | 矩形最左边相对于原点偏移量 |
top | 矩形最顶部相对于原点偏移量 |
width | 矩形宽度 |
height | 矩形高度 |
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…color=Colors.redAccent
…invertColors=false;
Rect rect=Rect.fromLTWH(0.0, 0.0, size.width/2, size.height/2);
canvas.drawArc(rect, 0.0, 2*pi/4, false, paint);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
Rect.fromPoints(Offset a, Offset b) : this.fromLTRB(
math.min(a.dx, b.dx),
math.min(a.dy, b.dy),
math.max(a.dx, b.dx),
math.max(a.dy, b.dy),
);
a | 相对于原点x方向上的偏移范围 |
b | 相对于原点y方向上的偏移范围 |
下面三种写法效果一样(主要是取x或y方向上偏移量的范围)
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=1.0
…color=Colors.redAccent
…invertColors=false;
Rect rect=Rect.fromPoints(Offset(size.width, 0.0),Offset(0,size.height));
canvas.drawArc(rect, 0.0, 2*pi/4, false, paint);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=1.0
…color=Colors.redAccent
…invertColors=false;
Rect rect=Rect.fromPoints(Offset(0.0, 0.0),Offset(size.width,size.height));
canvas.drawArc(rect, 0.0, 2*pi/4, false, paint);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=1.0
…color=Colors.redAccent
…invertColors=false;
Rect rect=Rect.fromPoints(Offset(size.width, size.height),Offset(0.0,0.0));
canvas.drawArc(rect, 0.0, 2*pi/4, false, paint);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
useCenter=false(绘制扇形)
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…color=Colors.redAccent
…invertColors=false;
Rect rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…color=Colors.redAccent
…invertColors=false;
Rect rect=Rect.fromLTRB(0.0, 0.0, size.width, size.height/2);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
Rect.lerp(在两个矩形之间进行线性插值)
源码:
static Rect lerp(Rect a, Rect b, double t) {
assert(t != null);
if (a == null && b == null)
return null;
if (a == null)
return Rect.fromLTRB(b.left * t, b.top * t, b.right * t, b.bottom * t);
if (b == null) {
final double k = 1.0 - t;
return Rect.fromLTRB(a.left * k, a.top * k, a.right * k, a.bottom * k);
}
return Rect.fromLTRB(
lerpDouble(a.left, b.left, t),
lerpDouble(a.top, b.top, t),
lerpDouble(a.right, b.right, t),
lerpDouble(a.bottom, b.bottom, t),
);
}
double lerpDouble(num a, num b, double t) {
if (a == null && b == null)
return null;
a ??= 0.0;
b ??= 0.0;
return a + (b - a) * t;
}
a | 矩形 |
b | 矩形 |
t | 时间值(0.0~1.0之间) |
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…color=Colors.redAccent
…invertColors=false;
Rect a=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: size.width/3);
Rect b=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: size.width/2);
Rect rect=Rect.lerp(a, b, 1.0);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint);
rect=Rect.lerp(a, b, 0.2);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint…color=Colors.blue);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…color=Colors.redAccent
…invertColors=false;
Rect a=Rect.fromLTRB(80.0, 80.0, size.width-30.0, size.height-60.0);
Rect b=Rect.fromLTRB(50.0, 50.0, size.width-20.0, size.height-20.0);
Rect rect=Rect.lerp(a, b, 1.0);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint);
rect=Rect.lerp(a, b, 0.2);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint…color=Colors.blue);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…color=Colors.redAccent
…invertColors=false;
Rect a=Rect.fromLTRB(80.0, 80.0, size.width-80.0, size.height-80.0);
Rect b=Rect.fromLTRB(50.0, 50.0, size.width-50.0, size.height-50.0);
Rect rect=Rect.lerp(a, b, 1.0);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint);
rect=Rect.lerp(a, b, 0.7);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint…color=Colors.blue);
rect=Rect.lerp(a, b, 0.5);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint…color=Colors.green);
rect=Rect.lerp(a, b, 0.3);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint…color=Colors.deepPurpleAccent);
rect=Rect.lerp(a, b, 0.1);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint…color=Colors.cyanAccent);
rect=Rect.lerp(a, b, -0.2);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint…color=Colors.lightGreenAccent);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
画圆
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…color=Colors.redAccent
…invertColors=false;
Rect rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
canvas.drawArc(rect, 0.0, 2*pi, false, paint);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
画同心圆
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…color=Colors.redAccent
…invertColors=false;
Rect rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
canvas.drawArc(rect, 0.0, 2*pi, false, paint);
rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 80.0);
canvas.drawArc(rect, 0.0, 2*pi, false, paint…color=Colors.blue);
rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 60.0);
canvas.drawArc(rect, 0.0, 2*pi, false, paint…color=Colors.deepPurple);
rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 40.0);
canvas.drawArc(rect, 0.0, 2*pi, false, paint…color=Colors.lightGreenAccent);
rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 20.0);
canvas.drawArc(rect, 0.0, 2*pi, false, paint…color=Colors.grey);
rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 10.0);
canvas.drawArc(rect, 0.0, 2*pi, false, paint…color=Colors.pinkAccent);
rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 2.0);
canvas.drawArc(rect, 0.0, 2*pi, false, paint…color=Colors.blueAccent);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
绘制渐变圆
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…invertColors=false;
Rect rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
canvas.drawArc(rect, 0.0, 2*pi, false, paint…color=Colors.redAccent.withOpacity(0.2));
rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 80.0);
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
le(center: Offset(size.width/2,size.height/2),radius: 100.0);
canvas.drawArc(rect, 0.0, 2*pi, false, paint…color=Colors.redAccent.withOpacity(0.2));
rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 80.0);
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-y4KeNRHT-1715517327563)]
[外链图片转存中…(img-qyO68dnW-1715517327565)]
[外链图片转存中…(img-3KKVa6de-1715517327565)]
[外链图片转存中…(img-9If07zgV-1715517327566)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!