gpt4 book ai didi

android - 如何使动画集重复?

转载 作者:行者123 更新时间:2023-11-29 18:57:40 25 4
gpt4 key购买 nike

我的 AnimationSet 中有 2 个动画。它们需要按顺序发生并且持续时间不同。如何让我的 AnimationSet 无限重复?

public void cartoonBreathing(ImageView v, int inhale_time, int exhale_time){
AnimationSet breathing_set = new AnimationSet(true);

Animation inhale = new ScaleAnimation(1.0f,2f,1.0f,2f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
inhale.setFillAfter(true);
inhale.setDuration(inhale_time * 1000);

Animation exhale = new ScaleAnimation(1.0f,0.5f,1.0f,0.5f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
exhale.setFillAfter(true);
exhale.setDuration(exhale_time * 1000);

breathing_set.addAnimation(inhale);
breathing_set.addAnimation(exhale);

//breathing_set.setRepeatMode(Animation.RESTART); DOES NOT WORK
//breathing_set.setRepeatMode(2); DOES NOT WORK
//breathing_set.setRepeatCount(Animation.INFINITE);

v.startAnimation(breathing_set);

}

最佳答案

引自documentationAnimationSet 上:

The way that AnimationSet inherits behavior from Animation is important to understand. Some of the Animation attributes applied to AnimationSet affect the AnimationSet itself, some are pushed down to the children, and some are ignored, as follows:

  • duration, repeatMode, fillBefore, fillAfter: These properties, when set on an AnimationSet object, will be pushed down to all child animations.
  • repeatCount, fillEnabled: These properties are ignored for AnimationSet.
  • startOffset, shareInterpolator: These properties apply to the AnimationSet itself.

这解释了为什么您的尝试不成功。相反,您可以使用 Animation.AnimationListener 在动画完成后重新启动动画:

public void cartoonBreathing(final ImageView v, int inhale_time, int exhale_time){
final AnimationSet breathing_set = new AnimationSet(true);

Animation inhale = new ScaleAnimation(1.0f,2f,1.0f,2f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
inhale.setFillAfter(true);
inhale.setDuration(inhale_time * 1000);

Animation exhale = new ScaleAnimation(1.0f,0.5f,1.0f,0.5f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
exhale.setFillAfter(true);
exhale.setDuration(exhale_time * 1000);

breathing_set.addAnimation(inhale);
breathing_set.addAnimation(exhale);

breathing_set.setAnimationListener(new Animation.Animation.AnimationListener()
{
@Override
public void onAnimationStart(Animation animation){}

@Override
public void onAnimationEnd(Animation animation)
{
v.startAnimation(breathing_set);
}

@Override
public void onAnimationRepeat(Animation animation){}
});

v.startAnimation(breathing_set);
}

请注意,ImageViewAnimationSet 必须声明为 final 才能从 AnimationListener< 中访问

如果应用停止,也许考虑在 ImageView 上调用 clearAnimation()

关于android - 如何使动画集重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49697896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com