Android:两个不同持续时间的动画(Android: Two animation together with different duration)

编程入门 行业动态 更新时间:2024-10-19 08:54:33
Android:两个不同持续时间的动画(Android: Two animation together with different duration)

我有两个不同持续时间的动画如下: 动画1:

TranslateAnimation trans1 = new TranslateAnimation(0, 0, 0, 500); trans1.setStartOffset(0); trans1.setDuration(5000); trans1.setFillAfter(true);

动画2:

TranslateAnimation trans2 = new TranslateAnimation(0, -100, -200, -200); trans2.setDuration(200); trans2.setRepeatCount(25); trans2.setFillAfter(true);

如您所见,这些动画有不同的持续时间。 我想在一次Anim1(持续时间5000)动画中从Y:0到500动画Anim2(持续时间200)循环25次。

我试过使用AnimationSet和addAnimation,但它不想工作。 你能给我一些提示如何解决这个问题?

我的代码:

AnimationSet rootSet = new AnimationSet(true); rootSet.setInterpolator(new AccelerateInterpolator()); rootSet.setRepeatMode(Animation.INFINITE); rootSet.setRepeatCount(200); rootSet.setDuration(5000); rootSet.setFillAfter(true); TranslateAnimation trans1 = new TranslateAnimation(0, 0, 0, 500); trans1.setStartOffset(0); trans1.setDuration(5000); trans1.setFillAfter(true); rootSet.addAnimation(trans1); AnimationSet rootSet2 = new AnimationSet(true); rootSet2.setInterpolator(new AccelerateInterpolator()); rootSet2.setRepeatMode(Animation.INFINITE); rootSet2.setRepeatCount(200); rootSet2.setDuration(200); TranslateAnimation trans2 = new TranslateAnimation(0, -100, -200, -200); trans2.setDuration(200); trans2.setRepeatCount(25); trans2.setFillAfter(true); rootSet2.addAnimation(trans2); rootSet.addAnimation(rootSet2); iv.startAnimation(rootSet);

编辑: 当我只播放trans2动画时,它会重复25次(水平方式)。 此外,我希望这个动画以垂直方式(trans1)动画(整个水平动画不仅是imageview)。

我希望这个描述会更清楚。

当我在一个AnimationSet中添加这两个动画时,第一个动画根本不起作用,而imageview(不是animationn)则线性转换为-200,500。

I have two animation with different duration like below: Anim 1:

TranslateAnimation trans1 = new TranslateAnimation(0, 0, 0, 500); trans1.setStartOffset(0); trans1.setDuration(5000); trans1.setFillAfter(true);

Anim 2:

TranslateAnimation trans2 = new TranslateAnimation(0, -100, -200, -200); trans2.setDuration(200); trans2.setRepeatCount(25); trans2.setFillAfter(true);

As you see these animation have different durations. I would like to animate Anim2 (duration 200) in loop 25 times during one time Anim1 (duration 5000) animation from Y: 0 to 500.

I've tried to do with AnimationSet and addAnimation but it doesn't want to work. Could You give me some tips how can I solve this problem?

My code:

AnimationSet rootSet = new AnimationSet(true); rootSet.setInterpolator(new AccelerateInterpolator()); rootSet.setRepeatMode(Animation.INFINITE); rootSet.setRepeatCount(200); rootSet.setDuration(5000); rootSet.setFillAfter(true); TranslateAnimation trans1 = new TranslateAnimation(0, 0, 0, 500); trans1.setStartOffset(0); trans1.setDuration(5000); trans1.setFillAfter(true); rootSet.addAnimation(trans1); AnimationSet rootSet2 = new AnimationSet(true); rootSet2.setInterpolator(new AccelerateInterpolator()); rootSet2.setRepeatMode(Animation.INFINITE); rootSet2.setRepeatCount(200); rootSet2.setDuration(200); TranslateAnimation trans2 = new TranslateAnimation(0, -100, -200, -200); trans2.setDuration(200); trans2.setRepeatCount(25); trans2.setFillAfter(true); rootSet2.addAnimation(trans2); rootSet.addAnimation(rootSet2); iv.startAnimation(rootSet);

EDIT: When I play only trans2 animation it repeats 25 times as I wanted (in horizontal way). And additionally I would like this animation to animate (whole horizontal animation not only imageview) in vertical way (trans1).

I hope that this description will be clearer.

When I added these two animations in one AnimationSet the first animation doesnt work at all and imageview (not animationn) is translated linear to -200,500.

最满意答案

现在我明白了你想做什么。 你可能更喜欢做多个动画 - 每个“线”一个。 添加AnimationListener以了解上一个AnimationListener何时结束( onAnimationEnd回调),向下跳一行并再次从右向左移动。

它们基本上都是相同的,只有fromYDelta和toYDelta值会因每个动画而改变。

Now I see what you want to do. You probably are better of doing multiple animations - one for each "line". Add an AnimationListener to know when the previous Animation ends (onAnimationEnd callback), jump down one line and go from right to left again.

They all basically are the same, just the fromYDelta and toYDelta values would change for each Animation.

更多推荐

本文发布于:2023-08-04 08:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412521.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:持续时间   两个   动画   Android   animation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!