Animacja poklatkowa z gifów
Gdy mamy klatki animacji w postaci np. gifów zamiast szukać programu do utworzenia animowanego gifa – możemy przygotować animację z użyciem klasy Animation
Tutaj zakładamy, że każda klatka animacji wyświetla się przez ten sam czas, co można łatwo zmienić umieszczając odpowiednie wartości Duration>
w obiektach klasy KeyFrame
.
Klasa Listing14c_05
Ściągnij klasę Listing14c_05.zip
package rozdzial14c; import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.StackPane; import javafx.stage.Stage; import javafx.util.Duration; import java.io.FileInputStream; import java.io.FileNotFoundException; public class Listing14c_05 extends Application { private Image gif1, gif2, gif3, gif4, gif5, gif6, gif7, gif8, gif9, gif10, gif11, gif12, gif13; public static void main(String[] args) { Application.launch(args); } @Override public void init() { try { gif1 = new Image(new FileInputStream( "rozdzial14c/src/rozdzial14c/zaba-smut_01.gif")); gif2 = new Image(new FileInputStream( "rozdzial14c/src/rozdzial14c/zaba-smut_02.gif")); gif3 = new Image(new FileInputStream( "rozdzial14c/src/rozdzial14c/zaba-smut_03.gif")); gif4 = new Image(new FileInputStream( "rozdzial14c/src/rozdzial14c/zaba-smut_04.gif")); gif5 = new Image(new FileInputStream( "rozdzial14c/src/rozdzial14c/zaba-smut_05.gif")); gif6 = new Image(new FileInputStream( "rozdzial14c/src/rozdzial14c/zaba-smut_06.gif")); gif7 = new Image(new FileInputStream( "rozdzial14c/src/rozdzial14c/zaba-smut_07.gif")); gif8 = new Image(new FileInputStream( "rozdzial14c/src/rozdzial14c/zaba-smut_08.gif")); gif9 = new Image(new FileInputStream( "rozdzial14c/src/rozdzial14c/zaba-smut_09.gif")); gif10 = new Image(new FileInputStream( "rozdzial14c/src/rozdzial14c/zaba-smut_10.gif")); gif11 = new Image(new FileInputStream( "rozdzial14c/src/rozdzial14c/zaba-smut_11.gif")); gif12 = new Image(new FileInputStream( "rozdzial14c/src/rozdzial14c/zaba-smut_12.gif")); gif13 = new Image(new FileInputStream( "rozdzial14c/src/rozdzial14c/zaba-smut_13.gif")); } catch (FileNotFoundException e) { e.printStackTrace(); } } @Override public void start(Stage stage) { try { int x = 120; StackPane root = new StackPane(); ImageView iv = new ImageView(gif1); root.getChildren().add(iv); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); //- KeyValue kv1 = new KeyValue(iv.imageProperty(), gif1); KeyFrame kf1 = new KeyFrame(Duration.millis(0), kv1); //- KeyValue kv2 = new KeyValue(iv.imageProperty(), gif2); KeyFrame kf2 = new KeyFrame(Duration.millis(x), kv2); //- KeyValue kv3 = new KeyValue(iv.imageProperty(), gif3); KeyFrame kf3 = new KeyFrame(Duration.millis(2 * x), kv3); //- KeyValue kv4 = new KeyValue(iv.imageProperty(), gif4); KeyFrame kf4 = new KeyFrame(Duration.millis(3 * x), kv4); //- KeyValue kv5 = new KeyValue(iv.imageProperty(), gif5); KeyFrame kf5 = new KeyFrame(Duration.millis(4 * x), kv5); //- KeyValue kv6 = new KeyValue(iv.imageProperty(), gif6); KeyFrame kf6 = new KeyFrame(Duration.millis(5 * x), kv6); //- KeyValue kv7 = new KeyValue(iv.imageProperty(), gif7); KeyFrame kf7 = new KeyFrame(Duration.millis(6 * x), kv7); //- KeyValue kv8 = new KeyValue(iv.imageProperty(), gif8); KeyFrame kf8 = new KeyFrame(Duration.millis(7 * x), kv8); //- KeyValue kv9 = new KeyValue(iv.imageProperty(), gif9); KeyFrame kf9 = new KeyFrame(Duration.millis(8 * x), kv9); //- KeyValue kv10 = new KeyValue(iv.imageProperty(), gif10); KeyFrame kf10 = new KeyFrame(Duration.millis(9 * x), kv10); //- KeyValue kv11 = new KeyValue(iv.imageProperty(), gif11); KeyFrame kf11 = new KeyFrame(Duration.millis(10 * x), kv11); //- KeyValue kv12 = new KeyValue(iv.imageProperty(), gif12); KeyFrame kf12 = new KeyFrame(Duration.millis(11 * x), kv12); //- KeyValue kv13 = new KeyValue(iv.imageProperty(), gif13); KeyFrame kf13 = new KeyFrame(Duration.millis(12 * x), kv13); //- Timeline timeline = new Timeline(); timeline.getKeyFrames().addAll(kf1, kf2, kf3, kf4, kf5, kf6, kf7, kf8, kf9, kf10, kf11, kf12, kf13); timeline.setCycleCount(Animation.INDEFINITE); timeline.play(); } catch (Exception e) { e.printStackTrace(); } } }
Wynik
Gdy uruchomimy klasę zobaczymy odtwarzaną w pętli animację.