Created
March 11, 2012 23:31
-
-
Save newbyca/2018678 to your computer and use it in GitHub Desktop.
workaround class for Android animation set repeatCount problem.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
} | |
} |
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
stronger would be to change this class to rely on a GlobalLayoutListener instead of the arbitrary time delay ... the benefit being reliability and elegance.