【问题标题】:How to make an animation/transition with the width of a node in javafx?如何在javafx中使用节点的宽度制作动画/过渡?
【发布时间】:2016-09-10 06:49:57
【问题描述】:

我想用节点的“宽度”制作动画。

在这种情况下,我的节点是“AnchorPane”。

我尝试在 javafx 中制作导航抽屉。

没有属性“width Property()”吗?

new Key Value (node.width Property (), 1, WEB_EASE)

node.widthProperty().getValue() 未找到

我的代码:

public void changeWidth(final Node node, double width) {
    this.node = node;
    this.timeline = TimelineBuilder.create()
        .keyFrames(
            new KeyFrame(Duration.millis(20),    
                new KeyValue(    going here?   , width, WEB_EASE)
            )
        )
        .build();

    setCycleDuration(Duration.seconds(5));
    setDelay(Duration.seconds(0));
}

具有“不透明度”属性的示例:

new KeyValue(node.opacityProperty(), 1, WEB_EASE)

我的班级 ConfigAnimationViewPane:

public class ConfigAnimationViewPane extends Transition {
    protected static final Interpolator WEB_EASE = Interpolator.EASE_BOTH;
    protected AnchorPane node;
    protected Timeline timeline;
    private boolean oldCache = false;
    private CacheHint oldCacheHint = CacheHint.DEFAULT;
    private final boolean useCache = true;



    /**
     * Called when the animation is starting
     */
    protected void starting() {
        if (useCache) {
            oldCache = node.isCache();
            oldCacheHint = node.getCacheHint();
            node.setCache(true);
            node.setCacheHint(CacheHint.SPEED);
        }
    }

    /**
     * Called when the animation is stopping
     */
    protected void stopping() {
        if (useCache) {
            node.setCache(oldCache);
            node.setCacheHint(oldCacheHint);
        }
    }

    @Override protected void interpolate(double d) {
        timeline.playFrom(Duration.seconds(d));
        timeline.stop();
    }

}

这是小米控制器:

将菜单向左移动(神秘)

LeftTransition leftTransition = new LeftTransition();
                leftTransition.OpenMenu(list1);
                leftTransition.play();

我想在这里放置我的尺寸“AnchorPane”。 (设置我的“anchorpane”的宽度)

 /*ViewPaneTransition paneTransition = new ViewPaneTransition();
            paneTransition.CloseMenu(viewPane, width );
            paneTransition.play();*/

【问题讨论】:

  • 啊什么?你不能这样做吗? changeWidth(final Pane node, double width) { ??在那里你会得到PrefWidthProperty维护你的节点并使用node.prefWidth(-1);使用双活页夹来连续获得你的宽度。
  • 属性或方法“Pref Width Property ()”不存在。
  • 嗯,我想我当时很高 :) ....Pane extends Region, and ...

标签: java animation javafx transition


【解决方案1】:

这是 java 9 的一个工作示例。它同时更改宽度和高度(如果不需要,只需删除高度线)

widthProperty 是只读的,所以你必须设置 maxWidth 或 minWidth 来切换你需要的大小写。 时间轴上的延迟时长默认为0,无需设置,循环时长由关键帧时长计算。

public void changeSize(final Pane pane, double width, double height) {
    Duration cycleDuration = Duration.millis(500);
    Timeline timeline = new Timeline(
            new KeyFrame(cycleDuration,
                    new KeyValue(pane.maxWidthProperty(),width,Interpolator.EASE_BOTH)),
            new KeyFrame(cycleDuration,
                    new KeyValue(pane.maxHeightProperty(),height,Interpolator.EASE_BOTH))
            );

    timeline.play();
    timeline.setOnFinished(event->{
       /* insert code here if you need */
    });
}

【讨论】:

    【解决方案2】:
    public void changeWidth(final Pane/*Region*/ node, double width) {//its a Pane or Regions
        this.node = node;
        this.timeline = TimelineBuilder.create()
            .keyFrames(
                new KeyFrame(Duration.millis(20),    
                    new KeyValue(    going here?   , width, WEB_EASE)
                )
            )
            .build();
    
        setCycleDuration(Duration.seconds(5));
        setDelay(Duration.seconds(0));
    }
    

    在这种情况下,我的节点是“AnchorPane”。

    你的AnchorPane 是一个子类或Pane,让你的包装器或方法接受Pane 或它们各自的类

    【讨论】:

    • 节点我想在我的窗口中处理这个。这在我的父节点中。
    • 我没有得到您的评论,在这里展示您的所有优点先生,老实说,如果您只是复制粘贴并按照我说的那样做,您会回来感谢您,但如果它作为父母返回,您可以投射它到 AnchorPane 并将其放在您的方法中 @VipPunkJoshersDroopy
    • 我尝试在 javafx 中制作导航抽屉。我想增加锚窗格的大小,paneTransition.Close Menu (view Pane, width), @Elltz.
    • 它在 Java 9 下根本无法编译,也不完整,请参阅我的回答。 - setCycleDuration 受保护,不应使用,Duration.seconds(5) 应传递给 keyFrame,timeline.start 不被调用。
    猜你喜欢
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    相关资源
    最近更新 更多