Nick Nick - 5 months ago 21
Android Question

Android: Update ImageView Background while Behind other ImageView

I'm building an app where I want to swipe images as though they were photos on a stack. In other words, if I swipe the top image I want it to animate moving in the direction of the swipe and have the next image underneath it visible the whole time. To accomplish this, I'm using a

FrameLayout
and two
ImageView
containers. I'm just alternating which one is on top. Meanwhile, as soon as a swipe occurs, the next image is loaded into the
ImageView
at the back using
setBackground(drawable)
. My problem is that the
ImageView
at the back doesn't update it's image until I call
bringToFront()
on it, which means that as the top
ImageView
is animating, the image underneath is incorrect until the animation completes, at which point it abruptly changes to the correct image. I've tried calling
invalidate()
on the rear
ImageView
after
setBackground(drawable)
but this doesn't work. Anyone have any ideas on how I can get the image to update while it's behind?

UPDATE: Turns out I'm just not very on it today. I was updating the wrong
ImageView
and because the image loading was being done off the network, there was just enough lag to make me think it was happening after the animation completed.

Answer

Sounds to me like you wanna do something like an Image Slider.

There are great libraries existing for this purpose, this one for example:

https://github.com/daimajia/AndroidImageSlider


If you don't wanna use this, here are some tips:

  • Images on ImageViews are set with setImageDrawable(Drawable)

  • When your animation starts, set the new Image to your ImageView behind and slide the visible one away.

  • When the visible ImageViewhas slided away, set it's visibility to GONEand move it behind the second ImageView

Do this for every time a new image is loaded.

This should actually work.

Comments