使textView循环成为一个不断增长和缩小的动画

时间:2014-05-16 13:00:21

标签: android animation android-animation

我正在尝试使TextView增长然后缩小。这个过程应该无限重复。我如何实现这一目标?我的scale.xml看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <scale
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="2.0"
    android:toYScale="2.0"
    android:duration="1400" />

</set>

这是我的动画方法:

public void runAnimation() {
    Animation a = AnimationUtils.loadAnimation(this, R.anim.scale);
    a.setRepeatMode(Animation.REVERSE);
    a.setRepeatCount(Animation.INFINITE);
    a.reset();
    textView.clearAnimation();
    textView.startAnimation(a);
}

现在我的问题是TextView增长了,但随后立即转换为开始的Appereance。为什么它不会反转,缩小并重复这个过程?

3 个答案:

答案 0 :(得分:4)

试试这个:

public void runAnimation() {
    Animation a = AnimationUtils.loadAnimation(this, R.anim.scale);
    a.setRepeatMode(Animation.REVERSE);
    a.setRepeatCount(-1);
    textView.clearAnimation();
    textView.startAnimation(a);
}

修改

这是我自己的代码,现在正在运行:

像animate一样调用animate(textView)

public void animate (View view) {
    mAnimation = new ScaleAnimation(0.3f,0.5f,0.3f,0.5f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.45f);
    mAnimation.setDuration(300);
    mAnimation.setRepeatCount(-1);
    mAnimation.setRepeatMode(Animation.REVERSE);
    mAnimation.setInterpolator(new AccelerateInterpolator());
    mAnimation.setAnimationListener(new Animation.AnimationListener(){

        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    });
    view.setAnimation(mAnimation);
}

答案 1 :(得分:1)

尝试在某些博客上找到此内容。适合我。

 <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
      android:interpolator="@android:anim/linear_interpolator">
    <scale
       android:fromXScale="0.0"
       android:toXScale="1.0"
       android:fromYScale="0.0"
       android:toYScale="1.0"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="2000" />
    </set>

答案 2 :(得分:0)

这是一种简单的方法

ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(textView,
                    PropertyValuesHolder.ofFloat("scaleX", 2.0f),
                    PropertyValuesHolder.ofFloat("scaleY", 2.0f));
            scaleDown.setDuration(410);

            scaleDown.setRepeatCount(ObjectAnimator.INFINITE);
            scaleDown.setRepeatMode(ObjectAnimator.REVERSE);

            scaleDown.start();

希望它有所帮助。

相关问题