Android动画不重复 [英] Android animation does not repeat

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

问题描述

我正在尝试制作可重复多次(或无限次)的简单动画.
android:repeatCount 好像不行!
这是 /res/anim/first_animation.xml 中我的动画资源:

I'm trying to make simple animation that would repeat several times (or infinitely).
It seems that android:repeatCount does not work!
Here is my animation resource from /res/anim/first_animation.xml :

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false"
    android:repeatCount="infinite"
    >
    <scale
        android:interpolator="@android:anim/decelerate_interpolator"
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="1.2"
        android:toYScale="1.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false" />
    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:startOffset="500"
        android:duration="500"
        android:fromXScale="1.2"
        android:fromYScale="1.2"
        android:toXScale="1.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false" />
</set>

首先它应该在 500 毫秒内将图像从 1.0 缩放到 1.2.
然后在 500 毫秒内将其缩放回 1.0.
以下是我的使用方法:

First it should scale image from 1.0 to 1.2 size in 500 ms.
And then scale it back to 1.0 in 500 ms.
Here is how I'm using it:

Animation firstAnimation = AnimationUtils.loadAnimation(this, R.anim.first_animation);
imgView.startAnimation(firstAnimation);

它进行一个循环,然后完成.
它先放大,然后缩小,然后停止.

我怎样才能按预期完成这项工作?

It makes one cycle and then finishes.
It scales up, then scales down ans then stops.

How can I make this work as intended?

推荐答案

更新: 早在 2011 年 9 月,一位 Android 工程师就在很大程度上解决了这个问题.XML 中被忽略的属性现在可以工作,除了 repeatCountfillEnabled 仍然被忽略(出于某种原因故意).这意味着不幸的是,重复 AnimationSet 仍然不容易.

Update: Back in Sep, 2011 an Android engineer fixed this issue for the most part. The attributes that were ignored in XML now work, with the exception of repeatCount and fillEnabled which are still ignored (on purpose for some reason). This means it still isn't easy to repeat an AnimationSet unfortunately.

有关详细信息,请参阅更新文档中的概述(解释哪些属性被忽略,哪些有效,哪些传递给子级).要更深入地了解 fillAfterfillBeforefillEnabled 的实际作用,请参阅工程师 (Chet Haase) 的博客文章此处.

For details please see the overview in the updated docs (explains which attributes are ignored, which work, and which are passed onto children). And for a deeper understanding of what fillAfter, fillBefore, and fillEnabled actually do, see the engineer's (Chet Haase) blog post about it here.

扩展 Pavel 和其他人的答案:<set> 标签确实存在可笑的错误.它无法正确处理 repeatCount 和许多其他属性.

To expand upon answers by Pavel and others: it is true that the <set> tag is ridiculously buggy. It can't deal correctly with repeatCount and a number of other attributes.

我花了几个小时弄清楚它能处理什么和不能处理什么,并在这里提交了错误报告/问题:问题 17662

I spent a few hours figuring out what it can and can't deal with and have submitted a bug report/issue here: Issue 17662

总而言之(这涉及AnimationSets):

In summary (this concerns AnimationSets):

setRepeatCount()/android:repeatCount

setRepeatCount() / android:repeatCount

此属性(以及 repeatMode)在代码或 XML 中不起作用.这使得重复一整套动画变得困难.

This attribute (as well as repeatMode) does not work in code or XML. This makes repeating an entire set of animations difficult.

setDuration()/android:duration

setDuration() / android:duration

在代码 WORKS 中的 AnimationSet 上设置它(覆盖子动画的所有持续时间),但当包含在 XML 中的标记中时不设置

Setting this on an AnimationSet in code WORKS (overrides all durations of children animations), but not when included in the tag in XML

setFillAfter()/android:fillAfter

setFillAfter() / android:fillAfter

这适用于标签的代码和 XML.奇怪的是,我无需将 fillEnabled 设置为 true 即可使用它.

This works in both code and XML for the tag. Strangely I have gotten it to also work without the need to set fillEnabled to true.

setFillBefore()/android:fillBefore

setFillBefore() / android:fillBefore

在代码和 XML 中似乎没有影响/被忽略

Seems to have no effect/ignored in both code and XML

setFillEnabled()/android:fillEnabled

setFillEnabled() / android:fillEnabled

在代码和 XML 中似乎没有影响/被忽略.即使不包含 fillEnabled 或将 fillEnabled 设置为 false,我仍然可以让 fillAfter 工作.

Seems to have no effect/ignored in both code and XML. I can still get fillAfter to work even without including fillEnabled or setting fillEnabled to false.

setStartOffset()/android:startOffset

setStartOffset() / android:startOffset

这仅适用于代码而不适用于 XML.

This works only in code and not XML.

这篇关于Android动画不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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