mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-05 17:30:38 +08:00
native decay animation
Summary: Add support for `useNativeDriver: true` to `Animated.decay`. Add example in Native Animated Example UIExplorer app. Reviewed By: ritzau Differential Revision: D3690127 fbshipit-source-id: eaa5e61293ed174191cec72255ea2677dbaa1757
This commit is contained in:
committed by
Facebook Github Bot 5
parent
37ab1c8844
commit
2a7f4be8f8
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.animated;
|
||||
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
|
||||
/**
|
||||
* Implementation of {@link AnimationDriver} providing support for decay animations. The
|
||||
* implementation is copied from the JS version in {@code AnimatedImplementation.js}.
|
||||
*/
|
||||
public class DecayAnimation extends AnimationDriver {
|
||||
|
||||
private final double mVelocity;
|
||||
private final double mDeceleration;
|
||||
|
||||
private long mStartFrameTimeMillis = -1;
|
||||
private double mFromValue;
|
||||
private double mLastValue;
|
||||
|
||||
public DecayAnimation(ReadableMap config) {
|
||||
mVelocity = config.getDouble("velocity");
|
||||
mDeceleration = config.getDouble("deceleration");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runAnimationStep(long frameTimeNanos) {
|
||||
long frameTimeMillis = frameTimeNanos / 1000000;
|
||||
if (mStartFrameTimeMillis == -1) {
|
||||
// since this is the first animation step, consider the start to be on the previous frame
|
||||
mStartFrameTimeMillis = frameTimeMillis - 16;
|
||||
mFromValue = mAnimatedValue.mValue;
|
||||
mLastValue = mAnimatedValue.mValue;
|
||||
}
|
||||
|
||||
final double value = mFromValue +
|
||||
(mVelocity / (1 - mDeceleration)) *
|
||||
(1 - Math.exp(-(1 - mDeceleration) * (frameTimeMillis - mStartFrameTimeMillis)));
|
||||
|
||||
if (Math.abs(mLastValue - value) < 0.1) {
|
||||
mHasFinished = true;
|
||||
return;
|
||||
}
|
||||
|
||||
mLastValue = value;
|
||||
mAnimatedValue.mValue = value;
|
||||
}
|
||||
}
|
||||
@@ -139,6 +139,8 @@ import javax.annotation.Nullable;
|
||||
animation = new FrameBasedAnimationDriver(animationConfig);
|
||||
} else if ("spring".equals(type)) {
|
||||
animation = new SpringAnimation(animationConfig);
|
||||
} else if ("decay".equals(type)) {
|
||||
animation = new DecayAnimation(animationConfig);
|
||||
} else {
|
||||
throw new JSApplicationIllegalArgumentException("Unsupported animation type: " + type);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user