mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
This CL cleans up the sky/framework/animation as follows: 1) I've moved code that's used only by the custom elements framework into sky/framework/elements/animation. This code is based on AnimationDelegates rather than Streams. 2) Rename ScrollCurve to ScrollBehavior because it encapsulates more behavior than just a curve. 3) Make the Generator interface explicit and mark subclasses as actual subclasses. 4) Move Simulation into generators.dart because it implements the Generator interface. 5) Move Animation out of generators.dart because it does not implement the Generator interface. R=eseidel@chromium.org Review URL: https://codereview.chromium.org/1001373002
52 lines
1.2 KiB
Dart
52 lines
1.2 KiB
Dart
// Copyright 2015 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import "dart:math" as math;
|
|
import "dart:sky" as sky;
|
|
|
|
abstract class AnimationDelegate {
|
|
void updateAnimation(double t);
|
|
}
|
|
|
|
class AnimationTimer {
|
|
final AnimationDelegate _delegate;
|
|
double _startTime = 0.0;
|
|
double _duration = 0.0;
|
|
int _animationId = 0;
|
|
|
|
AnimationTimer(this._delegate);
|
|
|
|
void start(double duration) {
|
|
if (_animationId != 0)
|
|
stop();
|
|
_duration = duration;
|
|
_scheduleTick();
|
|
}
|
|
|
|
void stop() {
|
|
sky.window.cancelAnimationFrame(_animationId);
|
|
_startTime = 0.0;
|
|
_duration = 0.0;
|
|
_animationId = 0;
|
|
}
|
|
|
|
void _scheduleTick() {
|
|
assert(_animationId == 0);
|
|
_animationId = sky.window.requestAnimationFrame(_tick);
|
|
}
|
|
|
|
void _tick(double timeStamp) {
|
|
_animationId = 0;
|
|
if (_startTime == 0.0)
|
|
_startTime = timeStamp;
|
|
double elapsedTime = timeStamp - _startTime;
|
|
double t = math.max(0.0, math.min(1.0, elapsedTime / _duration));
|
|
if (t < 1.0)
|
|
_scheduleTick();
|
|
else
|
|
stop();
|
|
_delegate.updateAnimation(t);
|
|
}
|
|
}
|