The Flex Drop Shadow

Skill

The Flex Drop Shadow

Posted in:

Shadows are all the rage right now in user interfaces - Vista and Leopard both have shadows underneath opened dialogs. They provide a really quick way to give your flat application the feeling of depth. Apparently the makers of Flex knew this, since they made adding shadows to pretty much anything very easy. By the end of this tutorial you'll know how to add drop shadows to Flex components and how to configure their appearance.

The first thing you need to know about shadows is that they have a lot of options. To help you decide what options you want for your drop shadows, I threw together a little Flex app that lets you adjust the appearance of shadows in real-time. Just drag around the sliders in the application below and the result will appear immediately on the right.

Get Adobe Flash player

If you're done playing around, let's look at how to create some shadows. I'm going to first show you how to add drop shadows using MXML then we'll look at how to add them using ActionScript.

Adding Drop Shadows using MXML

For the first example, here's the simplest way to add a drop shadow. In this example, the shadow will be using all of the default options.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
   xmlns:display="flash.display.*" width="125" height="125">
  <mx:Box verticalAlign="middle" horizontalAlign="center" width="100%" height="100%">
    <mx:Box width="75" height="75" backgroundColor="#FFFFFF" backgroundAlpha="1">
      <mx:filters>
        <mx:DropShadowFilter />
      </mx:filters>
    </mx:Box>
  </mx:Box>
</mx:Application>

As you can see, the application is just a white box with a drop shadow applied. Filters are applied simply by adding them to Flex component's filters arrays. If you wanted to add more filters other than a drop shadow, they would be added in the same manner. Here's what the above application looks like:

Get Adobe Flash player

Normally, the default shadow settings will work for most applications, but sometimes you need to change them. Here's the same example with the distance and angle changed.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
   xmlns:display="flash.display.*" width="125" height="125">
  <mx:Box verticalAlign="middle" horizontalAlign="center" width="100%" height="100%">
    <mx:Box width="75" height="75" backgroundColor="#FFFFFF" backgroundAlpha="1">
      <mx:filters>
        <mx:DropShadowFilter distance="10" angle="220"/>
      </mx:filters>
    </mx:Box>
  </mx:Box>
</mx:Application>

And here's what that looks like:

Get Adobe Flash player

Adding Drop Shadows using ActionScript

If MXML isn't your tour du jour, drop shadows can also be added using ActionScript. Below is the code required to produce the exact same application as the first MXML example.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
   xmlns:display="flash.display.*" width="125" height="125"
   applicationComplete="addShadow()">
  <mx:Script>
   <![CDATA[
     private function addShadow():void
     {  
       var filters:Array = new Array();
       var dFilter:DropShadowFilter = new DropShadowFilter();
       filters.push(dFilter);
       this.myBox.filters = filters;
     }
   ]]>
 </mx:Script>
  <mx:Box verticalAlign="middle" horizontalAlign="center" width="100%" height="100%">
    <mx:Box width="75" height="75" backgroundColor="#FFFFFF"
       backgroundAlpha="1" id="myBox">
    </mx:Box>
  </mx:Box>
</mx:Application>

The syntax for adding filters is a little funky, but it's necessary. What's even worse is if you want to change the properties of a shadow on the fly, you have to do all of that code every time - remaking and reassigning the filter array. If anyone knows a way around that, please let me know. So now let's look at the ActionScript code required to make the second example.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
   xmlns:display="flash.display.*" width="125" height="125"
   applicationComplete="addShadow()">
  <mx:Script>
   <![CDATA[
     private function addShadow():void
     {  
       var filters:Array = new Array();
       var dFilter:DropShadowFilter = new DropShadowFilter();
       dFilter.angle = 220;
       dFilter.distance = 10;
       filters.push(dFilter);
       this.myBox.filters = filters;
     }
   ]]>
 </mx:Script>
  <mx:Box verticalAlign="middle" horizontalAlign="center" width="100%" height="100%">
    <mx:Box width="75" height="75" backgroundColor="#FFFFFF"
       backgroundAlpha="1" id="myBox">
    </mx:Box>
  </mx:Box>
</mx:Application>

That's it for Flex drop shadows. As you can see, the good people over at Adobe made it very easy for us developers to create and use them. If you want more specific information on drop shadows, check out the Adobe live docs on the DropShadowFilter. You can also download the source code for the applications here.

Muzak
12/03/2007 - 14:41

It's best not to assign filters to a container that will contain text (Button, Label, etc..) as the container will have it's cacheAsBitmap property set to true, which will also change the appearance of text.
One way to solve that is to apply the filter to a nested canvas container.

(Not sure if code works here, so here's some pseudo code)

original (with messed up text)

- Box filters="{[yourShadow]}"
- Label
- Button
nested container (text displays properly)

- Canvas
- Canvas filters="{[yourShadow]}"
- Box
- Label
- Button

Because the canvas stacks its children on top of each other, the Box (containing the Label and Button) will overlay the nested Canvas (to which the filter is applied) and the shadow filter will not affect the text displayed (no bitmap caching on Box container).

Muzak

reply

Muzak
12/03/2007 - 14:43

argh.. spaces are removed in previous comment..

original (with messed up text)

- Box filters=”{[yourShadow]}”
- - Label
- - Button

nested container (text displays properly)

- Canvas
- - Canvas filters=”{[yourShadow]}”
- - Box
- - - Label
- - - Button

reply

The Reddest
12/03/2007 - 15:04

Thanks for the advice Muzak. I'll keep that in mind from now on.

reply

Mitch
12/21/2007 - 09:35

Just a quick note to say thanks! I've been struggling with trying to do "setStyle" in actionscript to set the shadow and that wasn't working and was thankful to find your sample... got me on the right path!

reply

lamb Cam
07/11/2008 - 09:20

This is great - is there any way I can apply a drop shadow effect to my application as a whole so that the air application has a drop shadow when it loads?

reply

Scott
01/07/2009 - 11:26

To the point, instructive, code to scarf, thank you very much.

reply

ciacob
01/21/2010 - 16:21

Thanks man :)

reply

ciacob
01/21/2010 - 17:02

This way is much shorter:

<mx:Box height="75" width="75"
filters="{[new DropShadowFilter(2.97,45,0x999900)]}" />

Although is harder to read too...

Thank you, take care,
C

reply

shashank Kulkarni
06/25/2010 - 04:23

Nice work helping me continuously. I have bookmarked it. :) Thanks Friend

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.

Sponsors