是否可以在反光层上使用过渡?

Is it possible to use transitions on glisten layers?

我根据这个material创建了一个自定义图层: http://docs.gluonhq.com/charm/2.1.1/#_creating_a_layer,并将其添加到我的应用程序中:

MobileApplication.getInstance().addLayerFactory(LAYER_NAME, () -> customLayer);

现在我想给这一层添加一个过渡。您可以在 View 上使用转换,例如:view.setShowTransitionFactory(BounceInDownTransition:new)

Layer 没有提供这样的方法。所以我尝试了这种方法来应用转换:

private void showLayer() {
    MobileApplication.getInstance().showLayer(LAYER_NAME);
    new BounceInDownTransition(customLayer).play();
}

当我第一次调用 showLayer() 时,转换似乎未完成。第一部分,图层应该从视图中移出的地方,缺失了。每次进一步调用 showLayer() 都会显示完整的转换。

图层是否要与过渡结合使用? 如果可能,推荐的方式是什么?

您可以使用 Gluon Charm 中的内置转换,因为您只需将一个节点传递给它们,然后调用播放来启动动画。

对于 Gluon 的图层,没有关于视图的内置机制,但您可以轻松地将其添加到您的 class。

这将为显示创建反弹效果,为隐藏创建反弹效果。

public class MyLayer extends Layer {

    private final Node root;
    private final double size = 150;

    public MyLayer() {
        final BounceInDownTransition transitionIn = new BounceInDownTransition(this, true);
        final BounceOutDownTransition transitionOut = new BounceOutDownTransition(this, true);
        transitionOut.setOnFinished(e -> hide());

        Button button = new Button("", MaterialDesignIcon.CLOSE.graphic());
        button.setOnAction(e -> transitionOut.playFromStart());
        root = new StackPane(button);
        root.setStyle("-fx-background-color: white;");
        getChildren().add(root);

        getGlassPane().getLayers().add(this);

        showingProperty().addListener((obs, ov, nv) -> {
            if (nv) {
                layoutChildren();
                setOpacity(0);
                transitionIn.playFromStart();
            }
        });
    }

    @Override
    public void show() {
        getGlassPane().setBackgroundFade(GlassPane.DEFAULT_BACKGROUND_FADE_LEVEL);
        super.show();
    }

    @Override
    public void hide() {
        getGlassPane().setBackgroundFade(0.0);
        super.hide();
    }

    @Override
    public void layoutChildren() {
        root.setVisible(isShowing());
        if (!isShowing()) {
            return;
        }
        root.resize(size, size);
        resizeRelocate((getGlassPane().getWidth() - size)/2, (getGlassPane().getHeight()- size)/2, size, size);
    }
}

现在,添加图层:

@Override
public void init() {
    addViewFactory(BASIC_VIEW, () -> new BasicView(BASIC_VIEW));

    addLayerFactory("My Layer", () -> new MyLayer());
}