paint method
Called whenever the object needs to paint. The given Canvas has its
coordinate space configured such that the origin is at the top left of the
box. The area of the box is the size of the size argument.
Paint operations should remain inside the given area. Graphical operations outside the bounds may be silently ignored, clipped, or not clipped. It may sometimes be difficult to guarantee that a certain operation is inside the bounds (e.g., drawing a rectangle whose size is determined by user inputs). In that case, consider calling Canvas.clipRect at the beginning of paint so everything that follows will be guaranteed to only draw within the clipped area.
Implementations should be wary of correctly pairing any calls to Canvas.save/Canvas.saveLayer and Canvas.restore, otherwise all subsequent painting on this canvas may be affected, with potentially hilarious but confusing results.
To paint text on a Canvas, use a TextPainter.
To paint an image on a Canvas:
-
Obtain an ImageStream, for example by calling ImageProvider.resolve on an AssetImage or NetworkImage object.
-
Whenever the ImageStream's underlying ImageInfo object changes (see ImageStream.addListener), create a new instance of your custom paint delegate, giving it the new ImageInfo object.
-
In your delegate's paint method, call the Canvas.drawImage, Canvas.drawImageRect, or Canvas.drawImageNine methods to paint the ImageInfo.image object, applying the ImageInfo.scale value to obtain the correct rendering size.
Implementation
@override
void paint(Canvas canvas, Size size) {
double waveLength = size.width;
double radius = size.width / 2;
double centerY = size.height * (1 - progress);
Paint circlePaint = Paint()..color = backgroundColor;
List<Paint> wavePaints = [];
List<Path> paths = [];
double dynamicWaveHeight = 5;
if (progress == 1 || progress == 0) {
dynamicWaveHeight = 8;
if (!completedIsShowWave) {
dynamicWaveHeight = 0;
}
}
/// 波浪画笔
for (var index = 0; index < colorsWave.length; index++) {
wavePaints.add(Paint()..color = colorsWave[index]);
paths.add(Path());
}
double sindy = 2.5;
if (paths.length == 1) {
sindy = 1.5;
} else if (paths.length == 2) {
sindy = 2.0;
}
/// 绘制波浪
for (var index = 0; index < paths.length; index++) {
for (double i = -waveLength / 4; i <= waveLength * 1.5; i++) {
double dx = i;
double dy;
dy =
sin((i / waveLength * sindy * pi) + (waveAnimationValue * 2 * pi)) *
dynamicWaveHeight +
centerY;
if (index == 1) {
dy = sin((i / waveLength * sindy * pi) +
(waveAnimationValue * 2 * pi) +
pi / 2) *
dynamicWaveHeight +
centerY;
} else if (index == 2) {
dy = sin((i / waveLength * sindy * pi) +
(waveAnimationValue * 2 * pi) +
pi) *
dynamicWaveHeight +
centerY;
}
if (i == -waveLength / 4) {
paths[index].moveTo(dx, dy);
} else {
paths[index].lineTo(dx, dy);
}
}
sindy -= .5;
}
for (var index = 0; index < paths.length; index++) {
paths[index].lineTo(size.width * 3, size.height);
paths[index].lineTo(-waveLength / 4, size.height);
paths[index].close();
}
// 绘制圆形背景
canvas.drawCircle(Offset(radius, radius), radius, circlePaint);
// // 裁剪圆形区域
canvas.clipPath(Path()
..addOval(
Rect.fromCircle(center: Offset(radius, radius), radius: radius)));
// 创建矩形背景
Rect rect = Rect.fromLTWH(0, 0, size.width, size.height);
canvas.clipPath(Path()
..addOval(
Rect.fromCircle(center: Offset(radius, radius), radius: radius)));
// 绘制背景矩形
canvas.drawRect(rect, circlePaint);
// 绘制多层波浪
for (var i = 0; i < wavePaints.length; i++) {
canvas.drawPath(paths[i], wavePaints[i]);
}
}