您好,登錄后才能下訂單哦!
本篇內容主要講解“Android怎么利用貝塞爾曲線繪制動畫”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Android怎么利用貝塞爾曲線繪制動畫”吧!
通過動畫控制繪制的結束點,就可以讓貝塞爾曲線動起來。例如下面的動圖展示的效果,看起來像搭了一個滑滑梯一樣。實際上就是用7條貝塞爾曲線實現的,我們使用了 Animation
對象的值來控制繪制的結束點,從而實現了對應的動畫效果。
具體源碼如下,其中控制繪制結束點就是在動畫過程中修改循環的次數,即t <= (100 * animationValue).toInt();
這句代碼,其中 animationValue 是動畫控制器當前值,范圍時從0-1。
class AnimationBezierPainter extends CustomPainter { AnimationBezierPainter({required this.animationValue}); final double animationValue; @override void paint(Canvas canvas, Size size) { final lineWidth = 6.0; paint.strokeWidth = lineWidth; paint.style = PaintingStyle.stroke; final colors = [ Color(0xFFE05100), Color(0xFFF0A060), Color(0xFFE0E000), Color(0xFF10F020), Color(0xFF2080F5), Color(0xFF104FF0), Color(0xFFA040E5), ]; final lineNumber = 7; for (var i = 0; i < lineNumber; ++i) { paint.color = colors[i % colors.length]; _drawAnimatedLines(canvas, paint, size, size.height / 4 + i * lineWidth); } } @override bool shouldRepaint(covariant CustomPainter oldDelegate) { return true; } _drawRainbowLines(Canvas canvas, Paint paint, Size size, yPos) { var yGap = 60.0; var p0 = Offset(0, yPos - yGap / 2); var p1 = Offset(size.width * 2 / 3, yPos - yGap); var p2 = Offset(size.width / 3, yPos + yGap); var p3 = Offset(size.width, yPos + yGap * 1.5); var path = Path(); path.moveTo(p0.dx, p0.dy); for (var t = 1; t <= (100 * animationValue).toInt(); t += 1) { var curvePoint = BezierUtil.get3OrderBezierPoint(p0, p1, p2, p3, t / 100.0); path.lineTo(curvePoint.dx, curvePoint.dy); } canvas.drawPath(path, paint); } }
我們修改曲線的控制點還可以實現下面的效果,大家有興趣可以自己嘗試一下。
用多個貝塞爾曲線首尾相接,在垂直方向疊起來就能畫出一條彈簧了,然后我們更改彈簧的間距和高度(曲線的數量)就能做出彈簧壓下去和彈起來的動畫效果了。
這部分的代碼如下所示:
@override void paint(Canvas canvas, Size size) { var paint = Paint()..color = Colors.black54; final lineWidth = 2.0; paint.strokeWidth = lineWidth; paint.style = PaintingStyle.stroke; final lineNumber = 20; // 彈簧效果 final yGap = 2.0 + 16.0 * animationValue; for (var i = 0; i < (lineNumber * animationValue).toInt(); ++i) { _drawSpiralLines( canvas, paint, size, size.width / 2, size.height - i * yGap, yGap); } } _drawSpiralLines(Canvas canvas, Paint paint, Size size, double xPos, double yPos, double yGap) { final xWidth = 160.0; var p0 = Offset(xPos, yPos); var p1 = Offset(xPos + xWidth / 2 + xWidth / 4, yPos - yGap); var p2 = Offset(xPos + xWidth / 2 - xWidth / 4, yPos - 3 * yGap); var p3 = Offset(xPos, yPos - yGap); var path = Path(); path.moveTo(p0.dx, p0.dy); for (var t = 1; t <= 100; t += 1) { var curvePoint = BezierUtil.get3OrderBezierPoint(p0, p1, p2, p3, t / 100.0); path.lineTo(curvePoint.dx, curvePoint.dy); } canvas.drawPath(path, paint); }
到此,相信大家對“Android怎么利用貝塞爾曲線繪制動畫”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。