JavaFX css border-radius issue

这一生的挚爱 提交于 2019-12-20 03:40:10

问题


I am trying to simulate the effect one would get from this css example:

border-radius: 50%;

From searching the API and reading posts on forums including this one, I found that I should be using -fx-background-radius. This however is not giving me the wanted effect.

I setup a picture as the background using -fx-background-image:url(...) and then I want to make it into a circle.

How can I achieve this?

Update

So I see that I was not being too specific so let me try to elaborate:

I created a Pane object, that does extend the Region class from JavaFX.

main.fxml:

...
<Pane styleClass="wrapper">
    <Pane layoutX="34.0" layoutY="28.0" styleClass="image" />
</Pane>

For this pane I created the styleclass image as seen above.

main.css:

.list-contact .image {
  -fx-alignment:left;
  -fx-pref-height:40px;
  -fx-pref-width:40px;
  -fx-background-radius:50%;
  -fx-background-image:url(...);
  -fx-background-repeat:no-repeat;
}

The effect I get:

The effect I want:

I hope this explains it better.


回答1:


It looks like a CSS border-radius: 50% should create an elliptical border, and JavaFX CSS does support the % shorthand for either -fx-border-radius or -fx-background-radius. To get the desired effect, however, use Path.subtract() to create an elliptical matte for the image, as shown below.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;

/**
 * @see http://stackoverflow.com/a/38008678/230513
 */
public class Test extends Application {

    private final Image IMAGE = new Image("http://i.imgur.com/kxXhIH1.jpg");

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Test");
        int w = (int) (IMAGE.getWidth());
        int h = (int) (IMAGE.getHeight());
        ImageView view = new ImageView(IMAGE);
        Rectangle r = new Rectangle(w, h);
        Ellipse e = new Ellipse(w / 2, h / 2, w / 2, h / 2);
        Shape matte = Path.subtract(r, e);
        matte.setFill(Color.SIENNA);
        StackPane root = new StackPane();
        root.getChildren().addAll(view, matte);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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



回答2:


This is not possible from CSS alone, since ImageView does not support any of Region's CSS properties.

However you can use a Ellipse as clip for the ImageView:

@Override
public void start(Stage primaryStage) throws MalformedURLException {
    Image img = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/2/23/Space_Needle_2011-07-04.jpg/304px-Space_Needle_2011-07-04.jpg");
    ImageView iv = new ImageView(img);
    Ellipse ellipse = new Ellipse(img.getWidth() / 2, img.getHeight() / 2, img.getWidth() / 2, img.getHeight() / 2);
    iv.setClip(ellipse);

    Text text = new Text("Space Needle, Seattle, Washington, USA");
    StackPane.setAlignment(text, Pos.TOP_CENTER);

    StackPane root = new StackPane(text, iv);

    Scene scene = new Scene(root, img.getWidth(), img.getHeight());
    scene.setFill(Color.AQUAMARINE);

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

I know it doesn't look good to let the image cover the text. This is only done for the purpose of demonstration.




回答3:


In one Line with Circle as Clip.You can use setClip(any shape).:

imageView.setClip(new Circle(width,height,radius);

The width,height,radius have to be slighty smaller that ImageView size to work.

Inspired by GuiGarage web site.



来源:https://stackoverflow.com/questions/38004410/javafx-css-border-radius-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!