Fading two images using the CrossFade effect in Flex 4

The following example shows how you can cross fade two bitmap images using the CrossFade effect in Flex 4.

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/09/21/fading-two-images-using-the-crossfade-effect-in-flex-4/ -->
<s:Application name="Spark_CrossFade_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
    <s:controlBarContent>
        <mx:Button id="btn"
                label="Play CrossFade effect"
                click="button1_click(event);" />
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            [Embed("assets/fl_player_appicon.jpg")]
            private const FlashPlayerIcon:Class;
 
            [Embed("assets/fx_appicon.jpg")]
            private const FlexPlayerIcon:Class;
 
            protected function button1_click(evt:MouseEvent):void {
                xFade.stop();
                xFade.bitmapFrom = new FlexPlayerIcon().bitmapData;
                xFade.bitmapTo = new FlashPlayerIcon().bitmapData;
                xFade.play();
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <s:CrossFade id="xFade" target="{logo}" duration="3000" />
    </fx:Declarations>
 
    <s:VGroup horizontalCenter="0" verticalCenter="0">
        <mx:Image id="logo" source="{FlexPlayerIcon}" />
    </s:VGroup>
 
</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

7 thoughts on “Fading two images using the CrossFade effect in Flex 4

  1. I replaces the last lines with the following lines so that I do not want to jump to the initial image after end of the effect, but this is not working can any body help whats the problem is

  2. I am seeing the jump to the initial image that bobby is talking about. Relevant code:

    if (i == timer.repeatCount) {
    clazz = resourceManager.getClass(‘assets’, ‘dash_article_7′);
    bitmapFrom = new clazz().bitmapData;
    clazz = resourceManager.getClass(‘assets’, ‘dash_article_1′);
    bitmapTo = new clazz().bitmapData;
    dash_numbers.selectedIndex = 0;

    } else {
    clazz = resourceManager.getClass(‘assets’, ‘dash_article_’+i);
    bitmapFrom = new clazz().bitmapData;
    clazz = resourceManager.getClass(‘assets’, ‘dash_article_’+(i+1))
    bitmapTo = new clazz().bitmapData;

    }

    xFade.stop();
    xFade.bitmapFrom = bitmapFrom;
    xFade.bitmapTo = bitmapTo;
    xFade.play();

  3. There should be a <Declarations> block containing my CrossFade. The target of the CrossFade is an mx:Image that didnt show up either.

  4. just stick your BitmapImage with second term of transition:
    xFade.stop();
    xFade.bitmapFrom = new FlexPlayerIcon().bitmapData;
    xFade.bitmapTo = new FlashPlayerIcon().bitmapData;
    xFade.play();
    logo.source = new FlashPlayerIcon().bitmapData;

  5. How do you crossfade scaled images? I want to crossfade a scaled background-image, but the fading occurs with non-scaled images, and once the fade is done, snaps to scale.

    Any ideas?

Comments are closed.