在运行时添加动画 [英] Add animation at run time

查看:73
本文介绍了在运行时添加动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时为 AnimatedVectorDrawable 制作动画,而不使用 .xml 文件。实际上,我使用的 .xml 文件与文档的示例所示相同:

I want to animate my AnimatedVectorDrawable at runtime without using .xml files. Actually I'm using .xml files same way as documentation's samples shows:

AnimatedVectorDrawable

所以,我具有 vector_drawable.xml 包含< vector> 和嵌套的< group> < path> 定义了形状。

So, I have vector_drawable.xml contains<vector> with nested <group> and <path> which defines a shape.

对于此向量,我有 animated_vector_drawable.xml 包含< animated-vector> ,并将 android:animation 分配给< target>

For this vector I have animated_vector_drawable.xml contains <animated-vector> with android:animation assinged to <target>.

最后一步是使用< objectAnimator> <定义动画文件 rotation.xml / code>由 animated_vector_drawable.xml

Last step is define an animation file rotation.xml using <objectAnimator> which is used by animated_vector_drawable.xml

使用一切正常,但是出现问题,当我需要用许多不同或相似的动画创建许多不同的形状(向量)时,因为这会生成许多 .xml 文件。

Everything works fine, but the problem appears, when I need to create many different shapes (vectors) with many different or similar animations, because this generate many .xml files.


  1. 我无法从一个 .xml中包含准备好的和准备好的< vector> / code>文件复制到另一个文件(某种< include> 标记),因此我需要将相同的代码复制到另一个文件。

  1. I can't include ready and prepared <vector> from one .xml file to another (some kind of <include> tag) so i need to copy the same code to another files. It is very annoying.

如果我想为几个< target> 使用相同的动画元素,但每个动画都必须具有特征不同的延迟或任何属性值( alpha 旋转插值器 ...),我必须创建一个新的 .xml 文件,其中包含< objectAnimator> ,而该属性值已更改使用相同的一个文件,其属性值已更改。这也很烦人。

If I want to use the same animation for few <target> elements but each animation must have f.e. different delay or any property value (alpha, rotation, interpolator...) , I must create new .xml file contains <objectAnimator> with changed one property value instead of use the same, one file with changed property value. It's also annoying.

我发现我可以使用 ObjectAnimator 并设置 alpha & fillColor AnimatedVectorDrawable ,但是当我要更改它时,有一个问题是 translateX 翻译Y 旋转或其他任何属性。有没有 .xml 的方法?我只想访问< group>

I discovered that I can use ObjectAnimator and set alpha & fillColor for AnimatedVectorDrawable but there is a problem when I want to change it's translateX, translateY, rotation or any other properties. Is there a way to do this without .xml. I just want to have access to <group>


推荐答案

您使用的构造函数会创建具有绝对值(像素)的动画。

Constructor that you used creates animation with absolute values (pixels).

TranslateAnimation in = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0, 0.0f);

尝试使用Animation.RELATIVE_TO_PARENT来满足您的需求。

Try use Animation.RELATIVE_TO_PARENT to fit your needs.

这篇关于在运行时添加动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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