Animator: added constructor that allows passing a runnable that is invoked at the end of the animation, which allows using lambdas in most cases

This commit is contained in:
Karl Tauber
2020-07-25 10:53:06 +02:00
parent 9f41ec3986
commit 5a9e620c17
3 changed files with 33 additions and 22 deletions

View File

@@ -31,6 +31,7 @@ public class Animator
private int resolution = 10; private int resolution = 10;
private Interpolator interpolator; private Interpolator interpolator;
private final ArrayList<TimingTarget> targets = new ArrayList<>(); private final ArrayList<TimingTarget> targets = new ArrayList<>();
private final Runnable endRunnable;
private boolean running; private boolean running;
private boolean hasBegun; private boolean hasBegun;
@@ -46,7 +47,7 @@ public class Animator
* @param duration the duration of the animation in milliseconds * @param duration the duration of the animation in milliseconds
*/ */
public Animator( int duration ) { public Animator( int duration ) {
this( duration, null ); this( duration, null, null );
} }
/** /**
@@ -57,8 +58,21 @@ public class Animator
* @param target the target that receives timing events * @param target the target that receives timing events
*/ */
public Animator( int duration, TimingTarget target ) { public Animator( int duration, TimingTarget target ) {
this( duration, target, null );
}
/**
* Creates an animation that runs duration milliseconds.
* Use {@link #start()} to start the animation.
*
* @param duration the duration of the animation in milliseconds
* @param target the target that receives timing events
* @param endRunnable a runnable invoked when the animation ends; or {@code null}
*/
public Animator( int duration, TimingTarget target, Runnable endRunnable ) {
setDuration( duration ); setDuration( duration );
addTarget( target ); addTarget( target );
this.endRunnable = endRunnable;
} }
/** /**
@@ -249,6 +263,9 @@ public class Animator
for( TimingTarget target : targets ) for( TimingTarget target : targets )
target.end(); target.end();
} }
if( endRunnable != null )
endRunnable.run();
} }
private void throwExceptionIfRunning() { private void throwExceptionIfRunning() {

View File

@@ -115,9 +115,7 @@ public class FlatAnimatedLafChange
return; return;
// create animator // create animator
animator = new Animator( duration, new Animator.TimingTarget() { animator = new Animator( duration, fraction -> {
@Override
public void timingEvent( float fraction ) {
if( fraction < 0.1 || fraction > 0.9 ) if( fraction < 0.1 || fraction > 0.9 )
return; // ignore initial and last events return; // ignore initial and last events
@@ -128,13 +126,9 @@ public class FlatAnimatedLafChange
if( e.getKey().isShowing() ) if( e.getKey().isShowing() )
e.getValue().repaint(); e.getValue().repaint();
} }
} }, () -> {
@Override
public void end() {
hideSnapshot(); hideSnapshot();
animator = null; animator = null;
}
} ); } );
animator.setResolution( resolution ); animator.setResolution( resolution );

View File

@@ -52,8 +52,8 @@ public class FlatAnimatorTest
linearAnimator.stop(); linearAnimator.stop();
linearAnimator.start(); linearAnimator.start();
} else { } else {
linearAnimator = new Animator( 1000, t -> { linearAnimator = new Animator( 1000, fraction -> {
linearScrollBar.setValue( Math.round( t * linearScrollBar.getMaximum() ) ); linearScrollBar.setValue( Math.round( fraction * linearScrollBar.getMaximum() ) );
} ); } );
linearAnimator.start(); linearAnimator.start();
} }
@@ -64,8 +64,8 @@ public class FlatAnimatorTest
easeInOutAnimator.stop(); easeInOutAnimator.stop();
easeInOutAnimator.start(); easeInOutAnimator.start();
} else { } else {
easeInOutAnimator = new Animator( 1000, t -> { easeInOutAnimator = new Animator( 1000, fraction -> {
easeInOutScrollBar.setValue( Math.round( t * easeInOutScrollBar.getMaximum() ) ); easeInOutScrollBar.setValue( Math.round( fraction * easeInOutScrollBar.getMaximum() ) );
} ); } );
easeInOutAnimator.setInterpolator( CubicBezierEasing.EASE_IN_OUT ); easeInOutAnimator.setInterpolator( CubicBezierEasing.EASE_IN_OUT );
easeInOutAnimator.start(); easeInOutAnimator.start();