欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

javafx实现图片3D翻转效果方法实例

时间:2021-02-06 10:07:16|栏目:JAVA代码|点击:

实现步骤: 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、效果图:

上一篇:详解JAVAEE――SSH三大框架整合(spring+struts2+hibernate)

栏    目:JAVA代码

下一篇:基于Java文件输入输出流实现文件上传下载功能

本文标题:javafx实现图片3D翻转效果方法实例

本文地址:http://www.codeinn.net/misctech/58276.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有