1

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?

Goo
  • 1,318
  • 1
  • 13
  • 31
manmal
  • 3,900
  • 2
  • 30
  • 42

1 Answers1

1

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

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
Hala.M
  • 766
  • 6
  • 15
  • I don't get it, can you please reformulate the answer? – manmal Feb 13 '13 at 11:23
  • here is my code in on create ` iSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher); iSwitcher.setFactory(this); iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); make view for the factory @Override public View makeView() { ImageView iView = new ImageView(this); iView.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); return iView; }` – Hala.M Feb 16 '13 at 13:32