拖动后,ScrollPane内容变得模糊 [英] ScrollPane content becomes blurry after dragging

查看:72
本文介绍了拖动后,ScrollPane内容变得模糊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaFX 8.0有此错误,我不知道如何解决.
例如: http://i.imgur.com/tavh6XA.png

JavaFX 8.0 has this bug and I don't know how to solve it.
Example: http://i.imgur.com/tavh6XA.png

如果拖动ScrollPane,其内容将变得模糊,但是如果将其拖动回,则内容将恢复其清晰度.如果我不修改坐标,则内容看起来会很好.

If I drag ScrollPane, its content becomes blurry, but if I drag it back, the content restores its sharpness. If I do not modify coords, the content looks pretty well.

有人解决了这个问题吗?

Did anybody solve this problem?

示例代码:

@Override
public void start(Stage stage) {
            Pane pane = new Pane();

    Scene scene = new Scene(pane, 500, 500);
    pane.prefWidthProperty().bind(scene.widthProperty());
    pane.prefHeightProperty().bind(scene.heightProperty());

    ScrollPane scroll = new ScrollPane();

    // Center ScrollPane
    scroll.layoutXProperty().bind(pane.widthProperty().divide(2).subtract(scroll.widthProperty().divide(2)));
    scroll.layoutYProperty().bind(pane.heightProperty().divide(2).subtract(scroll.heightProperty().divide(2)));
    scroll.setPrefSize(200, 200);

    ListView<String> list = new ListView<>();
    scroll.setContent(list);

    list.getItems().addAll("Resize the window", "and see how this list", "becomes blurry");

    // Label indicates x, y coords of ScrolPane and their ratio.
    TextField size = new TextField();
    size.setPrefWidth(500);
    size.textProperty().bind(
            scroll.layoutXProperty()
            .asString("x: %s; ").concat(
                    scroll.layoutYProperty()
                    .asString("y: %s; ")));

    pane.getChildren().addAll(size, scroll);

    stage.setScene(scene);
    stage.show();
}

推荐答案

推荐的方法-使用内置的布局管理器+大小调整提示

除非您需要,否则建议不要设置节点的布局值.相反,如果您使用标准布局容器,则默认情况下(应该)将所有非整数布局坐标对齐到整数值,以允许系统进行干净的布局,而不会因引起部分文字和线条的分数布局值的反锯齿而引起潜在的模糊不能直接在屏幕像素网格上对齐.

I recommend not setting the layout values for the node unless you need to. Instead if you use standard layout containers, then it (should) by default snap any non-integer layout co-ordinates to integral values allowing clean layout of the system without potential bluriness caused by anti-aliasing of fractional layout values that cause text and lines to not align directly on the screen pixel grid.

没有模糊性的示例代码

当我在机器上运行修改后的示例时,该示例不通过绑定执行布局,而是仅使用布局管理器进行布局,因此我没有观察到和模糊值(win 7,JavaFX 8u20).我也不需要修改默认的节点缓存提示.

When I ran a modified sample on my machine which does not perform layout by binding but instead does layout only using layout managers, I did not observe and blurry values (win 7, JavaFX 8u20). I also did not need to modify the default node caching hints.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class NonBlurryPane extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        VBox pane = new VBox();

        pane.setPrefSize(500, 500);

        Scene scene = new Scene(pane);

        ScrollPane scroll = new ScrollPane();
        StackPane scrollContainer = new StackPane(scroll);
        VBox.setVgrow(scrollContainer, Priority.ALWAYS);

        // Center ScrollPane
        scroll.setMinSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
        scroll.setPrefSize(200, 200);
        scroll.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);

        ListView<String> list = new ListView<>();
        scroll.setContent(list);

        list.getItems().addAll("Resize the window", "and see how this list", "does NOT become blurry");

        // Label indicates x, y coords of ScrolPane and their ratio.
        TextField size = new TextField();
        size.setMaxWidth(Double.MAX_VALUE);
        size.setMinHeight(Region.USE_PREF_SIZE);
        size.textProperty().bind(
                scroll.layoutXProperty()
                        .asString("x: %s; ").concat(
                        scroll.layoutYProperty()
                                .asString("y: %s; ")).concat(
                        scroll.layoutXProperty().divide(scroll.layoutYProperty())
                                .asString("x/y: %s; ")).concat(
                        scroll.layoutYProperty().divide(scroll.layoutXProperty())
                                .asString("y/x: %s")));

        pane.getChildren().addAll(size, scrollContainer);

        stage.setScene(scene);
        stage.show();
    }
}

我还注意到默认布局管理器将双精度坐标舍入为整数坐标.是否可以使数字绑定以这种方式工作?

I also noticed that default layout managers round double coords to integer coords. Is that possible to make the number bindings work in this way?

绑定整数坐标

整数坐标有点简化.有时您需要的坐标是x.5(请参阅

Integer co-ordinates are a bit of a simplification. Sometimes you want co-ordinates which are x.5 (see How to draw a crisp, opaque hairline in JavaFX 2.2? for more details). It's a detail you don't need to worry about usually with the standard layout panes as they by default will handle clean snapping.

Java 8的标准绑定例程中没有下层函数或舍入函数.您可以使用无效侦听器或更改侦听器并基于它们进行更新(而不是绑定),或者可以开发自定义绑定来舍入到适当的值.有关这些选项的更多信息,请参见探索可观察到的,ObservableValue,InvalidationListener和ChangeListener"以及Oracle JavaFX属性教程的使用低级绑定API"部分.

There is no kind of floor or round function in the standard bindings routines for Java 8. Either you can use invalidation listeners or change listeners and update based on them (instead of binding), or you can develop a custom binding which rounds to the appropriate value. For more information on these options, see the "Exploring Observable, ObservableValue, InvalidationListener, and ChangeListener" and "Using the Low-Level Binding API" sections of the Oracle JavaFX properties tutorial.

布局提示

进行布局的首选方法是(从最简单到更复杂):

The preferred way to do layout is (from easiest to more complex):

  1. 使用布局管理器.
  2. 对于非常简单的布局调整,绑定或更改侦听器通常是可以的.
  3. 覆盖 layoutChildren() 中的任何自定义和复杂的内容.

layoutChildren()

On layoutChildren()

内置JavaFX控件和布局管理器中的大多数自定义布局计算都是通过覆盖layoutChildren()而不是通过绑定来处理的.您可以查看 JavaFX源代码,并研究如何实现它们.这对于理解如何使用layoutChildren是必要的(因为我在任何地方都没有看到它的详细记录).覆盖layoutChildren有点先进并且很难做到.为了从布局算法中获得可预测的质量结果,您需要手动考虑填充插图,像素捕捉,准确测量子组件尺寸等情况.

Most of the custom layout calculations in the in-built JavaFX controls and layout managers are handled by over-riding layoutChildren() and not through bindings. You can review the JavaFX source code and study how these are implemented. This is necessary to understand how to use layoutChildren (as I have not seen it thoroughly documented anywhere). Overriding layoutChildren is a little advanced and tricky to do well. To get predictable quality results from your layout algorithm, you need to manually account for things like padding insets, pixel snapping, accurately measuring the size of child components, etc.

这篇关于拖动后,ScrollPane内容变得模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆