栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JavaFx 8-相对于鼠标位置缩放/缩放ScrollPane

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

JavaFx 8-相对于鼠标位置缩放/缩放ScrollPane

这是一个 可扩展的,可缩放的 JavaFX ScrollPane:

import javafx.geometry.Bounds;import javafx.geometry.Point2D;import javafx.geometry.Pos;import javafx.scene.Group;import javafx.scene.Node;import javafx.scene.control.ScrollPane;import javafx.scene.layout.VBox;public class ZoomableScrollPane extends ScrollPane {    private double scalevalue = 0.7;    private double zoomIntensity = 0.02;    private Node target;    private Node zoomNode;    public ZoomableScrollPane(Node target) {        super();        this.target = target;        this.zoomNode = new Group(target);        setContent(outerNode(zoomNode));        setPannable(true);        setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);        setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);        setFitToHeight(true); //center        setFitToWidth(true); //center        updateScale();    }    private Node outerNode(Node node) {        Node outerNode = centeredNode(node);        outerNode.setonScroll(e -> { e.consume(); onScroll(e.getTextDeltaY(), new Point2D(e.getX(), e.getY()));        });        return outerNode;    }    private Node centeredNode(Node node) {        VBox vBox = new VBox(node);        vBox.setAlignment(Pos.CENTER);        return vBox;    }    private void updateScale() {        target.setScaleX(scalevalue);        target.setScaleY(scalevalue);    }    private void onScroll(double wheelDelta, Point2D mousePoint) {        double zoomFactor = Math.exp(wheelDelta * zoomIntensity);        Bounds innerBounds = zoomNode.getLayoutBounds();        Bounds viewportBounds = getViewportBounds();        // calculate pixel offsets from [0, 1] range        double valX = this.getHvalue() * (innerBounds.getWidth() - viewportBounds.getWidth());        double valY = this.getVvalue() * (innerBounds.getHeight() - viewportBounds.getHeight());        scalevalue = scalevalue * zoomFactor;        updateScale();        this.layout(); // refresh ScrollPane scroll positions & target bounds        // convert target coordinates to zoomTarget coordinates        Point2D posInZoomTarget = target.parentToLocal(zoomNode.parentToLocal(mousePoint));        // calculate adjustment of scroll position (pixels)        Point2D adjustment = target.getLocalToParentTransform().deltaTransform(posInZoomTarget.multiply(zoomFactor - 1));        // convert back to [0, 1] range        // (too large/small values are automatically corrected by ScrollPane)        Bounds updatedInnerBounds = zoomNode.getBoundsInLocal();        this.setHvalue((valX + adjustment.getX()) / (updatedInnerBounds.getWidth() - viewportBounds.getWidth()));        this.setVvalue((valY + adjustment.getY()) / (updatedInnerBounds.getHeight() - viewportBounds.getHeight()));    }}


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

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

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