Fading two images using the CrossFade effect in Flex 4

by Peter deHaan on September 21, 2009

in Image,beta1

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 http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://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.

{ 4 comments… read them below or add one }

1 bobby September 23, 2009 at 10:59 am

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

Reply

2 bobby September 23, 2009 at 11:06 am
<mx:Image id="logo" initialize="logo.source='{FlashPlayerIcon}'" />

Reply

3 Dave April 18, 2010 at 12:12 am

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();

Reply

4 Dave April 18, 2010 at 12:14 am

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

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: