dynamic-animation/src/androidTest/java/androidx/dynamicanimation/tests/SpringTests.java
@LargeTest
    @Test
    public void testStiffness() {
        float[] dampingRatios = {0.3f, 0.5f, 1f, 5f};
        final float[] stiffness = {50f, 500f, 1500f, 5000f};
        DynamicAnimation.ViewProperty[] viewProperties =
                {DynamicAnimation.SCROLL_X, DynamicAnimation.TRANSLATION_X,
                        DynamicAnimation.TRANSLATION_Y, DynamicAnimation.TRANSLATION_Z};
        assertEquals(viewProperties.length, stiffness.length);

        final SpringAnimation[] springAnims = new SpringAnimation[stiffness.length];
        SpringForce[] springs = new SpringForce[stiffness.length];
        MyEndListener[] listeners = new MyEndListener[stiffness.length];

        // Sets stiffness
        for (int i = 0; i < stiffness.length; i++) {
            springs[i] = new SpringForce(0).setStiffness(stiffness[i]);
            listeners[i] = new MyEndListener();
            springAnims[i] = new SpringAnimation(mView1, viewProperties[i]).setSpring(springs[i])
                    .addEndListener(listeners[i]);
        }

        for (int i = 0; i < dampingRatios.length; i++) {
            for (int j = 0; j < stiffness.length; j++) {
                springs[j].setDampingRatio(dampingRatios[i]);
                springAnims[j].setStartValue(0).setStartVelocity(500);
                listeners[j].endTime = -1;
            }
            DynamicAnimation.OnAnimationEndListener mockListener = mock(
                    DynamicAnimation.OnAnimationEndListener.class);
            springAnims[1].addEndListener(mockListener);
            InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < stiffness.length; j++) {
                        springAnims[j].start();
                    }
                }
            });

            verify(mockListener, timeout(2000)).onAnimationEnd(springAnims[1], false, 0f, 0f);

            if (springAnims[0].isRunning()) {
                InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
                    @Override
                    public void run() {
                        springAnims[0].cancel();
                    }
                });
            }
            for (int j = 1; j < stiffness.length; j++) {
                // The stiffer spring should finish no later than the less stiff spring.
                assertTrue(listeners[j - 1].endTime > listeners[j].endTime);
            }
        }
    }
/**
     * Verify that stiffer spring animations finish sooner than less stiff spring animations. Run
     * the same verification on different damping ratios.
     */
Verify that stiffer spring animations finish sooner than less stiff spring animations. Run
the same verification on different damping ratios.
Verify that stiffer spring animations finish sooner than less stiff spring animations . Run the same verification on different damping ratios .
@LargeTest
    @Test
    public void testStiffness() {
        float[] dampingRatios = {0.3f, 0.5f, 1f, 5f};
        final float[] stiffness = {50f, 500f, 1500f, 5000f};
        DynamicAnimation.ViewProperty[] viewProperties =
                {DynamicAnimation.SCROLL_X, DynamicAnimation.TRANSLATION_X,
                        DynamicAnimation.TRANSLATION_Y, DynamicAnimation.TRANSLATION_Z};
        assertEquals(viewProperties.length, stiffness.length);
        final SpringAnimation[] springAnims = new SpringAnimation[stiffness.length];
        SpringForce[] springs = new SpringForce[stiffness.length];
        MyEndListener[] listeners = new MyEndListener[stiffness.length];
        for (int i = 0; i < stiffness.length; i++) {
            springs[i] = new SpringForce(0).setStiffness(stiffness[i]);
            listeners[i] = new MyEndListener();
            springAnims[i] = new SpringAnimation(mView1, viewProperties[i]).setSpring(springs[i])
                    .addEndListener(listeners[i]);
        }
        for (int i = 0; i < dampingRatios.length; i++) {
            for (int j = 0; j < stiffness.length; j++) {
                springs[j].setDampingRatio(dampingRatios[i]);
                springAnims[j].setStartValue(0).setStartVelocity(500);
                listeners[j].endTime = -1;
            }
            DynamicAnimation.OnAnimationEndListener mockListener = mock(
                    DynamicAnimation.OnAnimationEndListener.class);
            springAnims[1].addEndListener(mockListener);
            InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < stiffness.length; j++) {
                        springAnims[j].start();
                    }
                }
            });
            verify(mockListener, timeout(2000)).onAnimationEnd(springAnims[1], false, 0f, 0f);
            if (springAnims[0].isRunning()) {
                InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
                    @Override
                    public void run() {
                        springAnims[0].cancel();
                    }
                });
            }
            for (int j = 1; j < stiffness.length; j++) {
                assertTrue(listeners[j - 1].endTime > listeners[j].endTime);
            }
        }
    }
@ LargeTest @ Test public void testStiffness ( ) { float [ ] dampingRatios = { 0.3f , 0.5f , 1f , 5f } ; final float [ ] stiffness = { 50f , 500f , 1500f , 5000f } ; DynamicAnimation . ViewProperty [ ] viewProperties = { DynamicAnimation . SCROLL_X , DynamicAnimation . TRANSLATION_X , DynamicAnimation . TRANSLATION_Y , DynamicAnimation . TRANSLATION_Z } ; assertEquals ( viewProperties . length , stiffness . length ) ; final SpringAnimation [ ] springAnims = new SpringAnimation [ stiffness . length ] ; SpringForce [ ] springs = new SpringForce [ stiffness . length ] ; MyEndListener [ ] listeners = new MyEndListener [ stiffness . length ] ; for ( int i = 0 ; i < stiffness . length ; i ++ ) { springs [ i ] = new SpringForce ( 0 ) . setStiffness ( stiffness [ i ] ) ; listeners [ i ] = new MyEndListener ( ) ; springAnims [ i ] = new SpringAnimation ( mView1 , viewProperties [ i ] ) . setSpring ( springs [ i ] ) . addEndListener ( listeners [ i ] ) ; } for ( int i = 0 ; i < dampingRatios . length ; i ++ ) { for ( int j = 0 ; j < stiffness . length ; j ++ ) { springs [ j ] . setDampingRatio ( dampingRatios [ i ] ) ; springAnims [ j ] . setStartValue ( 0 ) . setStartVelocity ( 500 ) ; listeners [ j ] . endTime = - 1 ; } DynamicAnimation . OnAnimationEndListener mockListener = mock ( DynamicAnimation . OnAnimationEndListener . class ) ; springAnims [ 1 ] . addEndListener ( mockListener ) ; InstrumentationRegistry . getInstrumentation ( ) . runOnMainSync ( new Runnable ( ) { @ Override public void run ( ) { for ( int j = 0 ; j < stiffness . length ; j ++ ) { springAnims [ j ] . start ( ) ; } } } ) ; verify ( mockListener , timeout ( 2000 ) ) . onAnimationEnd ( springAnims [ 1 ] , false , 0f , 0f ) ; if ( springAnims [ 0 ] . isRunning ( ) ) { InstrumentationRegistry . getInstrumentation ( ) . runOnMainSync ( new Runnable ( ) { @ Override public void run ( ) { springAnims [ 0 ] . cancel ( ) ; } } ) ; } for ( int j = 1 ; j < stiffness . length ; j ++ ) { assertTrue ( listeners [ j - 1 ] . endTime > listeners [ j ] . endTime ) ; } } }
Verify that stiffer spring animations finish sooner than less stiff spring animations.
Verify that stiffer spring animations finish sooner than less stiff spring animations .
// Sets stiffness // The stiffer spring should finish no later than the less stiff spring.