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

I want to repeat this of textview loding animation until gotting an inforamtion from GPS

EMIN
  • 687
  • 1
  • 6
  • 9

4 Answers4

129

Like this.

animation.setRepeatCount(Animation.INFINITE);
alex
  • 6,099
  • 1
  • 21
  • 21
30

Android gives you elegant mechanisms to represent the loading process. You could use an indeterminate ProgressBar, or an ImageView with a scale/alpha animation, instead of animating the TextView itself.

Maybe you might find this animation useful, for animating alpha and scale at the same time. Vary the parameters for your preference:

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

then to launch it in your code:

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

And to stop it:

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

You can also use libraries like ProgressButtonView to have both user interaction and loading process supported in the same widget.

Hope that any of these solutions result useful to someone :)

voghDev
  • 4,864
  • 1
  • 31
  • 39
  • 1
    The documentation for `cancel()` (I'm using _ValueAnimator_, a sub-class) states that it needs to be on the same thread as the caller. This is rather inconvenient, but it seems to work anyway! (For others using the ValueAnimator class, there is no `reset()`; doesn't seem to need it.) – SMBiggs Mar 17 '19 at 03:06
  • 1
    You're right. `reset()` is not necessary and `cancel()` should be enough – voghDev Jan 05 '21 at 10:46
9

You can use these methods to control the repeat behavior:

fadeIn.setRepeatCount(int count) // or Animation.INFINITE

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

implement these listeners, if needed:

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

if you wanna stop your animation suddenly, use yourView.clearAnimation() I hope this helps.

Aaa
  • 810
  • 3
  • 9
  • 21
anzaidemirzoi
  • 359
  • 4
  • 12
  • [setRepeatMode](http://developer.android.com/reference/android/view/animation/Animation.html#setRepeatMode(int)) expects only RESTART or REVERSE. INFINITE should only be given to setRepeatCount. – Rupert Rawnsley Jan 15 '16 at 12:10
4

to repeat animation just add the code in xml file which located in anim folder file in which we create a animation object.

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