Creating a custom track skin on an MX ProgressBar control in Flex 4

The following example shows how you can create a custom track skin on an MX ProgressBar control in Flex 4 by setting the trackSkin style.

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/2010/04/13/creating-a-custom-track-skin-on-an-mx-progressbar-control-in-flex-4/ -->
<s:Application name="MX_ProgressBar_trackSkin_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:layout>
        <s:VerticalLayout horizontalAlign="center" verticalAlign="middle" />
    </s:layout>
    <s:controlBarContent>
        <s:HSlider id="sldr"
                minimum="0" maximum="100"
                value="0"
                change="pBar.setProgress(sldr.value, sldr.maximum);" />
    </s:controlBarContent>
 
    <mx:ProgressBar id="pBar"
            trackSkin="skins.CustomProgressBarTrackSkin"
            labelPlacement="center"
            mode="manual"
            color="red" />
 
</s:Application>

[HaloSparkSkins]

And the custom MX ProgressBar track skin, skins/CustomProgressBarTrackSkin.mxml, is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/04/13/creating-a-custom-track-skin-on-an-mx-progressbar-control-in-flex-4/ -->
<s:SparkSkin name="CustomProgressBarTrackSkin"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        minHeight="9">
 
    <fx:Script>
        <![CDATA[
            override protected function initializationComplete():void {
                useChromeColor = true;
                super.initializationComplete();
            }
        ]]>
    </fx:Script>
 
    <!-- layer 1: border -->
    <s:Rect left="0" right="0" top="0" bottom="0" width="199">
        <s:stroke>
            <s:LinearGradientStroke rotation="90">
                <s:GradientEntry color="0xFFFFFF" alpha="0.12" />
                <s:GradientEntry color="0xFFFFFF" alpha="0.8" />
            </s:LinearGradientStroke>
        </s:stroke>
    </s:Rect>
 
    <!-- layer 2: inner border -->
    <s:Rect left="1" right="1" top="1" bottom="1" >
        <s:stroke>
            <s:SolidColorStroke color="0x636363" alpha="1" />
        </s:stroke>
    </s:Rect>
 
    <!-- layer 3: fill -->
    <s:Rect left="2" right="2" top="2" bottom="2" >
        <s:fill>
            <s:BitmapFill source="@Embed('assets/pattern_143.gif')" fillMode="repeat" />
        </s:fill>
    </s:Rect>
 
    <!-- layer 4: inner shadow -->
    <s:Rect left="2" top="2" bottom="2" width="1">
        <s:fill>
            <s:SolidColor color="0x000000" alpha="0.12" />
        </s:fill>
    </s:Rect>
    <s:Rect right="2" top="2" bottom="2" width="1" >
        <s:fill>
            <s:SolidColor color="0x000000" alpha="0.12" />
        </s:fill>
    </s:Rect>
    <s:Rect left="3" top="2" right="3" height="1" >
        <s:fill>
            <s:SolidColor color="0x000000" alpha="0.12" />
        </s:fill>
    </s:Rect>
 
</s:SparkSkin>

[GoogleAdsWide]

View source is enabled in the following example.

[Squidfingers]

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.