Category: Learn Flex,
flex tutorials
In Flex you can write programmatic skins as ActionScript classes that use the Drawing API of the Flash Player. The methods for drawing at run-time are in the flash.display.Graphics package.
The best way to learn how to create programmatic skins is to look at the ones that come with Flex and then either extend them or create your own. The programmatic skin recipe is a basic template that you can use to implement a skin for a component.
The following code example is a programmatic skin class. You can use this class as a template to create new skins.
package
{
import flash.display.Graphics;
import mx.skins.Border;
import mx.skins.ProgrammaticSkin;
import mx.styles.StyleManager;
public class MyCustomSkin extends ProgrammaticSkin
{
// Constructor.
public function MyCustomSkin()
{
// Set default values here.
}
override protected function updateDisplayList(w:Number, h:Number):void
{
// Add styleable properties here.
// Add logic to detect component’s state and set properties here.
// Add drawing methods here.
}
}
}
Programmatic skins define the appearance of a skin in multiple states of a control, such as the upSkin, downSkin, and overSkin of a Button control. These states refer to the default state, when the button is pressed, and when the mouse is over the button but has not been pressed.
Choosing a Superclass
The first step in creating a programmatic skin is to select a superclass for the skin. You can extend the abstract base classes in the mx.skins package or the mx.skins.halo package, but the former gives you more control over the look and feel of your skins. The latter is fine if you use the default behavior of the Flex controls but want to add extra styling.
Most skins extend the mx.skins.ProgrammaticSkin class, but any of the following classes can be used:
The ProgrammaticSkin class implements all the necessary interfaces to make it easier for you to define skins for your components. It is also the most common superclass to use.
The Border class extends the ProgrammaticSkin class and adds a property called borderMetrics that allows you to get the thickness of the border.
The RectangularBorder class extends the Border class and adds a style property called backgroundImage so that you can set the background image of the component.
Note that a programmatic skin must implement the IFlexDisplayObject, IInvalidating, and ILayoutManagerClient interfaces. It must also implement the ISimpleStyleClient interface if it depends on the values of CSS styles.
The superclass of a skin can be a subclass of the DisplayObject class as long as you implement all of those interfaces.
The updateDisplayList() method is used to define the look of your skin. It is called after the skin’s constructor is called and then every time the skin is resized, restyled, moved, or interacted with in some way.
In the updateDisplayList() method, you use the drawing methods to draw the programmatic skin. When implementing the updateDisplayList() method, remember to use the override keyword to override the superclass’s implementation, set the return type to void, and declare the method as protected. The method also takes the width and height of the component as arguments.
The following code example draws a square with a border:
override protected function updateDisplayList(unscaledWidth:Number,
unscaledHeight:Number):void
graphics.clear();
graphics.lineStyle(2, 0×000000, 1.0, true, LineScaleMode.NORMAL,
CapsStyle.SQUARE, JointStyle.MITER);
graphics.beginFill(0xFFFFFF, 0);
graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
graphics.endFill();
}
The following example applies a programmatic skin to a specific state of the control:
Button
{
upSkin:ClassReference(’MyCustomSkin’);
}
Note that if you subclass the RectangularBorder class, you must also call the super.updateDisplayList() inside of your implementation of the updateDisplayList().