Facebook 开源了个 iOS&OSX 的动画库Pop,里面定义了很多动画效果,在 Android 上实现了类似 Pop 中 Spring 效果的库名叫Rebound,移植于Rebound-JS

Rebound 项目地址:https://github.com/facebook/rebound.git

  • rebound-core 使用 java 实现的一个引擎库。
  • rebound-android 结合 Android 的一些 API,封装 rebound-core 的实现。
  • rebound-android-example 一个很简单的图片缩放例子。
  • rebound-android-playground 里面包含了所有的例子教程。

rebound-android 依赖于 rebound-core,接合 Android 的 API 与 SpringSystem 连接,Android Displayer > SpringSystem.loop(),Android Jelly Bean 引入黄油计划,使用 Choreographer 统一管理视图刷新。


Spring 特效的插值器算法位于 Spring.advance(double realDeltaTime)


SpringListener 是 Spring 的回调
通过 Spring.getCurrentValue()可以拿到插值,来变换视图的位置。

基本使用

初始化SpringSystem和SpringListener
1
2
3
4
...
private final BaseSpringSystem mSpringSystem = SpringSystem.create();
private final ExampleSpringListener mSpringListener = new ExampleSpringListener();
...

通过SpringSystem创建一个Spring
1
mScaleSpring = mSpringSystem.createSpring();

设置动画的endValue
1
mScaleSpring.setEndValue(1);

在SpringListener中改变视图
1
2
3
4
5
6
7
8
private class ExampleSpringListener extends SimpleSpringListener {
@Override
public void onSpringUpdate(Spring spring) {
float mappedValue = (float) SpringUtil.mapValueFromRangeToRange(spring.getCurrentValue(), 0, 1, 1, 0.5);
mImageView.setScaleX(mappedValue);
mImageView.setScaleY(mappedValue);
}
}

在界面的生命周期中,不要忘了增加和删除SpringListener
1
2
3
4
5
6
7
8
9
10
11
@Override
public void onResume() {
super.onResume();
mScaleSpring.addListener(mSpringListener);
}

@Override
public void onPause() {
super.onPause();
mScaleSpring.removeListener(mSpringListener);
}

高级使用

使用origami可视化编辑动画效果。