我想用JAVAFX 做一个简单GIF小动画,就是几张图片的转换,但我这样做一直错,不知道错哪里?暂时也没办法做出来?
我想用JAVAFX 做一个简单GIF小动画,就是几张图片的转换,但我这样做一直错,不知道错哪里?暂时也没办法做出来?
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
import javafx.util.Duration;
import javafx.scene.layout.StackPane;
import javafx.animation.FadeTransition;
import javafx.animation.Timeline;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.Pane;
import javafx.scene.image.ImageView;
import java.util.Timer;
import java.util.TimerTask;
public class Cartoon extends Application{
@Override
public void start(Stage stage){
Pane pane=new StackPane();
Image image1=new Image("image/111.jpg");
ImageView imageView1=new ImageView(image1);
imageView1.fitHeightProperty().bind(pane.heightProperty().divide(1));
imageView1.fitWidthProperty().bind(pane.widthProperty().divide(1));
Image image2=new Image("image/222.jpg");
ImageView imageView2=new ImageView(image2);
imageView2.fitHeightProperty().bind(pane.heightProperty().divide(1));
imageView2.fitWidthProperty().bind(pane.widthProperty().divide(1));
Image image3=new Image("image/333.jpg");
ImageView imageView3=new ImageView(image3);
imageView3.fitHeightProperty().bind(pane.heightProperty().divide(1));
imageView3.fitWidthProperty().bind(pane.widthProperty().divide(1));
Image image4=new Image("image/444.jpg");
ImageView imageView4=new ImageView(image4);
imageView4.fitHeightProperty().bind(pane.heightProperty().divide(1));
imageView4.fitWidthProperty().bind(pane.widthProperty().divide(1));
//----------------------------------------------------------------------
ImageView[] imageView={imageView1,imageView2,imageView3,imageView4};
CartoonPane cartoonPane=new CartoonPane(imageView);
//----------------------------------------------------------------------
FadeTransition ft1=new FadeTransition(Duration.millis(3000),imageView1);
ft1.setCycleCount(Timeline.INDEFINITE);
ft1.setFromValue(1.0);
ft1.setToValue(0.1);
ft1.setAutoReverse(true);
ft1.play();
FadeTransition ft2=new FadeTransition(Duration.millis(3000),imageView2);
ft2.setCycleCount(Timeline.INDEFINITE);
ft2.setFromValue(1.0);
ft2.setToValue(0.1);
ft2.setAutoReverse(true);
ft2.play();
FadeTransition ft3=new FadeTransition(Duration.millis(3000),imageView3);
ft3.setCycleCount(Timeline.INDEFINITE);
ft3.setFromValue(1.0);
ft3.setToValue(0.1);
ft3.setAutoReverse(true);
ft3.play();
FadeTransition ft4=new FadeTransition(Duration.millis(3000),imageView4);
ft4.setCycleCount(Timeline.INDEFINITE);
ft4.setFromValue(1.0);
ft4.setToValue(0.1);
ft4.setAutoReverse(true);
ft4.play();
pane.getChildren().add(cartoonPane);
Scene scene=new Scene(pane,550,300);
stage.setScene(scene);
stage.setTitle("动画");
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
//下面是一个继承STACKPANE面板的自定义面板,主要是能传入一个图象显示数组,做成一个小动画
public class CartoonPane extends StackPane{
private Pane pane=new StackPane();
private ImageView[] imageView;
public CartoonPane(ImageView[] imageView){
this.imageView=imageView;
Timer timer2=new Timer();
timer2.schedule(new TimerTaskTest(imageView),0,1000);
}
//继承TimerTask,TimerTaskTest作为一个内部类
class TimerTaskTest extends TimerTask{
ImageView[] imageView1;
int i;
public TimerTaskTest(ImageView[] imageView1){
this.imageView1=imageView1;
this.i=0;
}
@Override
public void run(){
//Date date1=new Date(this.scheduledExecutionTime());
getChildren().clear();
getChildren().add(this.imageView1[this.i++]);
if(this.i>=4)//超过4,就令i变为0
this.i=0;
}
}
}