`
ttong547785026
  • 浏览: 5980 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

flex组件重写

    博客分类:
  • flex
 
阅读更多
1.在类声明前插入[Style]元数据标签,语法如下:

[Style(name="style_name"[,property="value",...])]

2.定义一个静态初始器,为样式属性设置默认值。

3.覆写styleChanged()方法,检测样式属性是否改变。

4.覆写updateDisplayList() 方法,在显示组件时加入样式。

一般情况下,我们会使用一个状态变量来标识样式是否改变。在updateDisplayList方法中,首先 检测该标识,再基于新样式设定更新组件外观。注意:updateDisplayList方法只更新因为该样式改变而需要重绘或重新计算的部分外观,而不是 整个组件的外观。

如果要创建一个自定义组件,你需要重写UIComponent类的某些方法,最少需要重写如下方法:构造函数, createChildren(), commitProperties(), measure(), layoutChrome(), updateDisplayList() 。
基础语句结构如下:
package myComponents
{
public class MyComponent extends UIComponent
{    .... }
}
注意包名与你的磁盘目录结构一致。接下来一一讲解每个方法的重写。
构造函数
示例如下:
public function 类名() {
super();
}
注意,AS3中构造函数不支持重载。
createChildren()
此方法的作用是在此自定义组件中创建子组件。
此方法不用你去调用,Flex在你将此自定义组件使用addChild方法加入到父组件时自动调用。示例如下:
// Declare two variables for the component children.
private var text_mc:TextArea;
private var mode_mc:Button;
override protected function createChildren():void {
    // Call the createChildren() method of the superclass.
    super.createChildren();
    // Test for the existence of the children before creating them.
    // This is optional, but do this so a subclass can create a different
    // child.
    if (!text_mc)     {
        text_mc = new TextArea();
        text_mc.explicitWidth = 80;
        text_mc.editable = false;
        text_mc.addEventListener("change", handleChangeEvent);
        // Add the child component to the custom component.
        addChild(text_mc);
    }
    // Test for the existence of the children before creating them.
    if (!mode_mc)     {   
        mode_mc = new Button();
        mode_mc.label = "Toggle Editing";
        mode_mc.addEventListener("click", handleClickEvent);
        // Add the child component to the custom component.
        addChild(mode_mc);
    }
}
上例使用createChildren()在此自定义组件中加入了一个Label和一个Button。
commitProperties()
此方法是在修改组件属性时使用。
此方法不用你去调用。当你调用invalidateProperties()(刷新属性)、addChild()(增加子组件)方法时,Flex会自动调用此方法。这样组件在下次显示时,就能以新的属性来显示。
此方法还有一个作用是为measure()方法提供最新的属性信息。
measure()
此方法的作用是设置组件的默认尺寸。
此方法不用你去调用。当你调用invalidateSize ()(刷新尺寸)、addChild()(增加子组件)方法时,Flex会自动调用此方法。这样组件在下次显示时,就能以默认尺寸来显示。
如果你显式的设置了组件的尺寸,如<mx:Button height="10" width="10"/>,Flex就不用调用此方法了。要注意,measure()方法只是设置组件的默认尺寸,在updateDisplayList()方法中,组件具备的实际尺寸(actual size)与默认尺寸可能不同。
Flex中的每个组件都是有默认尺寸的。如这样写:<mx:Button />,Flex就会自动给一个尺寸。如果你想重写默认尺寸,可以重新设置measuredHeight 、measuredWidth、measuredMinHeight、measuredMinWidth。如下例:
package myComponents
{
    // asAdvanced/myComponents/DeleteTextArea.as
    import mx.controls.Button;
    public class BlueButton extends Button {
        public function BlueButton() {
            super();
        }
       override protected function measure():void {
            super.measure();
            measuredWidth=100;
            measuredMinWidth=50;
            measuredHeight=50;
            measuredMinHeight=25;
        }
    }
}
然后在MXML中使用:
<?xml version="1.0"?>
<!-- asAdvanced/ASAdvancedMainBlueButton.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:MyComp="myComponents.*" >
    <mx:VBox>
        <MyComp:BlueButton/>
        <mx:Button/>
    </mx:VBox>
</mx:Application>
此中的BlueButton就会以默认值100宽,50高来显示。
layoutChrome()
一些容器类(Container)或其子类采用此方法设置组件的border Area(边框区域)。
此方法不用你去调用。当你调用invalidateDisplayList ()(刷新显示)方法时,Flex会自动调用此方法。这样组件在下次显示时,就能以新的边框来显示。
典型的用法是你可以重写RectangularBorder类。
一个将组件的边框区域(border Area)和内容区域(content area)分开处理的原因是当Container.autoLayout=false时。
总括的来讲,layoutChrome()是用来处理边框区域的刷新显示,而updateDisplayList()用来处理内容区域的刷新显示。
updateDisplayList()
此方法不用你去调用。当你调用invalidateDisplayList ()(刷新显示)、addChild()(增加子组件)方法时,Flex会自动调用此方法。这样组件在下次显示时,就能以新的样子来显示。其实类似VC++中的PAINT消息处理。
此方法的主要作用为:
A.更改组件的尺寸和位置。要改变尺寸,在此方法中使用setActualSize()方法,而不是使用width和height属性来完成。要改变位置,在此方法中使用move()方法,而不是使用x和y属性来完成。
B.绘制可视元素,如皮肤、样式、边框。你可以使用Flash Drawing API来完成。
函数的原型为:
protected function updateDisplayList(unscaledWidth:Number,
    unscaledHeight:Number):void
两个参数分别为此自定义组件的宽度和高度。当然如果设置父容器设置scaleY=2,你设置unscaledHeight=100,那么最终呈现的是200高度的组件。
第一个功能示例:
package myComponents
{
    // asAdvanced/myComponents/BottomUpVBox.as
    import mx.containers.VBox;
    import mx.core.EdgeMetrics;
    import mx.core.UIComponent;
    public class BottomUpVBox extends VBox
    {
        public function BottomUpVBox() {
            super();
        }
        override protected function updateDisplayList(unscaledWidth:Number,
            unscaledHeight:Number):void {
            super.updateDisplayList(unscaledWidth, unscaledHeight);
            // Get information about the container border area.
            // The usable area of the container for its children is the
            // container size, minus any border areas.
            var vm:EdgeMetrics = viewMetricsAndPadding;
            // Get the setting for the vertical gap between children.
            var gap:Number = getStyle("verticalGap");
            // Determine the y coordinate of the bottom of the usable area
            // of the VBox.
            var yOfComp:Number = unscaledHeight-vm.bottom;
            // Temp variable for a container child.
            var obj:UIComponent;
            for (var i:int = 0; i < numChildren; i++)
            {
                // Get the first container child.
                obj = UIComponent(getChildAt(i));
                // Determine the y coordinate of the child.
               yOfComp = yOfComp - obj.height;
                // Set the x and y coordinate of the child.
                // Note that you do not change the x coordinate.
                obj.move(obj.x, yOfComp);
                // Save the y coordinate of the child,
                // plus the vertical gap between children.
                // This is used to calculate the coordinate
                // of the next child.
                yOfComp = yOfComp - gap;
            }
         }
    }
}
第二个功能示例:
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
    super.updateDisplayList(unscaledWidth, unscaledHeight);
    // Draw a simple border around the child components.
    graphics.lineStyle(1, 0x000000, 1.0);
    graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);            
}
分享到:
评论

相关推荐

    flex 重写组件

    flex 重写组件

    flex中的组件重写例子

    其中描述了关于flex组件的重写原因、重写组件应注意的问题以及怎样的去重写一个组件和组件的内部执行流程。另外,附有几个例子可供参考!

    flex 高级自定义组件

    flex 高级自定义组件 需要重写的方法,以及为什么要重写,何时调用等。

    SimPager Flex分页组件

    重写的Flex分页组件,改进了一些BUG,原版的源码丢了,又重写了一个,所以没有版本之分 recordCount//记录总数,程序对该属性赋值生成分页 currentPageIndex//当前页 pageSize//每页记录数 buttonConut//显示的...

    flex自定义组件

    这是一个flex4的项目,可以下载下来单独运行,其中重写了组件createChildren、commitProperties、measure和updateDisplayList方法,基本描述了他们在组件生命周期中所起的作用,并且在项目中实现了属性的事件绑定,...

    flex 小节.rar

    Menu 的默认样式改写, 比如把分割线变细,flex 组件的生命周期,flex皮肤样式,动态图片的加载, 文件的上传于下载, 多个二进制图片数据的批量查询与现实。 另外还是都真实项目中的。 挺舍不得的, 但好东西分享...

    flex4自适应高度的TitleWindow

    这是一个flex4的项目,可以下载下来单独运行,项目中是一个自定义的TitleWindow,TitleWindow中存放了一个dataGrid,它可以根据...此组件使用mxml定义,重写了组件updateDisplayList的方法,希望对大家有所帮助。

    flex封装实现画布功能

    * 重写方法,创建子组件 * * */ override protected function createChildren():void { super.createChildren(); shape=new Shape(); shape.graphics.beginFill(bgColor); shape.graphics....

    flex tree 自动显示横向滚动条

    flex tree 自动显示横向滚动条 自定义重写

    Flex_CookBook_读书笔记(精品)

    Flex CookBook 读书笔记主要涵盖了Flex开发中的各种关键概念和技术,包括数组和对象的定义、重写父类方法、调用类的方法、自定义事件、参数类型定义、类型判断、接口实现、样式变更、SwfLoader的使用以及数据源管理...

    flex3的cookbook书籍完整版dpf(包含目录)

    使用Flex组件工具包创建组件 21.2节.在Flash中使用ContainerMovieClip创建Flex容器 21.3节.导入FlashCS3的组件 21.4节.认识Cairngorm小型结构 21.5节.创建Cairngorm视图、事件和模型 21.6节.创建Cairngorm命令和事务...

    Angular-php-sf-flex-webpack-encore-vuejs.zip

    试图使每个组件协同工作:symfony 4(最新的稳定版本,但如果只是更改composer.json中的版本,就可以使用sf 3.3 )、symfony/flex、webpack encore、vuejs 2.5.x、boostrap4 sassymfony示例,Angularjs于2016年发布,...

    TitleWidonw

    Flex4重写组件TitleWindow(最大化、最小化、还原、关闭、拖动、缩放) ######以下凑字数###### ######以下凑字数######

    flex带时分秒控件,项目实现

    带时分秒日历控件,重写DateField组件,项目完美实现

    重写SpringGraph的带标签的IEdgeRenderer

    对SpringGraph组件中的IEdgeRenderer接口进行重写,可以在edge上添加汉字说明做为标签

    使用FLEX 和 Actionscript开发FLASH 游戏(四)-2

    在开发Flash游戏时,FLEX和ActionScript是强大的工具,它们允许开发者创建互动性强、视觉效果丰富的游戏体验。...在实际开发中,还需要结合图形库、物理引擎和其他游戏组件,以构建完整的游戏环境和机制。

    使用FLEX 和 Actionscript开发FLASH 游戏(三)-3

    在开发Flash游戏的过程中,使用FLEX和Actionscript作为主要工具,可以实现丰富的交互性和动态效果。...在实际开发中,结合FLEX的UI组件和Actionscript的面向对象特性,可以构建出复杂且引人入胜的Flash游戏。

    flex fliter

    网上虽然有很多例子,但是写的都有很多bug,因此自己动手写了个组件。出现了一个难点就是如何点击下三角的时候数据源全部出来,看了ComboBox后才知道是这个方法downArrowButton_buttonDownHandler,因此重写这个方法...

    Flex与.NET互操作(十三):FluorineFx.Net实现视频录制与视频回放

    Flex提供了丰富的组件和API,可以方便地集成到FluorineFx提供的服务中。例如,使用`NetConnection`对象连接到服务器,`NetStream`对象则负责视频流的播放和录制。通过监听`NetStatusEvent`和`AsyncErrorEvent`事件,...

Global site tag (gtag.js) - Google Analytics