我有一些文本视图,我想制作MSN的Buzz效果。
我的计划是:
- 将视图向左移动10dip,
- 将其移回起始位置
- 之后将其向上移动10dip
- 再次移回起始位置
- 向下移回
- 向左移动...以此类推。
我的意思是,我有一些按顺序执行的动作序列,我想将它们设置给一个视图。
我该如何做到这一点?
您可能指的是AnimatorSet(而不是AnimationSet)。根据文档所述:
该类按指定顺序播放一组
Animator
对象。可以设置动画一起播放、按顺序播放或在指定延迟后播放。向 AnimatorSet 添加动画有两种不同的方法:可以调用
playTogether()
或playSequentially()
方法一次性添加一组动画,也可以使用play(Animator)
结合 Builder 类中的方法逐个添加动画。
移动 view
-100px
并在 700ms
内消失,然后在 300ms
内消失的动画:
final View view = findViewById(R.id.my_view);
final Animator translationAnimator = ObjectAnimator
.ofFloat(view, View.TRANSLATION_Y, 0f, -100f)
.setDuration(700);
final Animator alphaAnimator = ObjectAnimator
.ofFloat(view, View.ALPHA, 1f, 0f)
.setDuration(300);
final AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(
translationAnimator,
alphaAnimator
);
animatorSet.start()
我有一个兼容sdk 15的类的开端,可以用来构建复杂的动画链,希望能对某些人有所帮助。你应该能够遵循设计模式来添加自己的方法。如果你这样做了,请在这里评论它们,我会更新答案,谢谢!
package com.stuartclark45.magicmatt.util;
import java.util.LinkedList;
import java.util.List;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.View;
/**
* Used to build complex animations for a view. Example usage bellow makes view move out to the
* right whilst rotating 45 degrees, then move out to the left.
*
* {@code
* int rotateDuration = 200;
* int rotation = 45;
* new AnimationBuilder(view)
* .translationX(100, rotateDuration)
* .rotateTo(rotation, rotateDuration)
* .then()
* .translationX(-200, rotateDuration)
* .start();
* }
*
* @author Stuart Clark
*/
public class AnimationBuilder {
private View view;
private List<Animator> setsList;
private List<Animator> buildingList;
public AnimationBuilder(View view) {
this.view = view;
this.setsList = new LinkedList<>();
this.buildingList = new LinkedList<>();
}
public AnimationBuilder rotateTo(float deg, long duration) {
buildingList.add(ObjectAnimator.ofFloat(view, "rotation", deg).setDuration(duration));
return this;
}
public AnimationBuilder translationX(int deltaX, long duration) {
buildingList.add(ObjectAnimator.ofFloat(view, "translationX", deltaX).setDuration(duration));
return this;
}
public AnimationBuilder translationY(int deltaX, long duration) {
buildingList.add(ObjectAnimator.ofFloat(view, "translationY", deltaX).setDuration(duration));
return this;
}
public AnimationBuilder then() {
createAniSet();
// Reset the building list
buildingList = new LinkedList<>();
return this;
}
public void start() {
createAniSet();
AnimatorSet metaSet = new AnimatorSet();
metaSet.playSequentially(setsList);
metaSet.start();
}
private void createAniSet() {
AnimatorSet aniSet = new AnimatorSet();
aniSet.playTogether(buildingList);
setsList.add(aniSet);
}
}
使用AnimationSet:
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(100);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(500);
set.addAnimation(animation);
view.startAnimation( set );
animatorSet.start()
。 - mehulmpt