javafx scaletransition

4dbbbstv  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(330)

嗨,伙计们,我正试图添加一个卡翻转动画到我的游戏,但我得到一些例外,我不明白,我没有得到他们所有的时间,就像有时,有时他们停止过渡。输入图像描述这里是我的过渡代码。

public void transitionAnimation(ImageView imageView,Image image){
    ScaleTransition front = new ScaleTransition(Duration.seconds(0.15),imageView);
    ScaleTransition back = new ScaleTransition(Duration.seconds(0.15),imageView);

    front.setFromX(1);
    front.setToX(0);
    front.setCycleCount(1);
    front.play();

    back.setFromX(0);
    back.setToX(1);
    back.setCycleCount(1);
    back.setDelay(Duration.seconds(0.15));
    back.play();

    Timer timer = new Timer();
    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            imageView.setImage(image);
            timer.cancel();
            timer.purge();
        }
    };
    timer.schedule(timerTask,148);
}
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 180
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex(Objects.java:359)
    at java.base/java.util.ArrayList.get(ArrayList.java:427)
    at javafx.graphics/javafx.scene.Parent.updateCachedBounds(Parent.java:1701)
    at javafx.graphics/javafx.scene.Parent.recomputeBounds(Parent.java:1645)
    at javafx.graphics/javafx.scene.Parent.doComputeGeomBounds(Parent.java:1498)
    at javafx.graphics/javafx.scene.Parent.access$200(Parent.java:79)
    at javafx.graphics/javafx.scene.Parent$1.doComputeGeomBounds(Parent.java:115)
    at javafx.graphics/com.sun.javafx.scene.ParentHelper.computeGeomBoundsImpl(ParentHelper.java:84)
    at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.superComputeGeomBoundsImpl(RegionHelper.java:78)
    at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.superComputeGeomBounds(RegionHelper.java:62)
    at javafx.graphics/javafx.scene.layout.Region.doComputeGeomBounds(Region.java:3289)
    at javafx.graphics/javafx.scene.layout.Region.access$300(Region.java:147)
    at javafx.graphics/javafx.scene.layout.Region$1.doComputeGeomBounds(Region.java:168)
    at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.computeGeomBoundsImpl(RegionHelper.java:89)
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.computeGeomBounds(NodeHelper.java:115)
    at javafx.graphics/javafx.scene.Node.updateGeomBounds(Node.java:3837)
    at javafx.graphics/javafx.scene.Node.getGeomBounds(Node.java:3799)
    at javafx.graphics/javafx.scene.Node.getLocalBounds(Node.java:3747)
    at javafx.graphics/javafx.scene.Node.updateTxBounds(Node.java:3901)
    at javafx.graphics/javafx.scene.Node.getTransformedBounds(Node.java:3693)
    at javafx.graphics/javafx.scene.Parent.getChildTransformedBounds(Parent.java:1845)
    at javafx.graphics/javafx.scene.Parent.recomputeBounds(Parent.java:1634)
    at javafx.graphics/javafx.scene.Parent.doComputeGeomBounds(Parent.java:1498)
    at javafx.graphics/javafx.scene.Parent.access$200(Parent.java:79)
    at javafx.graphics/javafx.scene.Parent$1.doComputeGeomBounds(Parent.java:115)
    at javafx.graphics/com.sun.javafx.scene.ParentHelper.computeGeomBoundsImpl(ParentHelper.java:84)
    at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.superComputeGeomBoundsImpl(RegionHelper.java:78)
    at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.superComputeGeomBounds(RegionHelper.java:62)
    at javafx.graphics/javafx.scene.layout.Region.doComputeGeomBounds(Region.java:3289)
    at javafx.graphics/javafx.scene.layout.Region.access$300(Region.java:147)
    at javafx.graphics/javafx.scene.layout.Region$1.doComputeGeomBounds(Region.java:168)
    at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.computeGeomBoundsImpl(RegionHelper.java:89)
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.computeGeomBounds(NodeHelper.java:115)
    at javafx.graphics/javafx.scene.Node.updateGeomBounds(Node.java:3837)
    at javafx.graphics/javafx.scene.Node.getGeomBounds(Node.java:3799)
    at javafx.graphics/javafx.scene.Node.getLocalBounds(Node.java:3747)
    at javafx.graphics/javafx.scene.Node.intersectsBounds(Node.java:5282)
    at javafx.graphics/javafx.scene.Node$1.intersectsBounds(Node.java:548)
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.intersectsBounds(NodeHelper.java:262)
    at javafx.graphics/javafx.scene.layout.Region.doPickNodeLocal(Region.java:3158)
    at javafx.graphics/javafx.scene.layout.Region.access$600(Region.java:147)
    at javafx.graphics/javafx.scene.layout.Region$1.doPickNodeLocal(Region.java:184)
    at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.pickNodeLocalImpl(RegionHelper.java:104)
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocal(NodeHelper.java:128)
    at javafx.graphics/javafx.scene.Node.pickNode(Node.java:5192)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.pickNode(Scene.java:3993)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Scene.java:3579)
    at javafx.graphics/javafx.scene.Scene.pick(Scene.java:2029)
    at javafx.graphics/javafx.scene.Scene.access$6800(Scene.java:172)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3805)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:832)

Process finished with exit code 0
pkbketx9

pkbketx91#

你的 TimerTask 在后台线程上运行,并更新 ImageView 从那根线。因为javafx和大多数ui工具包一样,是单线程的,所以这里的行为是不确定的;您看到了跨多个线程的数据不一致的影响。
只需使用动画api专门管理这样的功能;e、 g.使用 SequentialTransition :

public void transitionAnimation(ImageView imageView,Image image){
    ScaleTransition front = new ScaleTransition(Duration.seconds(0.15),imageView);
    ScaleTransition back = new ScaleTransition(Duration.seconds(0.15),imageView);

    front.setFromX(1);
    front.setToX(0);
    front.setCycleCount(1);

    front.setOnFinished(e -> imageView.setImage(image));

    back.setFromX(0);
    back.setToX(1);
    back.setCycleCount(1);

    new SequentialTransition(front, back).play();

}

或者 Timeline :

public void transitionAnimation(ImageView imageView,Image image){

    KeyFrame start = new KeyFrame(Duration.ZERO, new KeyValue(imageView.scaleXProperty(), 1.0));
    KeyFrame middle = new KeyFrame(
        Duration.millis(150),
        e -> imageView.setImage(image),
        new KeyValue(imageView.scaleXProperty(), 0.0)
    );
    KeyFrame end = new KeyFrame(Duration.millis(300), new KeyValue(imageView.scaleXProperty(), 1.0));
    new Timeline(start, middle, end).play();
}

相关问题