forked from iamgio/animated
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnimatedButtonTest.java
More file actions
77 lines (61 loc) Β· 3.01 KB
/
AnimatedButtonTest.java
File metadata and controls
77 lines (61 loc) Β· 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package eu.iamgio.animatedtest;
import eu.iamgio.animated.Animated;
import eu.iamgio.animated.Curve;
import eu.iamgio.animated.property.PropertyWrapper;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import static eu.iamgio.animatedtest.TestUtil.center;
// This demo animates a button's background color and border radius on pressed and hover events.
public class AnimatedButtonTest extends Application {
private static final Color COLOR_DEFAULT = Color.TRANSPARENT;
private static final Color COLOR_HOVER = new Color(1, 1, 1, .5);
private static final Color COLOR_PRESSED = new Color(1, 1, 1, .8);
private static final double BASE_BACKGROUND_RADIUS = 60;
private static final double BORDER_WIDTH = 4;
public void start(Stage primaryStage) {
// Setup scene
Pane root = new Pane();
Scene scene = new Scene(root, 650, 500);
scene.getStylesheets().add("/themes/button.css");
// Create button
Button button = new Button("Animated button!");
center(button, scene);
// Initialize and bind the background color property
SimpleObjectProperty<Color> backgroundColorProperty = new SimpleObjectProperty<>(COLOR_DEFAULT);
button.backgroundProperty().bind(Bindings.createObjectBinding(
() -> {
// The button's background/border radius depends on the color opacity
double radius = backgroundColorProperty.getValue().getOpacity() * BASE_BACKGROUND_RADIUS + 16;
button.setStyle("-fx-border-radius: " + String.format("%f", radius)); // Format to avoid exponential notations
// Bind our new background
return new Background(new BackgroundFill(backgroundColorProperty.getValue(), new CornerRadii(radius), null));
},
backgroundColorProperty // Binding target
));
// Set listeners
button.hoverProperty().addListener((observable, oldValue, isHover) ->
backgroundColorProperty.set(isHover ? COLOR_HOVER : COLOR_DEFAULT)
);
button.pressedProperty().addListener((observable, oldValue, isPressed) ->
backgroundColorProperty.set(isPressed ? COLOR_PRESSED : button.isHover() ? COLOR_HOVER : COLOR_DEFAULT)
);
// Setup animation
Animated<Color> animated = new Animated<>(button,
PropertyWrapper.of(backgroundColorProperty)
).custom(settings -> settings.withCurve(Curve.EASE_OUT));
root.getChildren().add(animated);
// Show
primaryStage.setTitle("AnimatedButton");
primaryStage.setScene(scene);
primaryStage.show();
}
}