Skinning a Flex button with one MXML file (and Degrafa)
Skinning a flex button can often take multiple graphics and/or MXML files for each state. Here’s how to skin a button using MXML and Degrafa to handle three button states with just one file (plus a bonus ‘glow’ filter as an alternate way to get a drop shadow).
The secret is to override the updateDisplayList method and use the skin’s current name in a switch statement to apply different Degrafa strokes and fills to the button. Click to view an example with source.

<?xml version="1.0" encoding="utf-8"?>
<GraphicBorderSkin xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns="http://www.degrafa.com/2007">
<mx:Script>
<![CDATA[
[Bindable]
private var awidth:Number=0;
[Bindable]
private var aheight:Number=0;
override protected function updateDisplayList( unscaledWidth:Number, unscaledHeight:Number ):void
{
super.updateDisplayList( unscaledWidth, unscaledHeight );
awidth = unscaledWidth;
aheight = unscaledHeight;
var glow:GlowFilter = new GlowFilter();
glow.color = 0x000000;
glow.alpha = 1;
glow.strength = .75;
glow.quality = BitmapFilterQuality.MEDIUM;
switch ( name )
{
case "upSkin":
buttonRect.fill = upFill;
buttonRect.stroke = upStroke;
glow.blurX = 4;
glow.blurY = 4;
break;
case "overSkin":
buttonRect.fill = overFill;
buttonRect.stroke = overStroke;
glow.blurX = 5;
glow.blurY = 5;
break;
case "downSkin":
buttonRect.fill = downFill;
buttonRect.stroke = downStroke;
glow.blurX = 3;
glow.blurY = 3;
break;
}
this.filters = [glow];
}
]]>
</mx:Script>
<fills>
<LinearGradientFill id="upFill" angle="90" >
<GradientStop ratio="0" alpha="1" color="#666666" />
<GradientStop ratio=".5" alpha="1" color="#333333" />
<GradientStop ratio=".5" alpha="1" color="#000000" />
<GradientStop ratio="1" alpha="1" color="#666666" />
</LinearGradientFill>
<LinearGradientFill id="overFill" angle="90" >
<GradientStop ratio="0" alpha="1" color="#666666" />
<GradientStop ratio=".5" alpha="1" color="#333333" />
<GradientStop ratio=".5" alpha="1" color="#000000" />
<GradientStop ratio="1" alpha="1" color="#666666" />
</LinearGradientFill>
<LinearGradientFill id="downFill" angle="90" >
<GradientStop ratio="0" alpha="1" color="#000000" />
<GradientStop ratio=".5" alpha="1" color="#333333" />
<GradientStop ratio=".5" alpha="1" color="#444444" />
<GradientStop ratio="1" alpha="1" color="#777777" />
</LinearGradientFill>
</fills>
<strokes>
<SolidStroke id="upStroke" color="#ffffff" alpha="1" weight="1.5" />
<LinearGradientStroke id="overStroke" angle="90" weight="1.5">
<GradientStop alpha="1" ratio="0" color="#ccffff"/>
<GradientStop alpha="1" ratio="1" color="#66ccff"/>
</LinearGradientStroke>
<SolidStroke id="downStroke" color="#f4f4f4" alpha="1" weight="1.5" />
</strokes>
<geometry>
<RoundedRectangleComplex id="buttonRect" height="{aheight}" width="{awidth}"
bottomLeftRadius="4" bottomRightRadius="4" topLeftRadius="4" topRightRadius="4" />
</geometry>
</GraphicBorderSkin>
This entry was written by
Alastair, posted on
August 5, 2008 at 9:13 pm, filed under
ActionScript,
Flex. Bookmark the
permalink. Follow any comments here with the
RSS feed for this post.
or leave a trackback:
Trackback URL.
© Copyright 2006 - 2012 Alastair Dawson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
One Comment
Hey, Thanks for sharing the code.