Skip to content

Instantly share code, notes, and snippets.

@newbyca
Created March 11, 2012 23:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newbyca/2018678 to your computer and use it in GitHub Desktop.
Save newbyca/2018678 to your computer and use it in GitHub Desktop.
workaround class for Android animation set repeatCount problem.
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
...
new Handler().postDelayed(new Runnable() {
@Override public void run() {
findViewById(R.id.someview).startAnimation(
AnimationUtils.loadAnimation(AlarmAlert.this, R.anim.someanimation)
);
}
}, 1000);
}
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:shareInterpolator="true"
>
<alpha
android:duration="250"
android:fromAlpha="0.0"
android:toAlpha="1.0"
/>
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="1.0"
/>
</set>
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
public class AnimationLooper{
public static void start(final View v, final int animationResId){
v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override public void onGlobalLayout() {
v.getViewTreeObserver().removeOnGlobalLayoutListener(this);
Animation a = AnimationUtils.loadAnimation(v.getContext(), animationResId);
a.setAnimationListener(new AnimationListener() {
@Override public void onAnimationStart(Animation animation) {}
@Override public void onAnimationRepeat(Animation animation) {}
@Override public void onAnimationEnd(Animation animation) {
animation.reset();
animation.startNow();
}
});
v.startAnimation(a);
}
});
}
public static void stop(final View v){
v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override public void onGlobalLayout() {
v.getViewTreeObserver().removeOnGlobalLayoutListener(this);
Animation a = v.getAnimation();
if(a != null){
a.setAnimationListener(null);
a.cancel();
}
v.setAnimation(null);
}
});
}
}
@newbyca
Copy link
Author

newbyca commented May 9, 2012

stronger would be to change this class to rely on a GlobalLayoutListener instead of the arbitrary time delay ... the benefit being reliability and elegance.

@newbyca
Copy link
Author

newbyca commented Oct 1, 2012

refactd AnimationLooper to use ViewTree and got rid of unnecessary animation instances

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment