Creating an animated base color effect on a Spark Button control in Flex 4

The following example shows how you can create an animating background effect on a Spark Button control in Flex 4 by using the AnimateColor effect on the baseColor style.

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/08/16/creating-an-animated-base-color-effect-on-a-spark-button-control-in-flex-4/ -->
<s:Application name="Spark_Button_AnimateColor_baseColor_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">
 
    <fx:Declarations>
        <s:AnimateColor id="animatr"
                target="{btn}"
                colorFrom="haloSilver"
                colorTo="haloBlue"
                colorPropertyName="baseColor"
                duration="400"
                repeatCount="0" 
                repeatBehavior="reverse" />
    </fx:Declarations>
 
    <s:Button id="btn"
            label="Spark Button"
            horizontalCenter="0" verticalCenter="0"
            creationComplete="animatr.play();"/>
 
</s:Application>

View source is enabled in the following example.

[GoogleAdsWide]

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/08/16/creating-an-animated-base-color-effect-on-a-spark-button-control-in-flex-4/ -->
<s:Application name="Spark_Button_AnimateColor_baseColor_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"
        initialize="init();">
 
    <fx:Script>
        <![CDATA[
            import spark.effects.animation.RepeatBehavior;
            import spark.effects.AnimateColor;
            import spark.components.Button;
 
            private var btn:Button;
            private var animatr:AnimateColor;
 
            private function init():void {
                btn = new Button();
                btn.label = "Spark Button";
                btn.horizontalCenter = 0;
                btn.verticalCenter = 0;
                addElement(btn);
 
                animatr = new AnimateColor();
                animatr.target = btn;
                animatr.colorFrom = StyleManager.getColorName("haloSilver");
                animatr.colorTo = StyleManager.getColorName("haloBlue");
                animatr.colorPropertyName = "baseColor";
                animatr.duration = 400;
                animatr.repeatCount = 0;
                animatr.repeatBehavior = RepeatBehavior.REVERSE;
                animatr.play();
            }
        ]]>
    </fx:Script>
 
</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.

One thought on “Creating an animated base color effect on a Spark Button control in Flex 4

  1. Hey Peter,

    You know any ways to improve animation performance in flex? It seems that because Flash/pure Actionscript has neither [Bindable] nor invalidation methods, running animations is much much smoother than the same animations in Flex. You should be able to do Template Monster-type animations in Flex too, do you know how improve performance in this regard?

    Here’s a more detailed explanation of the problem:
    http://forums.adobe.com/thread/481266

    Thanks so much man,
    Lance

Comments are closed.