2013-02-02 628 views
0

我有一个圆圈,我想弹跳,所以它会在宽度上展开,然后在高度上折叠,然后反向和几次相同。这一切都适用于连续的几个ScaleAnimations。问题是,我想pivotY成为视图的底部。在这种情况下,每当新动画开始时,它都会将支点重置到中心。这里是我的代码:Android圆圈弹跳动画

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

回答

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

} 
0

仅仅因为你的动画的东西并不意味着你已经改变了它(见previous discussion)。

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

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

+0

是的,谢谢,我理解这一点。但我希望有连续的动画制作方法。 –

+0

我认为你正在寻找的是一个AnimationSet?第二和第三个动画需要startOffset。 http://developer.android.com/reference/android/view/animation/AnimationSet.html –

+0

您是否尝试过使用AnimatorSet.Builder? –