重复Android动画

59
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vitesse);

    gpsManager = new GPSManager();

    gpsManager.startListening(getApplicationContext());
    gpsManager.setGPSCallback(this);
    Typeface tf = Typeface.createFromAsset(getAssets(),
            "font/DS-DIGI.TTF");
     TextView  loding =(TextView)findViewById(R.id.info_message);
            loding.setTypeface(tf);
            AlphaAnimation fadeIn = new AlphaAnimation(0.0f , 1.0f ) ; 
            AlphaAnimation fadeOut = new AlphaAnimation( 1.0f , 0.0f ) ; 
            loding.startAnimation(fadeIn);
            loding.startAnimation(fadeOut);
            fadeIn.setDuration(500);
            fadeOut.setDuration(1200);
            fadeOut.setStartOffset(1200+fadeIn.getStartOffset()+1200);     
            measurement_index = AppSettings.getMeasureUnit(this);
}

我希望重复这个TextView加载动画,直到从GPS获取信息为止。

6个回答

138

这样

animation.setRepeatCount(Animation.INFINITE);

1
你也可以在XML中定义它,例如:android:repeatCount = "infinite" - NobodySomewhere

36

Android提供了优雅的机制来表示加载过程。您可以使用不确定的 ProgressBar,或者使用具有比动画 TextView 更好的视觉效果的ImageView与缩放/透明度动画替代。

也许您会发现这种动画很有用,它可以同时动画化alpha和scale。根据您的喜好更改参数:

文件 res / anim / alpha_scale_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
    android:fromXScale="0.7"
    android:toXScale="1.0"
    android:fromYScale="0.7"
    android:toYScale="1.0"
    android:pivotX="50%p"
    android:pivotY="50%p"
    android:duration="4000"
    android:repeatCount="infinite"
    />

<alpha
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="2000"
    android:repeatMode="reverse"
    android:repeatCount="infinite"
    />
</set>

然后在您的代码中启动它:

Animation connectingAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.alpha_scale_animation);
myView.startAnimation(connectingAnimation);

并且停止它:

myView.clearAnimation();
connectingAnimation.cancel();
connectingAnimation.reset();

你还可以使用像ProgressButtonView这样的库,在同一个小部件中支持用户交互和加载过程。

希望这些解决方案对某些人有用:)


1
cancel() 的文档(我使用 _ValueAnimator_,一个子类)指出它需要在调用者所在的同一线程上执行。这有点不方便,但它似乎仍然可以工作!(对于其他使用 ValueAnimator 类的人来说,没有 reset();好像也不需要它。) - SMBiggs
1
你是对的。reset() 不是必要的,cancel() 应该足够了。 - voghDev

11
你可以使用这些方法来控制重复行为:
fadeIn.setRepeatCount(int count) // or Animation.INFINITE

fadeIn.setRepeatMode(Animation.REPEAT) // or any other repeat mode, such as Animation.REVERSE

如果需要,实现这些监听器:

// anim = your animation    
anim.setAnimationListener(new AnimationListener() {
            public void onAnimationStart(Animation arg0) 
            {
                // TODO Auto-generated method stub
            }
            
            public void onAnimationRepeat(Animation arg0)
            {
                // TODO Auto-generated method stub
            }
            
            public void onAnimationEnd(Animation arg0)
            {
                // TODO Auto-generated method stub
            }
        });

如果您想突然停止动画,请使用yourView.clearAnimation()。希望这能帮到您。

setRepeatMode 只接受 RESTART 或 REVERSE。INFINITE 应该只用于设置 setRepeatCount。 - Rupert Rawnsley

8

要重复动画,只需在位于 anim 文件夹中创建动画对象的 xml 文件中添加以下代码:

  1. android:repeatCount="infinite"
  2. android:repeatMode="restart"

0

将重复次数设置为-1,它将无限动画


1
这是一个有效的答案,但包含代码示例可能会更有用。它也没有遵循最佳编码实践,而是使用静态值,而不是引用官方变量(例如Animation.REPEAT)。但这并不意味着它是一个糟糕的答案 - 欢迎加入社区! :) - ConcernedHobbit

0

除了其他答案外,这里有一个要点

如果您使用来自android.R.anim.的保留动画,则可以使用此方法

animation.setRepeatCount(Animation.INFINITE);

但是,如果您使用了动画(getApplicationContext(),R.anim.move_y),那么您必须在XML中使用这种方式。

 android:repeatCount="infinite"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接