So I want my animation to start as soon as the activity is created, but for some reason no matter what I try will get it to start. I can get it to start by having a click ev
My solution for playing animations while they are visible. It let you declare (in xml layout) and forget.
class AnimatedImageView extends ImageView {
// required constructors omitted for clarity
private void updateAnimationsState() {
boolean running = getVisibility() == View.VISIBLE && hasWindowFocus();
updateAnimationState(getDrawable(), running);
updateAnimationState(getBackground(), running);
}
private void updateAnimationState(Drawable drawable, boolean running) {
if(drawable instanceof AnimationDrawable) {
AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
if(running) {
animationDrawable.start();
} else {
animationDrawable.stop();
}
}
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
updateAnimationsState();
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
updateAnimationsState();
}
}
If you setup drawable from code, then you should override appropriate set*() methods and call updateAnimationsState() from there.
One more solution:
Call animationDrawable.stop()
before calling start()
method.
I know this is an old question, but I was having the same problem, but the answers weren't helping me. After spending hours working on it I finally found out that my problem was that I had added android:src="@drawable/myanimation"
to the imageview container. Once I removed this the above answers worked. I think the animation was running but by setting the src the first image of the animation was on top of it, and thus I thought it wasn't playing.
android:src
defined and is set to invisibleonCreate
I set the imageview to visible and setBackgroundResource
to my animation described in the XML fileonWindowFocusChanged
I start the animation. I fixed this problem by call setBackgroundResource
instead of setImageResource
when i want to start the animation as mentioned in the above answers
private AnimationDrawable animationDrawable;
fab.setImageResource(R.drawable.animation);
animationDrawable = (AnimationDrawable) fab.getDrawable();
fab.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
fab.setBackgroundResource(R.drawable.animation);
animationDrawable.start();
}
});
note: to reset the animation use
animationDrawable.selectDrawable(0);//animation drawable used here is collection of images
animationDrawable.stop();
Try starting your animation after the window gets focus by overriding onWindowFocusChanged in your Activity:
@Override
public void onWindowFocusChanged (boolean hasFocus)
{
//Start animation here
}
See docs here: http://developer.android.com/reference/android/app/Activity.html#onWindowFocusChanged%28boolean%29
I think you have to start the animation after initialization of the view in question is complete. You should be able to do something like:
final ImageView tweenImage = (ImageView) findViewById(R.id.imageView1);
tweenImage.setBackgroundResource(R.anim.cubicfacetween);
tweenImage.post(new Runnable() {
@Override
public void run() {
AnimationDrawable frameAnimation =
(AnimationDrawable) tweenImage.getBackground();
frameAnimation.start();
}
}
Edit - this question led me to believe that the onWindowFocusChanged
method won't always work. It does seem simpler and is probably a better idea if it works for you.