Android收缩和增长顺序动画

Android收缩和增长顺序动画,android,animation,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"

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

<?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>  

下面是一个可以(无休止地)工作的生长/收缩动画。 设置动画重复计数当然不起作用,但您可以自己添加一个计数器来停止它

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

使用
android:repeatMode=“reverse”
android:repeatCount=“infinite”
然后它将无限重复我在我的tween动画中使用它的时间,它的工作使用以下代码,这是工作代码。将此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>
    <?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>
演示


将其添加到动画文件夹中的xml中

<?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>
    <?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>


退出第二个
缩放
动画时会发生什么?按钮缩小了吗?是的,两个动画都是单独工作的。关于为什么动画是连续的,请给出注释和解释。我使用了问题中的代码,并做了一些小改动,你可以看到它们的区别,区别是1。在fromXScale等中使用浮点值。2.fillAfter-fillBefore值。我不知道它不起作用的确切原因,也不知道它起作用的原因。我测试了这个解决方案——它简单有效!
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()
    <?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>