咖啡伴侣

呆在上海
posts - 163, comments - 156, trackbacks - 0, articles - 2

Flex ColorLabel 实现

Posted on 2008-03-28 09:58 oathleo 阅读(481) 评论(0)  编辑  收藏 所属分类: Flex
 public class ColorLabel extends Label
 {
  private var colorValue:Number = -1;
  public function ColorLabel()
  {
   super();
  }
  
  public function setColorValue(colorValue:Number):void
  {
   this.colorValue = colorValue;
  }
  
  override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
     {
            super.updateDisplayList(unscaledWidth, unscaledHeight);
            if(colorValue>=0){
             drawColor(colorValue);
            }
     }
  
  private function drawColor(colorValue:Number):void
     {
        this.graphics.beginFill(colorValue);
              this.graphics.drawRect(this.textField.x,this.textField.y,this.textWidth ,this.textHeight);
              this.graphics.endFill();
     }
 }
 
 
 *  <p>In general, components do not override the <code>validateProperties()</code>,
 *  <code>validateSize()</code>, or <code>validateDisplayList()</code> methods

 *  In the case of UIComponents, most components override the
 *  <code>commitProperties()</code>, <code>measure()</code>, or
 *  <code>updateDisplayList()</code> methods
, which are called
 *  by the <code>validateProperties()</code>,
 *  <code>validateSize()</code>, or
 *  <code>validateDisplayList()</code> methods, respectively.</p>
 
 

 

Implementing the commitProperties() method

You use the commitProperties() method to coordinate modifications to component properties. Most often, you use it with properties that affect how a component appears on the screen.

Flex schedules a call to the commitProperties() method when a call to the invalidateProperties() method occurs. The commitProperties() method executes during the next render event after a call to the invalidateProperties() method. When you use the addChild() method to add a component to a container, Flex automatically calls the invalidateProperties() method.

Calls to the commitProperties() method occur before calls to the measure() method. This lets you set property values that the measure() method might use.

Implementing the measure() method

The measure() method sets the default component size, in pixels, and optionally sets the component's default minimum size.

Flex schedules a call to the measure() method when a call to the invalidateSize() method occurs. The measure() method executes during the next render event after a call to the invalidateSize() method. When you use the addChild() method to add a component to a container, Flex automatically calls the invalidateSize() method.

Implementing the updateDisplayList() method

The updateDisplayList() method sizes and positions the children of your component based on all previous property and style settings, and draws any skins or graphic elements that the component uses. The parent container for the component determines the size of the component itself.

A component does not appear on the screen until its updateDisplayList() method gets called. Flex schedules a call to the updateDisplayList() method when a call to the invalidateDisplayList() method occurs. The updateDisplayList() method executes during the next render event after a call to the invalidateDisplayList() method. When you use the addChild() method to add a component to a container, Flex automatically calls the invalidateDisplayList() method.

Drawing graphics in your component

Every Flex component is a subclass of the Flash Sprite class, and therefore inherits the Sprite.graphics property. The Sprite.graphics property specifies a Graphics object that you can use to add vector drawings to your component.

For example, in the updateDisplayList() method, you can use methods of the Graphics class to draw borders, rules, and other graphical elements:

 

总结:

修改属性用commitProperties,自己画用updateDisplayList


只有注册用户登录后才能发表评论。


网站导航: