栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/153335.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号