代码
public class AnimationUtil {
public enum AnimationType {
PRESS, ROTATION
}
//存放View 是否在执行动画中
private static HashMap<String, Boolean> animationMap = new HashMap<>();
//按下动画
public static void doAnimation(View view, AnimationType type) {
String name = view.getId() + type.name();
if (animationMap.get(name) == null || animationMap.get(name)) {
switch (type) {
case PRESS:
doPressAnimation(view);
break;
case ROTATION:
doRotationAnimation(view);
break;
}
}
}
private static void doPressAnimation(final View view) {
float y = view.getY();
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "y", y, y + 30, y);
animator.setDuration(500);
animator.setRepeatCount(0);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
animationMap.put(view.getId() + AnimationType.PRESS.name(), false);
}
@Override
public void onAnimationEnd(Animator animation) {
animationMap.put(view.getId() + AnimationType.PRESS.name(), true);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animator.setInterpolator(new AccelerateDecelerateInterpolator());
animator.start();
}
private static void doRotationAnimation(final View view) {
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0, 20f, -20f, 20f, -20f, 0);
animator.setDuration(500);
animator.setRepeatCount(0);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
animationMap.put(view.getId() + AnimationType.ROTATION.name(), false);
}
@Override
public void onAnimationEnd(Animator animation) {
animationMap.put(view.getId() + AnimationType.ROTATION.name(), true);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animator.setInterpolator(new AccelerateDecelerateInterpolator());
animator.start();
}
}
复制代码
用法
AnimationUtil.doAnimation(View, AnimationUtil.AnimationType.PRESS);
AnimationUtil.doAnimation(View, AnimationUtil.AnimationType.ROTATION);
复制代码