支持库中的AnimatedVectorDrawable和“pathData”的动画 [英] AnimatedVectorDrawable in Support Library and animation of "pathData"

查看:405
本文介绍了支持库中的AnimatedVectorDrawable和“pathData”的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用支持库23.2.0 中的动画矢量,如下所示:

I am using animated vectors from Support Library 23.2.0, like this:

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

我正在尝试动画 pathData (将线条变换为另一个)。我的代码看起来像这样。

I am trying to animate "pathData" (morphing lines one to another). My code looks like this.

drawable / ic_done.xml:

drawable/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>

drawable / ic_done_animated.xml:

drawable/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>

animator / tick_path_animation.xml:

animator/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

It works well on newer device with API level 21 but I have a problem on device with API level 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

According to an article android-support-library-232 the animated vectors (AnimatedVectorDrawableCompat) should be supported back to API level 11.

tick_path_animation.xml 读取 valueFrom 属性时,它看起来失败了。可能不支持此属性类型pathType(还是?)。知道如何解决这个问题吗?

It looks like it fails while reading valueFrom attribute from tick_path_animation.xml. This attribute type "pathType" is probably not supported (yet?). Any idea how to solve this?

推荐答案

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

Sorry, this will not work with the current version of the Support Library(23.2.0).

参见 Chris Banes的文章


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

There are also some limitations to what kind of things animated vectors can do when running on platforms < API 21. The following are the things which do not work currently on those platforms:

Path Morphing(PathType评估程序)。这用于将一条路径变形为另一条路径。

Path Morphing (PathType evaluator). This is used for morphing one path into another path.

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

Path Interpolation. This is used to defined a flexible interpolator (represented as a path) instead of the system defined ones like LinearInterpolator.

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

Move along path. This is rarely used. The geometry object can move around, along an arbitrary path.

因此目前不支持设置pathData或Path Morphing的动画。

So animating the pathData, or 'Path Morphing' isn't currently supported.

更新:

Frank的评论:

Update:
Frank's comment:


最终在支持库25。4。0(2017年6月)中修复:
AnimatedVectorDrawableCompat支持路径
变形和路径插值

This is finally fixed in support lib 25.4.0 (June 2017): "Path morphing and path interpolation are supported in AnimatedVectorDrawableCompat"

这篇关于支持库中的AnimatedVectorDrawable和“pathData”的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆