Slide Show with Recursion in Android

I was developing an app and the tutorial consisted of wire frame illustrations that needed to be displayed as a slideshow when the user wants to view the tutorial. Several pictures, one slideshow. Simple enough right? Apparently not …

I came across this link http://code.davidjanes.com/blog/2009/11/23/animated-slideshow-on-android/ by fellow blogger david. It is a neat solution to the problem and definitely more fault tolerant. But writing that much code for swiping through only a few pictures quite quickly? (in my case I had 10 slides).

So I decided to use a countdown timer and some recursion. While my solution (arguably) is clumsy, I think it’s cool. So here you go.

THE .XML: Same as david’s. Using a frame layout and stacking your ImageView‘s on top of one another.
mactivity.xmland so on. The special part is in a separate class called ‘ImageSlider’ which contains a countDownTimer which progressively makes the other pictures visible.

THE .JAVA:

code2Notice that I put all the ImageView’s into an array and then switch through each one during the recursion. Slide # 1 is excluded because this is the one you start on. But after that, you go from 1 to 2, 2 to 3, 3 to 4 … and so on. The recursion is on line #54. Notice the if prevents an out of bounds error.

While it may seem clumsy, I think this is a simple solution. You can literally take this function and plop it into your main activity or any activity that needs a series of pictures to fade in/out. Notice that this class is not an activity.

The countDownTimer is a very powerful class. Hope you enjoyed this little demonstration.

Please do ask me if you require further clarification.

 

Advertisements

Tell me what you think!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s