一起在不同视图上运行动画

时间:2016-10-01 06:38:56

标签: android animation

我需要动画2个视图,我希望动画一起开始。这是我的两个动画:

ScaleAnimation scaleAnimation1 = new ScaleAnimation(image.getScaleX(), 1.0f, image.getScaleY(), 1.0f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(300);
scaleAnimation.setFillAfter(true);
scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {

    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});
image.startAnimation(scaleAnimation);

ScaleAnimation scaleAnimation2 = new ScaleAnimation(logo.getScaleX(), 1.0f, logo.getScaleY(), 1.0f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(300);
scaleAnimation.setFillAfter(true);
scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {

    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});
logo.startAnimation(scaleAnimation);

我该怎么办?我需要以编程方式进行。

P.S。我没有多少动画经验。

1 个答案:

答案 0 :(得分:0)

以下是您的样本)

public class AnimationUtils {
    public static void applyAnimation(Context context, View view, int animationResource, int animationOffsetMilisec){
        Animation anim = android.view.animation.AnimationUtils.loadAnimation(context, animationResource);
        anim.setInterpolator(new AccelerateDecelerateInterpolator());
        anim.setStartOffset(animationOffsetMilisec);
        if(view != null) {
            view.setAnimation(anim);
            view.startAnimation(anim);
        }
    }
}

animation_tap.xml必须位于res / anim /

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator">
    <scale
        android:duration="100"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="1.00"
        android:toYScale="0.95"
        android:pivotX="50%"
        android:pivotY="50%"/>
    <scale
        android:duration="200"
        android:fromXScale="1.0"
        android:fromYScale="0.95"
        android:toXScale="1.0"
        android:toYScale="1.1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="100"/>
</set>

现在您可以使用的任何代码:

AnimationUtils.applyAnimation(context,view1,R.anim.animation_tap,0);
AnimationUtils.applyAnimation(context,view2,R.anim.animation_tap,0);
AnimationUtils.applyAnimation(context,view3,R.anim.animation_tap,0);
//...