我有一个我想弹跳的圆圈,所以它会在宽度上扩展,然后在高度上坍塌,然后相反并且相同几次。这一切都适用于连续几个ScaleAnimations。问题是,我希望pivotY成为视图的底部。在这种情况下,每次新动画开始时,它都会将枢轴点重置为中心。这是我的代码:
bounceAnimationPartOne = new ScaleAnimation(1.0f, 1.0f, 1.62f, 0.62f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
bounceAnimationPartOne.setDuration(45);
bounceAnimationPartOne.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
view.startAnimation(bounceAnimationPartTwo);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
bounceAnimationPartTwo = new ScaleAnimation(1.62f, 0.62f, 0.76f, 1.3f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
bounceAnimationPartTwo.setDuration(90);
bounceAnimationPartTwo.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
view.startAnimation(bounceAnimationPartThree);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
bounceAnimationPartThree = new ScaleAnimation(0.76f, 1.3f, 1.23f, 0.81f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
bounceAnimationPartThree.setDuration(105);
bounceAnimationPartThree.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
view.startAnimation(bounceAnimationPartFour);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
bounceAnimationPartFour = new ScaleAnimation(1.23f, 0.81f, 1.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
bounceAnimationPartFour.setDuration(60);
答案 0 :(得分:1)
我最终编写了自己的自定义动画,它基本上连续执行了4个缩放动画:
public class BounceAnimation extends Animation {
private final List<Float> expansionValuesX;
private final List<Float> expansionValuesY;
private final List<Float> timing;
private int currentTimingIndex;
private float currentTimingSum;
private float pivotX;
private float pivotY;
public BounceAnimation(List<Float> expansionValuesX, List<Float> expansionValuesY, List<Float> timing, int duration) {
this.expansionValuesX = expansionValuesX;
this.expansionValuesY = expansionValuesY;
this.timing = timing;
setDuration(duration);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float sx = 1.0f;
float sy = 1.0f;
if (currentTimingIndex < timing.size() - 1 && interpolatedTime >= currentTimingSum + timing.get(currentTimingIndex)) {
currentTimingSum += timing.get(currentTimingIndex);
currentTimingIndex++;
}
float currentFromX = expansionValuesX.get(currentTimingIndex);
float currentFromY = expansionValuesY.get(currentTimingIndex);
float currentToX = expansionValuesX.get(currentTimingIndex + 1);
float currentToY = expansionValuesY.get(currentTimingIndex + 1);
float currentInterpolatedTime = (interpolatedTime - currentTimingSum) / timing.get(currentTimingIndex);
if (currentFromX != 1.0f || currentToX != 1.0f) {
sx = currentFromX + ((currentToX - currentFromX) * currentInterpolatedTime);
}
if (currentFromY != 1.0f || currentToY != 1.0f) {
sy = currentFromY + ((currentToY - currentFromY) * currentInterpolatedTime);
}
if (pivotX == 0 && pivotY == 0) {
t.getMatrix().setScale(sx, sy);
} else {
t.getMatrix().setScale(sx, sy, pivotX, pivotY);
}
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
pivotX = resolveSize(Animation.RELATIVE_TO_SELF, 0.5f, width, parentWidth);
pivotY = resolveSize(Animation.RELATIVE_TO_SELF, 1.0f, height, parentHeight);
currentTimingIndex = 0;
currentTimingSum = 0;
}
}
答案 1 :(得分:0)
仅仅因为你动画一些东西并不意味着你已经改变了它(见previous discussion)。
您可能需要修改onAnimationEnd()
方法,以便在开始播放另一个动画之前使动画的效果永久化。
或者,使用AnimationSet并为集合中的第二个和第三个动画设置startOffset。或者,更简单的是,使用AnimatorSet.Builder。