【问题标题】:Animating path of Vector Drawable in AndroidAndroid中Vector Drawable的动画路径
【发布时间】:2017-09-04 09:33:19
【问题描述】:

我正在尝试在我的 Android 应用程序中为我从 Illustrator 导出的 SVG 文件设置动画,方法是对字符的行或路径进行动画处理,以向用户展示如何正确书写字符(例如:日文和中文字符)。但是当我运行该应用程序时,它只会为字符的边缘/笔划设置动画并恢复其原始状态。有没有人以前经历过这种情况?

这是我的 SVG 可绘制对象

<vector xmlns:android="http://schemas.android.com/apk/res/android"
     android:width="300dp"
     android:height="300dp"
     android:viewportWidth="300.0"
     android:viewportHeight="300.0">
 <path
     android:name="invertu"
     android:fillColor="#FF000000"
     android:pathData="M21.23,263.43c0.68,-14.76 6.03,-28.9 
9.58,-43.12c3.82,-15.3 6.51,-30.8 10.68,-46.04c4.22,-15.46 9.06,-30.74 
14.02,-45.97c3.7,-11.38 7.53,-22.54 14.07,-32.66c14.19,-21.95 43.47,-29.83 
67.74,-33.8c13.6,-2.22 27.52,-2.66 41.27,-2.32c10.71,0.27 21.44,2.13 
31.29,6.5c28.73,12.76 43.98,42.42 53.04,70.95c5.67,17.85 9,36.29 
12.31,54.69c4.36,24.27 7.8,48.7 8.3,73.39c0.19,9.66 15.19,9.68
 15,0c-0.65,-32.46 -6.33,-64.51 -12.34,-96.33c-4.07,-21.5 -9.94,-42.7
 -19.22,-62.57c-8.76,-18.76 -21.57,-35.7 -38.92,-47.24c-20.15,-13.41 
-44.1,-15.18 -67.61,-14.26c-24.9,0.97 -49.66,5.88 -72.26,16.55c-20.76,9.8 
-33.38,26.41 -41.69,47.37c-4.61,11.65 -8.1,23.81 -11.83,35.77c-4.22,13.53 
-8.13,27.16 -11.53,40.92c-3.27,13.2 -5.45,26.61 -9.05,39.73c-3.47,12.65 
-7.23,25.26 -7.84,38.45C5.79,273.11 20.79,273.06 21.23,263.43L21.23,263.43z"
     android:strokeLineCap="round"
     android:strokeLineJoin="round"
     android:strokeWidth="3"
     android:trimPathEnd="0"/> </vector>

这是我的动画矢量

<animated-vector
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:aapt="http://schemas.android.com/aapt"
  android:drawable="@drawable/ic_ba">

  <target android:name="invertu">
    <aapt:attr name="android:animation">
      <objectAnimator
        android:duration="1000"
        android:interpolator="@android:interpolator/fast_out_slow_in"
        android:propertyName="trimPathEnd"
        android:valueFrom="0"
        android:valueTo="1"/>
    </aapt:attr>
  </target>

我从 Adob​​e Illustrator 导出的 VSG 在中心变得透明,只有轮廓从一点到另一端有动画效果。 TIA

【问题讨论】:

    标签: android svg android-animation


    【解决方案1】:

    trimPathStarttrimPathEnd 仅用于设置路径笔划(轮廓)的开始和结束。如果您希望路径的填充部分具有动画效果,则必须以另一种方式进行。

    如果您的角色中的线条形状具有恒定宽度,那么只需将它们绘制为粗线 - 而不是像现在这样的轮廓。如果你这样做,线条将能够使用 trimPath 方法进行动画处理。

    【讨论】:

    • 是的,我把它画成一条实线,但是当我将它导入到安卓系统时,它变得透明了,我想知道它是否与插画家设置或 android studio 本身有关
    • 不看就很难说。
    • 您好,请在我的问题上查看来自 android studio 的矢量预览的附件图像。从 Illustrator 导出的矢量可绘制对象应该是实心的,但是当我将它导入到 android studio 时,它在中心变得透明,并且只呈现轮廓。
    • 正如我所说,如果你想使用 trimPathEnd 方法来绘制线条,那么你的路径需要是从起点到终点的单线。像你这样的轮廓形状是行不通的。如果您需要线条具有可变粗细,那么您将需要使用不同的方法。 You may have to switch to a WebView and use SVG and Javascript. 或者您可以使用 Android Canvas 调用重现相同的技术。
    最近更新 更多