Facebook rebound模仿现实生活中的阻力和弹力的库

Rebound库实战笔记

资料

github rebound

导入

implementation 'com.facebook.rebound:rebound:0.3.8'

常用方法

Spring

Spring spring = SpringSystem.create().createSpring();
//tension(拉力)值改成100(拉力值越小,晃动越慢),friction(摩擦力)值改成1(摩擦力越小,弹动大小越明显)
spring.setSpringConfig(SpringConfig.fromOrigamiTensionAndFriction(tension, friction));
spring.addListener(new SimpleSpringListener(){})

SpringChain

//创建SpringChain对象
SpringChain springChain = SpringChain.create();
 springChain.addSpring(new SimpleSpringListener() {})

使用

弹簧效果

Demo1

    //Facebook-Rebound弹簧
    private void Two() {
        //创建SpringSystem对象
        SpringSystem springSystem = SpringSystem.create();
        //添加到弹簧系统
        Spring spring = springSystem.createSpring();
        spring.setCurrentValue(1.0f);
        //tension(拉力)值改成100(拉力值越小,晃动越慢),friction(摩擦力)值改成1(摩擦力越小,弹动大小越明显)
        spring.setSpringConfig(SpringConfig.fromOrigamiTensionAndFriction(100, 1));
        //与上面一样,回弹效果更明显,但速度慢
//        spring.setSpringConfig(new SpringConfig(100, 1));
        spring.addListener(new SimpleSpringListener() {
            @Override
            public void onSpringUpdate(Spring spring) {
                float value = (float) spring.getCurrentValue();
                float scale = 1f - (value * 0.5f);
                imageView.setScaleX(scale);
                imageView.setScaleY(scale);
            }
        });
          spring.setEndValue(0.1f);
    }

Demo2

 /**
   * 弹簧动画
   * @param v 动画View
   * @param tension 拉力系数
   * @param friction 摩擦力系数
   */
private void animateViewDirection(final View v, float from, float to, double tension,
      double friction) {
    Spring spring = SpringSystem.create().createSpring();
    spring.setCurrentValue(from);
      //tension(拉力)值改成100(拉力值越小,晃动越慢),friction(摩擦力)值改成1(摩擦力越小,弹动大小越明显)
    spring.setSpringConfig(SpringConfig.fromOrigamiTensionAndFriction(tension, friction));
    spring.addListener(new SimpleSpringListener() {
      @Override
      public void onSpringUpdate(Spring spring) {
        //根据rebound返回的值进行更新
         float value = (float) spring.getCurrentValue();
         float scale = 1f - (value * 0.5f);
        v.setTranslationY((float) spring.getCurrentValue());
      }
    });
    spring.setEndValue(to);
  }
  //setControlSpringIndex:从第几个子view开始
  springChain.setControlSpringIndex(0);
  //设置结束的位置,
  springChain.getControlSpring().setEndValue(0);

多个childView “chain “ 进行控制

    //Facebook-Rebound
    private void One() {
        ViewGroup viewGroup = (ViewGroup) findViewById(R.id.chain_layout);
        viewGroup.setVisibility(View.VISIBLE);
        //创建SpringChain对象
        SpringChain springChain = SpringChain.create();
        //获取到子控件
        int childCount = viewGroup.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View view = viewGroup.getChildAt(i);

            springChain.addSpring(new SimpleSpringListener() {
                @Override
                public void onSpringUpdate(Spring spring) {
                    float value = (float) spring.getCurrentValue();
                    //只设置沿X轴
                    view.setTranslationX(value);
                }
            });
        }

        List<Spring> springs = springChain.getAllSprings();
        for (int i = 0; i < springs.size(); i++) {
            springs.get(i).setCurrentValue(1080);
        }

        //setControlSpringIndex:从第几个子view开始
        springChain.setControlSpringIndex(0);
        //设置结束的位置,
        springChain.getControlSpring().setEndValue(0);
    }

参考