dynamic-animation/src/androidTest/java/androidx/dynamicanimation/tests/SpringTests.java
@Test
    public void testDampingRatioOverAndCriticallyDamped() {
        // Compare overdamped springs
        final SpringAnimation anim1 = new SpringAnimation(mView1, DynamicAnimation.X, 0);
        final SpringAnimation anim2 = new SpringAnimation(mView2, DynamicAnimation.Y, 0);
        final SpringAnimation anim3 = new SpringAnimation(mView2, DynamicAnimation.Z, 0);
        final DynamicAnimation.OnAnimationUpdateListener updateListener =
                new DynamicAnimation.OnAnimationUpdateListener() {
                    public float position1 = 1000;
                    public float position2 = 1000;
                    public float position3 = 1000;
                    @Override
                    public void onAnimationUpdate(DynamicAnimation animation, float value,
                            float velocity) {
                        if (animation == anim1) {
                            position1 = value;
                            if (position1 == 800) {
                                // first frame
                                assertEquals(position1, position2, 0);
                                assertEquals(position1, position3, 0);
                            } else {
                                assertTrue(position2 > position1);
                                assertTrue(position3 > position2);
                                assertTrue(800 > position3);
                            }
                        } else if (animation == anim2) {
                            position2 = value;
                        } else {
                            position3 = value;
                        }
                    }
                };
        final MyEndListener l1 = new MyEndListener();
        final MyEndListener l2 = new MyEndListener();
        final MyEndListener l3 = new MyEndListener();

        final DynamicAnimation.OnAnimationEndListener mockListener =
                mock(DynamicAnimation.OnAnimationEndListener.class);
        anim1.getSpring().setStiffness(SpringForce.STIFFNESS_HIGH).setDampingRatio(1f);
        anim2.getSpring().setStiffness(SpringForce.STIFFNESS_HIGH).setDampingRatio(1.5f);
        anim3.getSpring().setStiffness(SpringForce.STIFFNESS_HIGH).setDampingRatio(2.0f);

        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                anim2.setStartValue(800).addUpdateListener(updateListener).addEndListener(l2)
                        .start();
                anim3.setStartValue(800).addUpdateListener(updateListener).addEndListener(l3)
                        .addEndListener(mockListener).start();
                anim1.setStartValue(800).addUpdateListener(updateListener).addEndListener(l1)
                        .start();

            }
        });
        // The spring animation with critically-damped spring should return to rest position faster.
        verify(mockListener, timeout(2000)).onAnimationEnd(anim3, false, 0, 0);
        assertTrue(l1.endTime > 0);
        assertTrue(l2.endTime > l1.endTime);
        assertTrue(l3.endTime > l2.endTime);
    }
/**
     * Verify that for over-damped springs, the higher the damping ratio, the slower it is. Also
     * verify that critically damped springs finish faster than overdamped springs.
     */
Verify that for over-damped springs, the higher the damping ratio, the slower it is. Also
verify that critically damped springs finish faster than overdamped springs.
Verify that for over - damped springs the higher the damping ratio the slower it is . Also verify that critically damped springs finish faster than overdamped springs .
@Test
    public void testDampingRatioOverAndCriticallyDamped() {
        final SpringAnimation anim1 = new SpringAnimation(mView1, DynamicAnimation.X, 0);
        final SpringAnimation anim2 = new SpringAnimation(mView2, DynamicAnimation.Y, 0);
        final SpringAnimation anim3 = new SpringAnimation(mView2, DynamicAnimation.Z, 0);
        final DynamicAnimation.OnAnimationUpdateListener updateListener =
                new DynamicAnimation.OnAnimationUpdateListener() {
                    public float position1 = 1000;
                    public float position2 = 1000;
                    public float position3 = 1000;
                    @Override
                    public void onAnimationUpdate(DynamicAnimation animation, float value,
                            float velocity) {
                        if (animation == anim1) {
                            position1 = value;
                            if (position1 == 800) {
                                assertEquals(position1, position2, 0);
                                assertEquals(position1, position3, 0);
                            } else {
                                assertTrue(position2 > position1);
                                assertTrue(position3 > position2);
                                assertTrue(800 > position3);
                            }
                        } else if (animation == anim2) {
                            position2 = value;
                        } else {
                            position3 = value;
                        }
                    }
                };
        final MyEndListener l1 = new MyEndListener();
        final MyEndListener l2 = new MyEndListener();
        final MyEndListener l3 = new MyEndListener();
        final DynamicAnimation.OnAnimationEndListener mockListener =
                mock(DynamicAnimation.OnAnimationEndListener.class);
        anim1.getSpring().setStiffness(SpringForce.STIFFNESS_HIGH).setDampingRatio(1f);
        anim2.getSpring().setStiffness(SpringForce.STIFFNESS_HIGH).setDampingRatio(1.5f);
        anim3.getSpring().setStiffness(SpringForce.STIFFNESS_HIGH).setDampingRatio(2.0f);
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                anim2.setStartValue(800).addUpdateListener(updateListener).addEndListener(l2)
                        .start();
                anim3.setStartValue(800).addUpdateListener(updateListener).addEndListener(l3)
                        .addEndListener(mockListener).start();
                anim1.setStartValue(800).addUpdateListener(updateListener).addEndListener(l1)
                        .start();
            }
        });
        verify(mockListener, timeout(2000)).onAnimationEnd(anim3, false, 0, 0);
        assertTrue(l1.endTime > 0);
        assertTrue(l2.endTime > l1.endTime);
        assertTrue(l3.endTime > l2.endTime);
    }
@ Test public void testDampingRatioOverAndCriticallyDamped ( ) { final SpringAnimation anim1 = new SpringAnimation ( mView1 , DynamicAnimation . X , 0 ) ; final SpringAnimation anim2 = new SpringAnimation ( mView2 , DynamicAnimation . Y , 0 ) ; final SpringAnimation anim3 = new SpringAnimation ( mView2 , DynamicAnimation . Z , 0 ) ; final DynamicAnimation . OnAnimationUpdateListener updateListener = new DynamicAnimation . OnAnimationUpdateListener ( ) { public float position1 = 1000 ; public float position2 = 1000 ; public float position3 = 1000 ; @ Override public void onAnimationUpdate ( DynamicAnimation animation , float value , float velocity ) { if ( animation == anim1 ) { position1 = value ; if ( position1 == 800 ) { assertEquals ( position1 , position2 , 0 ) ; assertEquals ( position1 , position3 , 0 ) ; } else { assertTrue ( position2 > position1 ) ; assertTrue ( position3 > position2 ) ; assertTrue ( 800 > position3 ) ; } } else if ( animation == anim2 ) { position2 = value ; } else { position3 = value ; } } } ; final MyEndListener l1 = new MyEndListener ( ) ; final MyEndListener l2 = new MyEndListener ( ) ; final MyEndListener l3 = new MyEndListener ( ) ; final DynamicAnimation . OnAnimationEndListener mockListener = mock ( DynamicAnimation . OnAnimationEndListener . class ) ; anim1 . getSpring ( ) . setStiffness ( SpringForce . STIFFNESS_HIGH ) . setDampingRatio ( 1f ) ; anim2 . getSpring ( ) . setStiffness ( SpringForce . STIFFNESS_HIGH ) . setDampingRatio ( 1.5f ) ; anim3 . getSpring ( ) . setStiffness ( SpringForce . STIFFNESS_HIGH ) . setDampingRatio ( 2.0f ) ; InstrumentationRegistry . getInstrumentation ( ) . runOnMainSync ( new Runnable ( ) { @ Override public void run ( ) { anim2 . setStartValue ( 800 ) . addUpdateListener ( updateListener ) . addEndListener ( l2 ) . start ( ) ; anim3 . setStartValue ( 800 ) . addUpdateListener ( updateListener ) . addEndListener ( l3 ) . addEndListener ( mockListener ) . start ( ) ; anim1 . setStartValue ( 800 ) . addUpdateListener ( updateListener ) . addEndListener ( l1 ) . start ( ) ; } } ) ; verify ( mockListener , timeout ( 2000 ) ) . onAnimationEnd ( anim3 , false , 0 , 0 ) ; assertTrue ( l1 . endTime > 0 ) ; assertTrue ( l2 . endTime > l1 . endTime ) ; assertTrue ( l3 . endTime > l2 . endTime ) ; }
Verify that for over-damped springs, the higher the damping ratio, the slower it is.
Verify that for over - damped springs the higher the damping ratio the slower it is .
// Compare overdamped springs // first frame // The spring animation with critically-damped spring should return to rest position faster.