9

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

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

6 回答 6

16
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()

演示

于 2019-05-30T07:24:12.457 回答
11

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

    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();
        }
    });     
于 2013-04-26T08:31:37.787 回答
5

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

于 2013-04-26T09:22:46.110 回答
1

Use the following code, this is the working code. copy this shrink_grow.xml in anim folder.

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

in the code:

Animation sgAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shrink_grow);
btPrimary.startAnimation(sgAnimation);
btSecondary.startAnimation(sgAnimation);
于 2014-06-23T13:07:17.190 回答
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();
        }
    });
于 2016-04-10T10:01:37.817 回答
1

将此添加到 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>
于 2020-05-09T20:34:31.207 回答