我的程序中有两个动画,在一个阶段中逐个更改窗格
当我尝试这样做时
package sample;
import javafx.animation.FadeTransition;
import javafx.animation.PathTransition;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.shape.Line;
import java.io.IOException;
import java.net.URL;
import java.time.Duration;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private BorderPane BP;
public static boolean main = true;
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
if(main){
isSplashed();
}
mainButtonSplash();
} catch (IOException e1) {
e1.printStackTrace();
}
}
@FXML
private Button ButtonID;
@FXML
void ONButton(ActionEvent event) {
}
private void isSplashed() throws IOException {
main=false;
BorderPane bps = FXMLLoader.load(getClass().getResource("SPLASH.fxml"));
BP.getChildren().setAll(bps);
FadeTransition fdin = new FadeTransition((javafx.util.Duration.seconds(3)), bps);
fdin.setFromValue(0);
fdin.setToValue(1);
fdin.play();
FadeTransition fadeTransition = new FadeTransition(javafx.util.Duration.seconds(3),bps);
fadeTransition.setToValue(0);
fdin.setOnFinished(e -> {
fadeTransition.play();
});
fadeTransition.setOnFinished(e->{
try {
BorderPane bpss = FXMLLoader.load(getClass().getResource("sample.fxml"));
BP.getChildren().setAll(bpss);
} catch (IOException e1) {
e1.printStackTrace();
}
});
}
public void mainButtonSplash(){
PathTransition pathTransition = new PathTransition();
pathTransition.setDuration(javafx.util.Duration.seconds(2));
pathTransition.setNode(ButtonID);
pathTransition.setPath(new Line(0,0,200,0));
FadeTransition fadeTransition = new FadeTransition(javafx.util.Duration.seconds(1),ButtonID);
fadeTransition.setFromValue(0);
fadeTransition.setToValue(1);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
pathTransition.play();
fadeTransition.play();
}
});
thread.start();
}
}
然后按下按钮上的mainbuttonflash方法动画在开始时轻弹(对我来说,它看起来像那个按钮,然后消失然后动画开始)。
但是当我在issplashed方法之前直接启动mainbuttonflash方法时,它可以正常工作
package sample;
import javafx.animation.FadeTransition;
import javafx.animation.PathTransition;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.shape.Line;
import java.io.IOException;
import java.net.URL;
import java.time.Duration;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private BorderPane BP;
public static boolean main = true;
@Override
public void initialize(URL location, ResourceBundle resources) {
// try {
// if(main){
// isSplashed();
// }
mainButtonSplash();
// } catch (IOException e1) {
// e1.printStackTrace();
// }
}
@FXML
private Button ButtonID;
@FXML
void ONButton(ActionEvent event) {
}
private void isSplashed() throws IOException {
main=false;
BorderPane bps = FXMLLoader.load(getClass().getResource("SPLASH.fxml"));
BP.getChildren().setAll(bps);
FadeTransition fdin = new FadeTransition((javafx.util.Duration.seconds(3)), bps);
fdin.setFromValue(0);
fdin.setToValue(1);
fdin.play();
FadeTransition fadeTransition = new FadeTransition(javafx.util.Duration.seconds(3),bps);
fadeTransition.setToValue(0);
fdin.setOnFinished(e -> {
fadeTransition.play();
});
fadeTransition.setOnFinished(e->{
try {
BorderPane bpss = FXMLLoader.load(getClass().getResource("sample.fxml"));
BP.getChildren().setAll(bpss);
} catch (IOException e1) {
e1.printStackTrace();
}
});
}
public void mainButtonSplash(){
PathTransition pathTransition = new PathTransition();
pathTransition.setDuration(javafx.util.Duration.seconds(2));
pathTransition.setNode(ButtonID);
pathTransition.setPath(new Line(0,0,200,0));
FadeTransition fadeTransition = new FadeTransition(javafx.util.Duration.seconds(1),ButtonID);
fadeTransition.setFromValue(0);
fadeTransition.setToValue(1);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
pathTransition.play();
fadeTransition.play();
}
});
thread.start();
}
}
我希望两个动画一个接一个地顺利运行!
我试图搜索但没有任何帮助!
答案 0 :(得分:2)
Thread
方法中使用单独的mainButtonSplash()
来启动路径和淡入淡出过渡?这没有任何意义,只需在FX Thread
中启动它们即可。 ParallelTransition
和SequentialTransition
来纠正处理动画。使用Timeline
更容易实现动画,但如果需要转换,请尝试使用此修复代码。
@FXML
private BorderPane BP;
@FXML
private Button ButtonID;
public static boolean main = true;
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
final ParallelTransition mainButtonSplashAnimation = getMainButtonSplashAnimation();
if (main) {
final SequentialTransition splashedAnimation = getSplashedAnimation();
SequentialTransition st = new SequentialTransition(splashedAnimation, mainButtonSplashAnimation);
st.play();
} else {
mainButtonSplashAnimation.play();
}
} catch (IOException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
@FXML
void ONButton(ActionEvent event) {
}
private SequentialTransition getSplashedAnimation() throws IOException {
main = false;
BorderPane bps = FXMLLoader.load(getClass().getResource("SPLASH.fxml"));
BP.getChildren().setAll(bps);
FadeTransition fdin = new FadeTransition((javafx.util.Duration.seconds(3)), bps);
fdin.setFromValue(0);
fdin.setToValue(1);
FadeTransition fadeTransition = new FadeTransition(javafx.util.Duration.seconds(3), bps);
fadeTransition.setToValue(0);
SequentialTransition st = new SequentialTransition(fdin, fadeTransition);
st.setOnFinished(e -> {
try {
BorderPane bpss = FXMLLoader.load(getClass().getResource("sample.fxml"));
BP.getChildren().setAll(bpss);
} catch (IOException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
});
return st;
}
public ParallelTransition getMainButtonSplashAnimation() {
PathTransition pathTransition = new PathTransition();
pathTransition.setDuration(javafx.util.Duration.seconds(2));
pathTransition.setNode(ButtonID);
pathTransition.setPath(new Line(0, 0, 200, 0));
FadeTransition fadeTransition = new FadeTransition(javafx.util.Duration.seconds(1), ButtonID);
fadeTransition.setFromValue(0);
fadeTransition.setToValue(1);
ParallelTransition pt = new ParallelTransition(pathTransition, fadeTransition);
return pt;
}