代号一 - 如何将 onTitleScrollAnimation 与 BorderLayout 一起使用?

Codename One - how to use onTitleScrollAnimation with BorderLayout?

由于在 Toolbar API, and also in this great video tutorial 的最后一节(大约从 45 分钟开始)中引用了滚动动画功能上的工具栏或标题区域,因此动画在特定情况下运行良好。

我找不到任何关于这些必须是什么的文档,但是我发现了一种情况,在这种情况下它不起作用。这是一个演示问题的工作示例:

        Form hi = new Form("Title", new BoxLayout(BoxLayout.Y_AXIS));
        EncodedImage placeholder = EncodedImage
                .createFromImage(Image.createImage(hi.getWidth(), hi.getWidth() / 5, 0xffff0000), true);
        URLImage background = URLImage.createToStorage(placeholder, "400px-AGameOfThrones.jpg",
                "http://awoiaf.westeros.org/images/thumb/9/93/AGameOfThrones.jpg/400px-AGameOfThrones.jpg");
        background.fetch();
        Style stitle = hi.getToolbar().getTitleComponent().getUnselectedStyle();
        stitle.setBgImage(background);
        stitle.setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FILL);
        stitle.setPaddingUnit(Style.UNIT_TYPE_DIPS, Style.UNIT_TYPE_DIPS, Style.UNIT_TYPE_DIPS, Style.UNIT_TYPE_DIPS);
        stitle.setPaddingTop(15);

//      hi.setLayout(new BorderLayout()); // uncomment this for the animation to break
        Container contentContainer = new Container(BoxLayout.y());
        contentContainer.setScrollableY(true);
        // add some elements so we have something to scroll
        for (int i = 0; i < 50; i++)
            contentContainer.add(new Label("Entry " + i));
        hi.add(contentContainer);
//      hi.add(BorderLayout.CENTER, contentContainer); // use this line instead of the above for the animation to break

        ComponentAnimation anim = hi.getToolbar().getTitleComponent().createStyleAnimation("Title", 200);
        hi.getAnimationManager().onTitleScrollAnimation(anim);
        hi.show();

使用我当前的应用程序和工具栏 API 中的代码示例(此处大致改编),我发现未调用 onScrollAnimation 事件,当 BorderLayout 内发生滚动时。即使我有一个单独的容器,它不是内容窗格本身,并且我将 setScrollableY(true); 设置为 true,动画也能正常工作。当这个容器通过 Borderlayout 放入窗体中心时,动画停止工作。在上面的示例中,布局完全相同,因为当然其他区域没有其他组件,但是它破坏了动画。

如何解决?在我的应用程序中,我需要 BorderLayout 但仍想使用这个很酷的功能。此外,这是一个非常 un-intuitive 的功能,如果它适用于某些但不是所有布局。它应该完全 layout-agnostic 并且在任何情况下都有效。

谢谢。

适配器绑定到滚动的表单内容窗格,因此如果您在此处有边框布局,它将无法工作。在这种情况下,不会检测到滚动,因为代码只是不知道滚动。它需要跟踪 UI 中任何组件的滚动以检测该滚动。

正如 Shai 所暗示的,解决方案如下:

    hi.setLayout(new BorderLayout()); 
    Container contentContainer = new Container(BoxLayout.y());
    contentContainer.setScrollableY(true);

    // add some elements so we have something to scroll
    for (int i = 0; i < 50; i++)
        contentContainer.add(new Label("Entry " + i));

    hi.add(BorderLayout.CENTER, contentContainer); // use this line instead of the above for the animation to break

    ComponentAnimation anim = hi.getToolbar().getTitleComponent().createStyleAnimation("Title", 200);
    hi.getAnimationManager().onTitleScrollAnimation(contentContainer, anim);

不要使用 onTitleScollAnimation 来添加动画,而是提供您自己的可滚动 "body" 或内容容器作为第一个参数,并附加动画。