Skip to content

Commit a1ba721

Browse files
committed
使用贝塞尔曲线制作一个简单的加载动画
1 parent d099d33 commit a1ba721

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

demo/src/main/kotlin/com/icuxika/bittersweet/demo/controller/MainController.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ class MainController : Initializable {
203203
)
204204
}
205205
},
206+
Button("启动画面").apply {
207+
styleClass.add("test-button")
208+
onAction {
209+
AppView(SplashScreenController::class).show()
210+
(container.scene.window as Stage).close()
211+
}
212+
},
206213
ComboBox(FXCollections.observableArrayList(Theme.entries)).apply {
207214
valueProperty().bindBidirectional(AppResource.themeProperty())
208215
cellFactory = Callback<ListView<Theme>, ListCell<Theme>> {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.icuxika.bittersweet.demo.controller
2+
3+
import com.icuxika.bittersweet.demo.annotation.AppFXML
4+
import javafx.animation.PathTransition
5+
import javafx.animation.PauseTransition
6+
import javafx.fxml.FXML
7+
import javafx.fxml.Initializable
8+
import javafx.scene.layout.BorderPane
9+
import javafx.scene.layout.Pane
10+
import javafx.scene.layout.StackPane
11+
import javafx.scene.paint.Color
12+
import javafx.scene.shape.*
13+
import javafx.util.Duration
14+
import java.net.URL
15+
import java.util.*
16+
17+
@AppFXML(fxml = "fxml/splash-screen.fxml")
18+
class SplashScreenController : Initializable {
19+
20+
@FXML
21+
private lateinit var rootContainer: StackPane
22+
23+
@FXML
24+
private lateinit var container: BorderPane
25+
26+
private val circle = Circle(8.0, Color.DODGERBLUE)
27+
private val path = Path().apply {
28+
stroke = Color.BLACK
29+
strokeLineCap = StrokeLineCap.ROUND
30+
strokeDashArray.addAll(4.0, 4.0)
31+
strokeWidth = 2.0
32+
fill = null
33+
}
34+
35+
override fun initialize(location: URL?, resources: ResourceBundle?) {
36+
val pane = Pane()
37+
container.center = pane.apply {
38+
children.addAll(circle, path)
39+
}
40+
41+
val pathTransition = PathTransition().apply {
42+
node = circle
43+
duration = Duration.seconds(3.0)
44+
cycleCount = PathTransition.INDEFINITE
45+
isAutoReverse = false
46+
orientation = PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT
47+
}
48+
49+
val pauseTransition = PauseTransition()
50+
pauseTransition.setOnFinished {
51+
pathTransition.path = path
52+
pathTransition.play()
53+
}
54+
55+
pane.layoutBoundsProperty().addListener { _, _, newValue ->
56+
if (newValue.width > 0 && newValue.height > 0) {
57+
pathTransition.stop()
58+
path.elements.clear()
59+
path.elements.addAll(
60+
MoveTo(newValue.width / 2, newValue.height / 2),
61+
CubicCurveTo(
62+
0.0, 0.0,
63+
0.0, newValue.height,
64+
newValue.width / 2, newValue.height / 2
65+
),
66+
CubicCurveTo(
67+
newValue.width, 0.0,
68+
newValue.width, newValue.height,
69+
newValue.width / 2, newValue.height / 2
70+
),
71+
)
72+
pauseTransition.playFromStart()
73+
}
74+
}
75+
}
76+
77+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.scene.layout.BorderPane?>
4+
<?import javafx.scene.layout.StackPane?>
5+
<StackPane fx:id="rootContainer" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
6+
prefHeight="800.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1"
7+
fx:controller="com.icuxika.bittersweet.demo.controller.SplashScreenController">
8+
<children>
9+
<BorderPane fx:id="container"/>
10+
</children>
11+
</StackPane>

0 commit comments

Comments
 (0)