javafx實現圖片3D翻轉效果方法執行個體_java

來源:互聯網
上載者:User

實現步驟: 1、定義FlipView對象。包含以下屬性:

複製代碼 代碼如下:

    //正面視圖
public Node frontNode;
//反面視圖
public Node backNode;
//是否翻轉
boolean flipped = false;
//翻轉角度
DoubleProperty time = new SimpleDoubleProperty(Math.PI / 2);
//正面翻轉特效
PerspectiveTransform frontEffect = new PerspectiveTransform();
//反面翻轉特效
PerspectiveTransform backEffect = new PerspectiveTransform();

 create方法返回需要顯示的內容:

複製代碼 代碼如下:

private void create() {
        time.addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue<? extends Number> arg0,
                    Number arg1, Number arg2) {
                setPT(frontEffect, time.get());
                setPT(backEffect, time.get());
            }
        });
        anim.getKeyFrames().addAll(frame1, frame2);
        backNode.visibleProperty().bind(
                Bindings.when(time.lessThan(0)).then(true).otherwise(false));

        frontNode.visibleProperty().bind(
                Bindings.when(time.lessThan(0)).then(false).otherwise(true));
        setPT(frontEffect, time.get());
        setPT(backEffect, time.get());
        frontNode.setEffect(frontEffect);
        backNode.setEffect(backEffect);
        getChildren().addAll(backNode, frontNode);
    }

以上代碼需要注意的是: 隨著time值的變化frontEffect和backEffect的值也會隨著變換。 2、PerspectiveTransform特效的實現使用了Math.sin()和Math.cos()方法類比3D角度變換。 具體實現如下:
複製代碼 代碼如下:

private void setPT(PerspectiveTransform pt, double t) {
        double width = 200;
        double height = 200;
        double radius = width / 2;
        double back = height / 10;
        pt.setUlx(radius - Math.sin(t) * radius);
        pt.setUly(0 - Math.cos(t) * back);
        pt.setUrx(radius + Math.sin(t) * radius);
        pt.setUry(0 + Math.cos(t) * back);
        pt.setLrx(radius + Math.sin(t) * radius);
        pt.setLry(height - Math.cos(t) * back);
        pt.setLlx(radius - Math.sin(t) * radius);
        pt.setLly(height + Math.cos(t) * back);
    }

3、角度變換在1秒的時間內從3.14/2變換到-3.14/2。
複製代碼 代碼如下:

KeyFrame frame1 = new KeyFrame(Duration.ZERO, new KeyValue(time,
            Math.PI / 2, Interpolator.LINEAR));
    KeyFrame frame2 = new KeyFrame(Duration.seconds(1),
            new EventHandler() {
                @Override
                public void handle(ActionEvent event) {
                    flipped = !flipped;
                }
            }, new KeyValue(time, -Math.PI / 2, Interpolator.LINEAR));

 4、FlipView對象的建立:通過建構函式可以很方便的建立FlipView對象.

複製代碼 代碼如下:

ImageView image1 = new ImageView(new Image(getClass()
                .getResourceAsStream("lion1.png")));
ImageView image2 = new ImageView(new Image(getClass()
                .getResourceAsStream("lion2.png")));
FlipView flip = new FlipView(image1, image2);

 5、效果圖:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.