9

动画我使用从支持库23.2.0动画载体,像这样:AnimatedVectorDrawable在支持库和 “pathData”

​​

我试图动画 “pathData”(将线条变形到另一个)。我的代码看起来像这样。

绘制/ ic_done.xml:

<?xml version="1.0" encoding="utf-8"?> 
<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:width="24dp" 
    android:height="24dp" 
    android:viewportHeight="24.0" 
    android:viewportWidth="24.0"> 
    <path 
     android:name="tick" 
     android:pathData="M4.8,12L9,16.2L20,8" 
     android:strokeColor="#FF000000" /> 
</vector> 

绘制/ ic_done_animated.xml:

<?xml version="1.0" encoding="utf-8"?> 
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:drawable="@drawable/ic_done"> 
    <target 
     android:name="tick" 
     android:animation="@animator/tick_path_animation" /> 
</animated-vector> 

动画/ tick_path_animation.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:ordering="sequentially"> 
    <objectAnimator 
     android:duration="200" 
     android:propertyName="pathData" 
     android:valueFrom="M4.8,12L4.8,12L4.8,12" 
     android:valueTo="M4.8,12L9,16.2L9,16.2" 
     android:valueType="pathType" /> 
    <objectAnimator 
     android:duration="200" 
     android:propertyName="pathData" 
     android:valueFrom="M4.8,12L9,16.2L9,16.2" 
     android:valueTo="M4.8,12L9,16.2L20,8" 
     android:valueType="pathType" /> 
</set> 

Java代码:

ImageView vImgAnimated = findByViewId(R.id.img); 
AnimatedVectorDrawableCompat animatedVector = AnimatedVectorDrawableCompat.create(getContext(), R.drawable.ic_done_animated); 
vImgAnimated.setImageDrawable(animatedVector); 
animatedVector.start(); 

它运作良好,新的设备上API级别21,但我有API级别16上装置了一个问题:

java.lang.NumberFormatException: Invalid int: "M4.8,12L4.8,12L4.8,12" 
    at java.lang.Integer.invalidInt(Integer.java:138) 
    at java.lang.Integer.parse(Integer.java:375) 
    at java.lang.Integer.parseInt(Integer.java:366) 
    at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:123) 
    at android.content.res.TypedArray.getInt(TypedArray.java:254) 
    at android.animation.AnimatorInflater.loadAnimator(AnimatorInflater.java:258) 
    at android.animation.AnimatorInflater.loadObjectAnimator(AnimatorInflater.java:161) 
    at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java:117) 
    at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java:126) 
    at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java:93) 
    at android.animation.AnimatorInflater.loadAnimator(AnimatorInflater.java:72) 
    at android.support.graphics.drawable.AnimatedVectorDrawableCompat.inflate(AnimatedVectorDrawableCompat.java:377) 
    at android.support.graphics.drawable.AnimatedVectorDrawableCompat.createFromXmlInner(AnimatedVectorDrawableCompat.java:162) 
    at android.support.graphics.drawable.AnimatedVectorDrawableCompat.create(AnimatedVectorDrawableCompat.java:142) 

根据文章android-support-library-232动画向量(AnimatedVectorDrawableCompat)应该得到支持回到API级别11

它看起来像读取时失败valueFrom属性来自tick_path_animation.xml。此属性类型“pathType”可能不受支持(但?)。任何想法如何解决这个问题?

+0

尝试通过wnafee使用Vector-Compat https://github.com/wnafee/vector-compat,我测试了路径变形,效果很好 –

+1

您的问题已在支持库25.4.0+:https:// developer中得到解决。 android.com/topic/libraries/support-library/revisions.html#25-4-0 – Frank

+0

对于平台上的路径变形 Tarek360

回答

23

对不起,这不支持当前版本的支持库(23.2.0)。

请参阅Chris Banes article

也有一定的局限性,以什么样的东西在平台上运行时的动画向量可以做< API 21.以下是不上这些平台目前的工作的事情:

路径变形算法(PathType评估)。这用于将一个路径变形为另一个路径。

路径插值。这用于定义灵活的插补器(表示为路径),而不是像LinearInterpolator这样的系统定义的插补器。

沿路径移动。这很少使用。几何对象可以沿着任意路径移动。

所以动画pathData或'Path Morphing'目前不支持。

更新:
弗兰克的评论:

这是最后固定在支持LIB 25.4.0(六月2017): “路径 变形和路径插值支持 AnimatedVectorDrawableCompat”

+0

非常感谢,我无法找到这些限制。 –

+0

@Lewis McGeary:你是否发现任何支持路径 –

+7

的版本的信息在24.2.0中仍然不起作用。它使得我的支持库中的'Compat'部分无用!在向API11声明兼容性之后相当恼火,但隐藏了不适用的“相关”细节。应该称为AnimatedVectorDrawablePartiallyCompat .. –

4

API 16 animation
上面的动画中(在图像的中心)的圆形“闪光”是我预先点击屏幕开始变形。

  • 调用静态getDrawable()方法:
//This will only inflate a drawable with <vector> as the root element 
VectorDrawable.getDrawable(context, R.drawable.ic_arrow_vector); 

//This will only inflate a drawable with <animated-vector> as the root element 
AnimatedVectorDrawable.getDrawable(context, R.drawable.ic_arrow_to_menu_animated_vector); 

// This will inflate any drawable and will auto-fallback to the lollipop implementation on api 21+ devices 
ResourcesCompat.getDrawable(context, R.drawable.any_drawable);

膨化绘制对象的

`VectorDrawable``AnimatedVectorDrawable`在此支持库(`矢量compat`)可以以这种方式被充气

如果在Java代码中绘制Drawable,建议始终使用ResourcesCompat.getDrawable(),因为这适用于处理棒棒堂后备。这允许系统缓存Drawable ConstantState,因此效率更高。
库(`矢量compat`)具有以下变形(双向​​)的动画:

  • 播放-暂停变形动画
  • 播放停止变形动画
  • 箭汉堡包菜单变形动画

  • 正如你所看到的,我公司生产的上面的图片我 API 16电话:

    import com.wnafee.vector.compat.AnimatedVectorDrawable; 
    mdrawable = (AnimatedVectorDrawable) AnimatedVectorDrawable.getDrawable(this.getApplicationContext(), R.drawable.consolidated_animated_vector); 
    

    看看GitHub的READMEvector-compat这里:https://github.com/wnafee/vector-compat
    这将解决您的问题(低至API 14)如果你用它合并您的应用程序模块的build.gradledependencies(通常在文件的结尾):

    dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    //Trying to FIX Binary XML file line #2: invalid drawable tag animated-vector 
        compile 'com.android.support:appcompat-v7:25.0.0' 
        compile 'com.android.support:design:25.0.0' 
    //not needed 
    // compile 'com.android.support:support-vector-drawable:25.0.0' 
        compile 'com.wnafee:vector-compat:1.0.5'//*******holy grail *******https://github.com/wnafee/vector-compat 
    // Failed to resolve: com.android.support:support-animated-vector-drawable:25.0.0 
    //not needed 
    // compile 'com.android.support:support-animated-vector-drawable:25.0.0' 
    }