2

我有一個自定義view,它顯示drawable並檢測到觸摸。它有許多的drawable層,其中一些包含文本,例如,如何訪問或翻譯矢量繪圖文本

Hello in a box!

我已轉換的各SVG矢量圖形插入vectordrawable並希望能夠設置文本是一個資源所以我可以替換不同的翻譯。

上面的 '你好盒子' 的SVG XML代碼: -

<svg 
    width="800" 
    height="400" 
    viewBox="0 0 800 400.00001" 
    id="svg5772" 
    version="1.1" 
    inkscape:version="0.91 r13725" 
    sodipodi:docname="HELLO in a box.svg"> 
    <defs 
    id="defs5774" /> 
    <sodipodi:namedview 
    id="base" 
    pagecolor="#ffffff" 
    bordercolor="#666666" 
    borderopacity="1.0" 
    <rect 
     style="opacity:1;fill:#ffffff;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" 
     id="rect6320" 
     width="468.57144" 
     height="128.57143" 
     x="174.28572" 
     y="786.64795" 
     rx="0" 
     ry="0" /> 
    <text 
     xml:space="preserve" 
     style="font-style:normal;font-weight:normal;font-size:40px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" 
     x="348.57141" 
     y="869.50507" 
     id="text6322" 
     sodipodi:linespacing="125%"><tspan 
     sodipodi:role="line" 
     id="tspan6324" 
     x="348.57141" 
     y="869.50507">HELLO!!!</tspan></text> 
    </g> 
</svg> 

...將這個vectordrawable XML: -

<?xml version="1.0" encoding="utf-8"?> 
<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:viewportWidth="800" 
    android:viewportHeight="400" 
    android:width="800dp" 
    android:height="400dp"> 
    <group 
     android:translateY="-652.3621"> 
     <path 
      android:pathData="M174.28572 786.64795l468.57144 0 0 128.57143 -468.57144 0z" 
      android:strokeWidth="2" 
      android:fillColor="#ffffff" 
      android:strokeLineJoin="round" 
      android:strokeLineCap="round" 
      android:strokeMiterLimit="4" 
      android:strokeColor="#000000" /> 
     <path 
      android:pathData="M374.72375 869.50507l-3.86718 0 0 -14.23829 -14.51172 0 0 14.23829 -3.86719 0 0 -29.08204 3.86719 0 0 11.40625 14.51172 0 0 -11.40625 3.86718 0 0 29.08204z" 
      android:strokeLineCap="butt" 
      android:fillColor="#000000" 
      android:strokeLineJoin="miter" /> 
     <path 
      android:pathData="M401.71594 869.50507l-19.16015 0 0 -29.08204 19.16015 0 0 3.4375 -15.29297 0 0 7.96875 15.29297 0 0 3.4375 -15.29297 0 0 10.80079 15.29297 0 0 3.4375z" 
      android:strokeLineCap="butt" 
      android:fillColor="#000000" 
      android:strokeLineJoin="miter" /> 
     <path 
      android:pathData="M426.26672 869.50507l-18.39843 0 0 -29.08204 3.86718 0 0 25.64454 14.53125 0 0 3.4375z" 
      android:strokeLineCap="butt" 
      android:fillColor="#000000" 
      android:strokeLineJoin="miter" /> 
     <path 
      android:pathData="M448.53235 869.50507l-18.39844 0 0 -29.08204 3.86719 0 0 25.64454 14.53125 0 0 3.4375z" 
      android:strokeLineCap="butt" 
      android:fillColor="#000000" 
      android:strokeLineJoin="miter" /> 
     <path 
      android:pathData="M473.6886 843.76288q1.77734 1.95312 2.71484 4.78515 0.95703 2.83204 0.95703 6.42579 0 3.59375 -0.97656 6.44531 -0.95703 2.83203 -2.69531 4.72656 -1.79688 1.97266 -4.25781 2.96875 -2.44141 0.99609 -5.58594 0.99609 -3.06641 0 -5.58594 -1.01562 -2.5 -1.01563 -4.25781 -2.94922 -1.75781 -1.93359 -2.71485 -4.74609 -0.9375 -2.8125 -0.9375 -6.42578 0 -3.55469 0.9375 -6.36719 0.9375 -2.83203 2.73438 -4.84375 1.71875 -1.91406 4.25781 -2.92969 2.5586 -1.01562 5.56641 -1.01562 3.125 0 5.60547 1.03515 2.5 1.01563 4.23828 2.91016zm-0.35156 11.21094q0 -5.66407 -2.53907 -8.73047 -2.53906 -3.08594 -6.93359 -3.08594 -4.43359 0 -6.97266 3.08594 -2.51953 3.0664 -2.51953 8.73047 0 5.72265 2.57813 8.76953 2.57812 3.02734 6.91406 3.02734 4.33594 0 6.89453 -3.02734 2.57813 -3.04688 2.57813 -8.76953z" 
      android:strokeLineCap="butt" 
      android:fillColor="#000000" 
      android:strokeLineJoin="miter" /> 
     <path 
      android:pathData="M489.64563 840.42303l-0.50781 21.07422 -3.32032 0 -0.54687 -21.07422 4.375 0zm-0.19531 29.08204l-3.98438 0 0 -4.1211 3.98438 0 0 4.1211z" 
      android:strokeLineCap="butt" 
      android:fillColor="#000000" 
      android:strokeLineJoin="miter" /> 
     <path 
      android:pathData="M505.42688 840.42303l-0.50781 21.07422 -3.32032 0 -0.54687 -21.07422 4.375 0zm-0.19531 29.08204l-3.98438 0 0 -4.1211 3.98438 0 0 4.1211z" 
      android:strokeLineCap="butt" 
      android:fillColor="#000000" 
      android:strokeLineJoin="miter" /> 
     <path 
      android:pathData="M521.20813 840.42303l-0.50781 21.07422 -3.32032 0 -0.54687 -21.07422 4.375 0zm-0.19531 29.08204l-3.98438 0 0 -4.1211 3.98438 0 0 4.1211z" 
      android:strokeLineCap="butt" 
      android:fillColor="#000000" 
      android:strokeLineJoin="miter" /> 
    </group> 
</vector> 

它看起來非常像的每個字母「你好!!!」文字會被轉換成path以直接畫到canvas?!

所以我的問題是,是否有另一種方式顯示包含canvas文本的矢量圖形,因此能夠使用文本部分的資源?或者我堅持爲每種語言創建不同的圖形?

回答

4

你的結論是正確的。

矢量繪圖是完全基於路徑的。這是他們動畫的根本原因,因爲只有一條路徑正在改變。因此,他們不會將文字或圖像作爲輸入。

一個選項可能會通過文本(某些工具可能?)獲取.svg,然後將其轉換爲每種語言的xml。 如果您的目標是較大的本地化,矢量繪製可能不是正確的方法。

1

您將不得不使用一種將可本地化的內容與drawable分離的方法。

目前尚不清楚你到底在做什麼,但是如果你想要自定義按鈕,那麼你應該讓VectorDrawable成爲自定義按鈕設計。然後使用可繪製的背景作爲TextView的背景,或者將文本部分作爲TextView疊加在ImageView的頂部。