11

我正在使用Support Library 23.2.0中的动画矢量,如下所示:

compile 'com.android.support:support-vector-drawable:23.2.0'
compile 'com.android.support:animated-vector-drawable:23.2.0'

我正在尝试为“ 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

从tick_path_animation.xml读取valueFrom属性时似乎失败了。可能不支持此属性类型“pathType”(还没有?)。知道如何解决这个问题吗?

4

2 回答 2

23

抱歉,这不适用于当前版本的支持库 (23.2.0)。

请参阅Chris Banes 文章

在平台 < API 21 上运行时,动画矢量可以做什么样的事情也有一些限制。以下是目前在这些平台上不起作用的事情:

路径变形(PathType 评估器)。这用于将一条路径变形为另一条路径。

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

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

因此,当前不支持对 pathData 或“路径变形”进行动画处理。

更新:
弗兰克的评论:

这最终在支持库 25.4.0(2017 年 6 月)中得到修复:“AnimatedVectorDrawableCompat 支持路径变形和路径插值”

于 2016-03-04T22:30:10.340 回答
4

API 16 动画
上面动画中的圆形“闪光”(在图像的中心)是我按下屏幕开始变形。
充气 Drawable

`VectorDrawable`并且`AnimatedVectorDrawable`在这个支持库(`vector-compat`)中可以通过这种方式进行膨胀:

  • 调用静态getDrawable()方法:
//这只会膨胀一个以<vector>为根元素的drawable
VectorDrawable.getDrawable(context, R.drawable.ic_arrow_vector);

//这只会膨胀一个以 <animated-vector> 作为根元素的drawable
AnimatedVectorDrawable.getDrawable(context, R.drawable.ic_arrow_to_menu_animated_vector);

// 这将膨胀任何可绘制对象并自动回退到 api 21+ 设备上的棒棒糖实现
ResourcesCompat.getDrawable(context, R.drawable.any_drawable);

如果在 java 代码中对 Drawable 进行膨胀,建议始终使用它,ResourcesCompat.getDrawable()因为这会在适用时处理 Lollipop 回退。这允许系统缓存 Drawable ConstantState,因此效率更高。
库(`vector-compat`)具有以下变形(双向​​)动画:

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

  • 如您所见,我在API 16手机上制作了上图:

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

    在这里查看 github READMEvector-compathttps ://github.com/wnafee/vector-compat如果您将它与您的应用程序模块(通常在文件末尾)合并,
    这将解决您的问题(下降到):API 14build.gradle dependencies

    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'
    }
    
    于 2016-12-31T15:30:21.340 回答