【问题标题】:AndroidSVG: How to dynamically change Inkscape svg colorAndroidSVG:如何动态更改 Inkscape svg 颜色
【发布时间】:2020-10-21 21:47:30
【问题描述】:

AndroidSVG 允许动态颜色更改,如 here 所述。

以下代码适用于我 95% 的图标库...

SVGImageView svgIV;
...... 
String color = "#FEFBDE"
svgIV.setCSS(String.format("* { fill:%s; }", color););

...但在 Inkscape SVG 上失败,例如:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   version="1.1"
   width="64"
   height="64"
   id="svg2"
   style="display:inline">
  <defs
     id="defs4" />
  <g
     transform="translate(403.45398,-1106.6063)"
     id="layer1"
     style="display:inline">
    <g
       transform="matrix(7.6594323,0,0,7.6594323,2702.0005,-7795.5738)"
       id="g3766">
      <path
         d="m -401.264,1167.506 0,2.8346 1.41732,0"
         id="path4668"
         style="fill:none;stroke:#000000;stroke-width:0.53149605;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" />
      <path
         d="m -401.264,1162.5164 c 0,0 -0.5132,-0.012 -0.5132,0.6511 0,0.3318 0,0.1926 0,0.5423 0,0.3989 -0.64122,0.3626 -0.64122,1.0261 0,0.5996 0.047,0.4425 0,0.8135 -0.047,0.371 -0.76981,0.356 -0.76981,1.1045 0,0.9955 0.64122,0.8521 0.64122,0.8521 l 1.28301,0"
         id="path4711-1"
         style="fill:none;stroke:#000000;stroke-width:0.53149605;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" />
      <path
         d="m -401.264,1162.5164 c 0,0 0.5132,-0.012 0.5132,0.6511 0,0.3318 0,0.1926 0,0.5423 0,0.3989 0.64122,0.3626 0.64122,1.0261 0,0.5996 -0.047,0.4425 0,0.8135 0.047,0.371 0.76981,0.356 0.76981,1.1045 0,0.9955 -0.64122,0.8521 -0.64122,0.8521 l -1.28301,0"
         id="path4711-1-3"
         style="fill:#000000;fill-opacity:1;stroke:#000000;stroke-width:0.53149605;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" />
    </g>
  </g>
</svg>

什么是必要的 CSS(如果有的话)?

经过反思,这可能是一个更一般的 CSS 问题: 如何覆盖 stylepart

【问题讨论】:

    标签: css inkscape androidsvg


    【解决方案1】:

    通常(在 CSS 中)实现此目的的方式是使用 !important 标志。

    svgIV.setCSS(String.format("* { fill:%s !important; }", color););
    

    很遗憾,AndroidSVG 中的 CSS 系统还不支持!important。它在我的 TODO 清单上。

    与此同时,您需要将这些 SVG 转换为使用属性 (fill="whatever") 或 CSS。您可以轻松地将这些文件转换为使用属性形式,方法是将它们加载到 Inkscape 中并保存为“优化的 SVG”。

    【讨论】:

      【解决方案2】:

      fork 是为回答问题而创建的。

      它允许通过用户定义的回调对 any .svg 进行全颜色控制,非常适合不希望手动转换 2,735 个 Inkscape 定义以符合当前限制的用户。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-28
        • 2013-12-12
        • 1970-01-01
        • 2012-04-27
        • 1970-01-01
        • 2020-09-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多