1

我有一个想要反弹的圆圈,所以它会在宽度上扩大,在高度上塌陷,然后反过来,同样的几次。这一切都适用于连续的几个 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);
4

2 回答 2

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;
    }

}
于 2013-02-03T07:27:10.193 回答
0

仅仅因为你为某些东西设置了动画并不意味着你已经改变了它(参见前面的讨论)。

在开始另一个动画之前,您可能需要修改该onAnimationEnd()方法以使动画效果永久化。

或者,使用AnimationSet并为集合中的第二个和第三个动画设置 startOffset。或者,更简单的是,使用 AnimatorSet.Builder。

于 2013-02-02T02:45:18.483 回答