简体   繁体   English

如何在两个偏移之间添加控制点(贝塞尔曲线)?

[英]How to add a control point (Bézier curve) between two Offsets?

I'm trying to animate a picture sliding on the screen.我正在尝试为在屏幕上滑动的图片设置动画。 So far I'm using a Tween<Offset> to lerp between two offsets: Tween(begin: startOffset, end: destOffset) .到目前为止,我使用Tween<Offset>在两个偏移量之间调整: Tween(begin: startOffset, end: destOffset) This animates the picture sliding in a straight line.这使图片沿直线滑动。

Is there an easy way to add a control point to form a Bézier curve?有没有一种简单的方法可以添加控制点以形成贝塞尔曲线? I see there's quadraticBezierTo in Path class, but I don't know how to transform between an Offset (or a Point ) class to a Path class.我看到Path class 中有quadraticBezierTo ,但我不知道如何在Offset (或Point )class 到Path class 之间进行转换。 Another way is to create my own Tween that calculates a Bézier curve instead of just doing lerp , but I thought I'd ask before reinventing the wheel.另一种方法是创建我自己的Tween来计算 Bézier 曲线,而不是仅仅做lerp ,但我想在重新发明轮子之前我会问。

It's not difficult to write a BezierTween that calculates a Bezier curve with a control point:编写一个计算带有控制点的贝塞尔曲线的 BezierTween 并不难:

class BezierTween extends Tween<Offset> {
  final Offset begin;
  final Offset end;
  final Offset control;

  BezierTween({this.begin, this.end, this.control})
      : super(begin: begin, end: end);

  @override
  Offset lerp(double t) {
    assert(begin != null);
    assert(end != null);
    assert(control != null);
    final t1 = 1 - t;
    return begin * t1 * t1 + control * 2 * t1 * t + end * t * t;
  }
}

To use it, change:要使用它,请更改:

Tween(begin: startOffset, end: destOffset from the question, to: Tween(begin: startOffset, end: destOffset从问题到:

BezierTween(begin: startOffset, end: destOffset, control: controlOffset)

However I'm still wondering if this kind of use case is already built into the awesome Flutter framework.但是我仍然想知道这种用例是否已经内置在了不起的 Flutter 框架中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM