views:

1029

answers:

2

Hello, I need help coming up with a way of executing the following sequence using some type of view or layout combination in Android:

I have 3 image objects... say object A, B, and C...

[all objects invisible and layered, one on top of the other... A/B/C, like in a RelativeLayout]-->[fade in object A]-->[display A for 200ms]-->[simultaneously fade out object A and fade in object B]-->[display object B for 200ms]-->[simultaneously fade out object B and fade in object C]-->[object C remains on the screen indefinitely]

I have tried every combination of Threads, AsyncTasks, Handlers, custom layouts, AnimationListeners, etc. but everything I've tried has failed.

If only the ViewSwitcher could take more than 2 views... Please help.

Ryan

+1  A: 

The main thing about threads is that they are supposed to allow you to do parallel tasks, but the problem is that they don't guarantee "how parallel" those tasks will actually look when they're executed. Since the scheduler decides which thread to execute at any given time you are not guaranteed that your images will be properly faded in/out. While you can do this task with multithreaded code, I don't think it's a good candidate.

The best thing to do is to update both images (fade in/fade out) on every frame of the animation. A signals when it begins to fade out and B picks up that signal, then it starts to fade in. You will get a smooth transition since both A and B get updated on each frame, and you will not have any of the uncertainty of the threads. Do the same thing for B and C.

UPDATE: You caught me! :)
I thought you'd let me get away with just giving you the general information, but now that you have me cornered I had no choice but to google stuff :). OK, so the android library has some animation classes, the library also offers you a way in which you can actually perform frame-by-frame animation.

I'd give you a simple hack for this, but I'm sure there are better ways to do it: The animations take several images which should be displayed over a certain duration, so all you have to do is alternate the images.

<!-- Animation frames are AfadeOut01.png to AfadeOut03.png and BfadeIn01.png to BfadeIn03.png files inside the res/drawable/ folder,  -->
 <animation-list android:id="selected" android:oneshot="true">
    <item android:drawable="@drawable/AfadeOut01" android:duration="50" />
    <item android:drawable="@drawable/BfadeIn01" android:duration="50" />
    <item android:drawable="@drawable/AfadeOut02" android:duration="50" />
    <item android:drawable="@drawable/BfadeIn02" android:duration="50" />
    <item android:drawable="@drawable/AfadeOut03" android:duration="50" />
    <item android:drawable="@drawable/BfadeIn03" android:duration="50" />
 </animation-list>

You have to load the xml animation and display the animation, do something like this:

 // Load the ImageView that will host the animation and
 // set its background to our AnimationDrawable XML resource.
 ImageView img = (ImageView)findViewById(/*resourceImageID e.g. AfadeOut03*/);
 img.setBackgroundResource(/*backgroundResource*/);

 // note that this loads the resource from an XML file, but
 // instead of getting the resource from file you can generate
 // it from a single image by performing the required modifications
 // of the image and storing them in a resource.

 // Get the background, which has been compiled to an AnimationDrawable object.
 AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

 // Start the animation
 frameAnimation.setOneShot(true);// don't loop if not set in XML
 frameAnimation.start();

OK, so I know that was a dirty hack, but it should do what you want :). If this is too simple and uncool for you, then you could go the original route and try to figure out how to display your images frame by frame, etc.

Lirik
Okay, that does make sense. Thank you. So, your suggestion would be to create a class that extends from a View/ImageView/SurfaceView and perform the alpha animation on the three bitmaps directly? How would I control the speed or duration of the overall animations? If I set it up to repeatedly call onDraw or Invalidate, how would I control the frequency? Again, thank you for the response.
borg17of20
@borg17of20 Ah... well, now you caught me red-handed :)... I'm not actually familiar with the android libraries for animation, but luckily google is my friend and I was able to find some links: http://code.google.com/android/reference/android/view/animation/package-descr.html and http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html I'll update my answer for more info.
Lirik
Your link to the AnimationDrawable helped me find this: http://code.davidjanes.com/blog/2009/11/23/animated-slideshow-on-android/ which looks promising.
borg17of20
@borg17of20 RE: David Jones blog- Should I remove my self-incriminating update? LOL David Jones' blog seems to demonstrate a slideshow animation and I don't think this will give you the cross-fade between two images... instead I think it fades each image out and fades the following image in.
Lirik
@Lirik Again, thanks for the reply. Okay, so from what I gather, the animation described in your update requires 3 images of object A and three of object B in some stage of alpha flux, or fading (e.g. alpha %100, %50, and 0% for each). What I have now is three images of three different objects @100% alpha. To make the above proposed solution work, I'd have to create more photos from my source images. Am I correct in this assumption? Regardless, I think you have answered my initial question. I don't see why it wouldn't work, I just need to make the alpha adjustments by hand. Thanks.
borg17of20
@borg17of20 well, the example I gave you uses 3 images for each fade, but you can use more than 3 depending on how smooth you want the fade to be. I am pretty sure that you can generate the transitions directly from your java code and you should be able to instantiate an ImageView with the images you generate, but it really depends on how economical you want to be with your storage. If you can use more storage, then create more photos... if you are limited on storage then create the transitions by adjusting the alpha of each pixel of the image in java.
Lirik
A: 

These things don't really require threads, they require an Animation. More specifically an AlphaAnimation. Check out the Alpha Animation page for details on how to use it. It's pretty simple, you just set what you want to happen and how long you want it to take.

CaseyB
Unfortunately, the timing of the images is very important and I have not been able to get it right either with threading or using the built-in means of implementing animations. Thank you for responding.
borg17of20