国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

javafx實現圖片3D翻轉效果方法實例

2019-11-26 16:11:31
字體:
來源:轉載
供稿:網友

實現步驟: 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、效果圖:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 佛学| 新营市| 东台市| 湾仔区| 雷山县| 古浪县| 苏尼特右旗| 布尔津县| 达日县| 历史| 阿坝| 新田县| 东方市| 鄂托克前旗| 东城区| 东丰县| 大埔县| 梅河口市| 宁国市| 海盐县| 长子县| 惠水县| 贵阳市| 金昌市| 海盐县| 河津市| 丰镇市| 西盟| 和平区| 禄劝| 怀仁县| 左贡县| 马边| 郧西县| 璧山县| 延庆县| 自治县| 永靖县| 开鲁县| 石林| 日土县|