【问题标题】:Java Batik resize SVG to panel size keeping aspect ratioJava Batik 将 SVG 调整为面板大小,保持纵横比
【发布时间】:2019-02-24 15:05:49
【问题描述】:

我正在尝试在我的摇摆应用程序上显示 SVG 图像, 因为我没有 SVG 文件,但我将路径转换为有效的 svg 文档的路径:

private static Document buildSVGDocument(Color svgColor, /*double svgWidth, double svgHeight,*/ String svgPath) {
    DOMImplementation svgDocumentImplementation = SVGDOMImplementation.getDOMImplementation();
    Document svgDocument = svgDocumentImplementation.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null);

    Element svgDocumentElement = svgDocument.getDocumentElement();
    //svgDocumentElement.setAttribute("height", String.valueOf(svgHeight));
    //svgDocumentElement.setAttribute("width", String.valueOf(svgWidth));

    Element svgDocumentPath = svgDocument.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "path");
    svgDocumentPath.setAttribute("style", String.format("fill:rgb(%s, %s, %s);", svgColor.getRed(), svgColor.getGreen(), svgColor.getBlue()));
    svgDocumentPath.setAttribute("d", svgPath);
    svgDocumentElement.appendChild(svgDocumentPath);

    return svgDocument;
}

然后我在蜡染画布上显示 SVG 文档:

JSVGCanvas panel = new JSVGCanvas();
panel.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
panel.setDisableInteractions(true);
panel.setDocument(buildSVGDocument(/*etc*/));
container.add(panel, BorderLayout.WEST);

现在我的问题是:如何将 svg 调整为面板大小以保持纵横比?

【问题讨论】:

    标签: java swing svg scale batik


    【解决方案1】:

    我想出了怎么做!, 浏览源码找到了计算图片比例的方法,是:

    calculateViewingTransform
    

    然后我实现了一个简单的类来将图像缩放到容器中

    public class SVGCanvas extends JSVGCanvas {
    
        private static final long serialVersionUID = 1L;
    
        /**
         * The type of scale
         */
        private short svgScale;
    
        /**
         * Image padding
         */
        private int svgPadding;
    
        public SVGCanvas() {
            this.svgScale = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMAX;
            this.svgPadding = 5;
        }
    
        @Override
        protected AffineTransform calculateViewingTransform(String svgElementIdentifier, SVGSVGElement svgElement) {
            SVGRect svgElementBounds = svgElement.getBBox();
            float[] svgElementBoundsVector = new float[] {
                svgElementBounds.getX(),
                svgElementBounds.getY(),
                svgElementBounds.getWidth(),
                svgElementBounds.getHeight()
            };
    
            float svgEemenetScaleToHeight = getHeight() - svgPadding;
            float svgElementScaleToWidth = getWidth() - svgPadding;
    
            return ViewBox.getPreserveAspectRatioTransform(
                svgElementBoundsVector, svgScale, true,
                svgElementScaleToWidth,
                svgEemenetScaleToHeight
            );
        }
    
        public void setSvgScale(short svgScale) {
            this.svgScale = svgScale;
        }
    
        public void setSvgPadding(int svgPadding) {
            this.svgPadding = svgPadding;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-16
      • 2012-05-01
      • 2020-06-14
      • 2013-06-26
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多