JavaFX中没有缩放效果的ViewPort方法

ViewPort method without zoom effect in JavaFX

我正在尝试对图像进行切片以在屏幕上显示。 例如,如果我将左侧花卉图片的右边距更改为 20,将右侧花卉图片的下边距更改为 20,则它们的行为应如下所示:

(第一栏为原图) enter image description here

ViewPort 方法完全符合我的要求,剪切图像并留空边距,但它使用图像的原始大小显示缩放效果,如 javafx 网站所述:

"图像中的矩形视口。在缩放或任何其他转换之前,视口在图像的坐标中指定。 如果 viewport 为 null,则显示整个图像。如果视口不为空,则仅显示落在视口内的图像部分。如果图像没有完全覆盖视口,则视口的任何剩余区域都将为空。"

我已经尝试在设置视口之前使用 imageView.resize(screenWidth, screenHeight),但它不起作用。
也试过imageView.setFitWidth(screenWidth), imageView.setFitHeight(screenHeight)
是否可以重新缩放图像以使其按描述显示? 或者任何其他解决方法?

下面是我的左边距代码示例:
mediaContent.resize(screenWidth, screen.getHeight()); // mediaContent.setFitWidth(screenWidth); // mediaContent.setFitHeight(screen.getHeight()); mediaContent.setViewport(new Rectangle2D(screenWidth-dXLeft, 0, screenWidth, screen.getHeight()));

更新:已尝试设置setFitWidthsetFitHeight建议的静止图像显示放大的图像

    `imageView.setFitWidth(space.getX());
    imageView.setFitHeight(space.getY());
    imageView.setViewport(new Rectangle2D(screen.getWidth()-space.getX(), 0, screen.getWidth(), screen.getHeight()));
    imageView.setFitWidth(space.getX());
    imageView.setFitHeight(space.getY());

    return imageView;`

这是我在不使用setViewport方法时的结果 设置边距:enter image description here

不设置边距时应该如何:enter image description here

更新 2:示例(修改自 java-buddy)放大

package javafx_imageview_viewport;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_ImageView_Viewport extends Application {

@Override
public void start(Stage primaryStage) {

    ImageView imageView1 = new ImageView(new Image("https://i.imgur.com/6Zl0eQB.jpg"));
    imageView1.setFitWidth(150);
    imageView1.setFitHeight(100);

    //Example to rotate ImageView
    Image image2 = new Image("https://i.imgur.com/6Zl0eQB.jpg");
    Rectangle2D viewportRect2 = new Rectangle2D(
            image2.getWidth()/4, 
            image2.getHeight()/4, 
            image2.getWidth()*3/4, 
            image2.getHeight()*3/4);
    ImageView imageView2 = new ImageView(image2);
    imageView2.setFitWidth(150);
    imageView2.setFitHeight(100);
    imageView2.setViewport(viewportRect2);

    Slider sliderRotate = new Slider();
    sliderRotate.setMin(0);
    sliderRotate.setMax(360);
    sliderRotate.setValue(0);
    sliderRotate.valueProperty().addListener(
            (ObservableValue<? extends Number> observable, 
                    Number oldValue, Number newValue) -> {
        imageView2.setRotate((double)newValue);
    });

    //Example to change ViewPort
    Image image3 = new Image("https://i.imgur.com/6Zl0eQB.jpg");
    Rectangle2D viewportRect3 = new Rectangle2D(
            0, 
            0, 
            image3.getWidth(), 
            image3.getHeight());
    ImageView imageView3 = new ImageView(image3);
    imageView3.setFitWidth(150);
    imageView3.setFitHeight(100);
    imageView3.setViewport(viewportRect3);

    Slider sliderViewPort = new Slider();
    sliderViewPort.setMin(0);
    sliderViewPort.setMax(1.0);
    sliderViewPort.setValue(1.0);
    sliderViewPort.valueProperty().addListener(
            (ObservableValue<? extends Number> observable, 
                    Number oldValue, Number newValue) -> {
        Rectangle2D newViewportRect3 = new Rectangle2D(
                0,
                0,
                (double)newValue*image3.getWidth(),
                (double)newValue*image3.getHeight());
        imageView3.setViewport(newViewportRect3);
    });


    VBox vBox = new VBox();
    vBox.getChildren().addAll(imageView1, 
            imageView2, sliderRotate,
            imageView3, sliderViewPort);

    StackPane root = new StackPane();
    root.getChildren().add(vBox);

    Scene scene = new Scene(root, 300, 350);

    primaryStage.setTitle("java-buddy: ImageVIew ViewPort");
    primaryStage.setScene(scene);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}    
}

来自 java 的示例 2 - 伙计,我希望它如何表现,剪切图像:

package javafx_imageview_viewport;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_ImageView_Viewport extends Application {

@Override
public void start(Stage primaryStage) {

    ImageView imageView1 = new ImageView(new Image("https://i.imgur.com/294AEFU.png"));

    //Example to rotate ImageView
    Image image2 = new Image("https://i.imgur.com/294AEFU.png");
    Rectangle2D viewportRect2 = new Rectangle2D(
            image2.getWidth()/4, 
            image2.getHeight()/4, 
            image2.getWidth()*3/4, 
            image2.getHeight()*3/4);
    ImageView imageView2 = new ImageView(image2);
    imageView2.setViewport(viewportRect2);

    Slider sliderRotate = new Slider();
    sliderRotate.setMin(0);
    sliderRotate.setMax(360);
    sliderRotate.setValue(0);
    sliderRotate.valueProperty().addListener(
            (ObservableValue<? extends Number> observable, 
                    Number oldValue, Number newValue) -> {
        imageView2.setRotate((double)newValue);
    });

    //Example to change ViewPort
    Image image3 = new Image("https://i.imgur.com/294AEFU.png");
    Rectangle2D viewportRect3 = new Rectangle2D(
            0, 
            0, 
            image3.getWidth(), 
            image3.getHeight());
    ImageView imageView3 = new ImageView(image3);
    imageView3.setViewport(viewportRect3);

    Slider sliderViewPort = new Slider();
    sliderViewPort.setMin(0);
    sliderViewPort.setMax(1.0);
    sliderViewPort.setValue(1.0);
    sliderViewPort.valueProperty().addListener(
            (ObservableValue<? extends Number> observable, 
                    Number oldValue, Number newValue) -> {
        Rectangle2D newViewportRect3 = new Rectangle2D(
                0,
                0,
                (double)newValue*image3.getWidth(),
                (double)newValue*image3.getHeight());
        imageView3.setViewport(newViewportRect3);
    });


    VBox vBox = new VBox();
    vBox.getChildren().addAll(imageView1, 
            imageView2, sliderRotate,
            imageView3, sliderViewPort);

    StackPane root = new StackPane();
    root.getChildren().add(vBox);

    Scene scene = new Scene(root, 300, 350);

    primaryStage.setTitle("java-buddy: ImageVIew ViewPort");
    primaryStage.setScene(scene);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

看来我想要的没有意义

如果我想让图像适合屏幕,我必须使用 fitScreenWidth(screenWidth)fitScreenHeight(screenHeight) 但在这种情况下使用 viewPort 方法没有意义,因为图像有已经调整大小了。

viewPort 方法只能用于原始图像的尺寸。