Reputation: 3928
I am using Android's lesser known ImageSwitcher to animate image changes in an ImageView. Currently, there are 2 images (default and "ok" image) which are switched on certain events (all happening on the main thread) - the first image is faded out, while the second is faded in. That's how ImageSwitcher is supposed to work.
The first 2 animations (going from default to "ok" and then back to default) fade over just fine. Then, suddenly, images are not faded from one to another any more, but the view switches immediately to the second image and the animation messes around with two overlayed versions of the second image (those images are semi-transparent black, and I can see how the second image goes from almost full black back to normal).
Switching is implemented like this:
// Initialization in onCreate():
mRefreshImageSwitcher = (ImageSwitcher) findViewById(R.id.main_refresh);
mRefreshImageSwitcher.setFactory(new RefreshButtonViewFactory());
mRefreshImageSwitcher.setImageResource(R.drawable.refresh);
// ...
mRefreshImageSwitcher.setInAnimation(getFadeAnimation(true, 300));
mRefreshImageSwitcher.setOutAnimation(getFadeAnimation(false, 300));
mRefreshImageSwitcher.setImageResource(R.drawable.refresh_ok);
// ...
private Animation getFadeAnimation(boolean in, long durationMillis) {
Animation a = AnimationUtils.loadAnimation(this, in ? android.R.anim.fade_in : android.R.anim.fade_out);
a.setDuration(durationMillis);
return a;
}
Alternatively, I have tried to always use the same Animation objects, but that did not change anything.
Is it possible that setting the same image more than once somehow messes up the ImageSwitcher? Any other ideas?
Upvotes: 1
Views: 1079
Reputation: 766
You are not writing how you change the image inside events but one thing is to make sure you are using mRefreshImageSwitcher.setImageResource()
not mRefreshImageSwitcher.setBackgroundResource()
cause setting background wont play the animation
Upvotes: 1