Android缩小并增长顺序动画

时间:2013-03-28 15:14:15

标签: android animation

我想制作一个连续动画,其中两个按钮缩小,直到它们消失,然后它们再次增长到原始大小。 当我运行它时按钮就会消失而没有任何动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fillAfter="true">

    <scale
        android:fromXScale="1"
        android:toXScale="0"
        android:fromYScale="1"
        android:toYScale="0"
        android:duration="400"
        android:pivotX="50%"
        android:pivotY="50%"
        /> 

    <scale
        android:startOffset="700"
        android:fromXScale="0"
        android:toXScale="1"
        android:fromYScale="0"
        android:toYScale="1"
        android:duration="400"
        android:pivotX="50%"
        android:pivotY="50%"
     /> 

</set>  

Animation sgAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shrink_grow);
btPrimary.startAnimation(sgAnimation);
btSecondary.startAnimation(sgAnimation);

6 个答案:

答案 0 :(得分:11)

以下是可以无休止地使用的增长/缩小动画。 设置动画repeatcount当然不会工作,但你可以自己添加一个计数器来阻止它。

    final ScaleAnimation growAnim = new ScaleAnimation(1.0f, 1.15f, 1.0f, 1.15f);
    final ScaleAnimation shrinkAnim = new ScaleAnimation(1.15f, 1.0f, 1.15f, 1.0f);

    growAnim.setDuration(2000);
    shrinkAnim.setDuration(2000);

    viewToAnimate.setAnimation(growAnim);
    growAnim.start();

    growAnim.setAnimationListener(new AnimationListener()
    {
        @Override
        public void onAnimationStart(Animation animation){}

        @Override
        public void onAnimationRepeat(Animation animation){}

        @Override
        public void onAnimationEnd(Animation animation)
        {
            viewToAnimate.setAnimation(shrinkAnim);
            shrinkAnim.start();
        }
    });
    shrinkAnim.setAnimationListener(new AnimationListener()
    {
        @Override
        public void onAnimationStart(Animation animation){}

        @Override
        public void onAnimationRepeat(Animation animation){}

        @Override
        public void onAnimationEnd(Animation animation)
        {
            viewToAnimate.setAnimation(growAnim);
            growAnim.start();
        }
    });     

答案 1 :(得分:5)

使用android:repeatMode =“reverse”android:repeatCount =“infinite”然后它将重复无限时间我在我的补间动画中使用它的工作

答案 2 :(得分:1)

使用以下代码,这是工作代码。将此shrink_grow.xml复制到anim文件夹中。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="false"
android:fillBefore="false"
android:shareInterpolator="true" >

<scale
    android:duration="1000"
    android:fillAfter="true"
    android:fillBefore="false"
    android:fillEnabled="true"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="0.0"
    android:toYScale="0.0" />
<scale
    android:duration="1000"
    android:fillAfter="true"
    android:fillBefore="false"
    android:fillEnabled="true"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:startOffset="1000"
    android:toXScale="1.0"
    android:toYScale="1.0" />

</set>
代码中的

Animation sgAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shrink_grow);
btPrimary.startAnimation(sgAnimation);
btSecondary.startAnimation(sgAnimation);

答案 3 :(得分:1)

        final ScaleAnimation growAnim = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.5f, Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_SELF, 0.5F);
    final ScaleAnimation shrinkAnim = new ScaleAnimation(1.5f, 1.0f, 1.5f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_SELF, 0.5F);

    growAnim.setDuration(2000);
    shrinkAnim.setDuration(2000);

    view.setAnimation(growAnim);
    growAnim.start();

    growAnim.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            view.setAnimation(shrinkAnim);
            shrinkAnim.start();
        }
    });
    shrinkAnim.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            view.setAnimation(growAnim);
            growAnim.start();
        }
    });

答案 4 :(得分:0)

val scaleDown = ObjectAnimator.ofPropertyValuesHolder(
    view,
    PropertyValuesHolder.ofFloat("scaleX", 0.5f),
    PropertyValuesHolder.ofFloat("scaleY", 0.5f)
)
scaleDown.duration = 2000
scaleDown.repeatMode = ValueAnimator.REVERSE
scaleDown.repeatCount = ValueAnimator.INFINITE
scaleDown.start()

演示

答案 5 :(得分:0)

将此添加到anim文件夹中的xml中。

    <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:interpolator="@android:anim/linear_interpolator">
    <scale
        android:duration="1000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.15"
        android:toYScale="1.15"
        android:repeatCount="infinite"
        android:repeatMode="reverse"/>
</set>