Rebound - Spring Animations for Android 小结

来源:互联网 发布:起床战争服务器端口 编辑:程序博客网 时间:2024/05/16 00:46
1、综述
Rebound 通过胡克定律,实现的一个类似“弹簧”动画效果的第三方工具包。
项目来源:
http://facebook.github.io/rebound/


2、应用及简析

Rebound主要提供了SpringSystem和Spring两个类,可用来实现动画;可以通过设置Spring的SpringConfig控制摩擦力(friction)、张力(tension),来控制反弹的幅度;通过实现Spring的SpringListener监听来实现最终动画效果。一般实现onSpringUpdate方法,使用通过胡适定律转换来的参数Spring的PhysicsState的position(取值为[0,1])实现动画。

示例:

SpringSystem springSys = SpringSystem.create();final Spring spring = springSys.createSpring();final float maxScale = 0.1f;spring.addListener(new SimpleSpringListener(){@Overridepublic void onSpringUpdate(Spring spring){super.onSpringUpdate(spring);float value = (float) spring.getCurrentValue();float scale = 1f - (value * maxScale);//ViewHelper是nineoldandroids的一个动画兼容类//可参阅:http://nineoldandroids.com/ViewHelper.setScaleX(mAnimView, scale);ViewHelper.setScaleY(mAnimView, scale);}});mAnimView.setOnTouchListener(new OnTouchListener(){@Overridepublic boolean onTouch(View v, MotionEvent event){int key = event.getAction();switch(key){case MotionEvent.ACTION_DOWN:spring.setEndValue(1.0);break;case MotionEvent.ACIOTN_UP:case MotionEvent.ACTION_CANCEL:spring.setEndValue(0.0);break;default:break;}return true;}});

3、困惑
写的这些东西都是最基本的使用,至于Round的核心部分并未详解。
(1) SpringSystem 和 Spring 为什么要使用ReentrantCallback<SpringListener>集合而不是直接使用一个简单的对象,有啥好处?
(2) Choreographer扮演了一个什么角色?
(3) 如何理解Spring中的PhysicsState的position和velocity,以及mStartValue、mEndValue?

4、附:

/** * <p> * <li>Because Java doesn't permit additions/removals to a list while iterating, * a callback cannot remove itself when it is being called.</br> *  * 由于java不支持对一个正在迭代的集合进行添加或删除操作,一个回调不可以删除它本身,当它正被调用的时候.</br></br> *  * <li>This class makes it safe for Callbacks to remove themselves. Also, it is * thread-safe.</br> *  * 这个类确保回调删除它们自己是安全的.当然,它是线程安全的.</br></br> *  * <li>Implementation detail: This class works by storing an inner set that will * be modified by the calls to add/removeListener. When getListeners() or * iterator() is called, an immutable copy from the inner set is returned. That * copy will be reused if no changes have been done to the set since last * request for it.</br></br> *  * 实现详解: *  * 如果自从上次请求后这个set没有进行任何改变,那么这个copy将被重用 * </p> */public class ReentrantCallback


关于Choreographer,可参见:http://blog.csdn.net/innost/article/details/8272867

  • Choreographer是线程单例的,而且必须要和一个Looper绑定,因为其内部有一个Handler需要和Looper绑定。
  • DisplayEventReceiver是一个abstract class,其JNI的代码部分会创建一个IDisplayEventConnectionVSYNC监听者对象。这样,来自EventThreadVSYNC中断信号就可以传递给Choreographer对象了。由图8可知,当VSYNC信号到来时,DisplayEventReceiveronVsync函数将被调用。
  • 另外,DisplayEventReceiver还有一个scheduleVsync函数。当应用需要绘制UI时,将首先申请一次VSYNC中断,然后再在中断处理的onVsync函数去进行绘制。
  • Choreographer定义了一个FrameCallback interface,每当VSYNC到来时,其doFrame函数将被调用。这个接口对Android Animation的实现起了很大的帮助作用。以前都是自己控制时间,现在终于有了固定的时间中断。
  • Choreographer的主要功能是,当收到VSYNC信号时,去调用使用者通过postCallback设置的回调函数。目前一共定义了三种类型的回调,它们分别是:
  • CALLBACK_INPUT:优先级最高,和输入事件处理有关。
  • CALLBACK_ANIMATION:优先级其次,和Animation的处理有关。
  • CALLBACK_TRAVERSAL:优先级最低,和UI等控件绘制有关。