怎样通过filters设置enabled=false时的Icon
来源:优易学  2011-4-15 16:46:04   【优易学:中国教育考试门户网】   资料下载   IT书店
  对于某些页面,对应的ToolBar上的按钮要置灰(即enabled=false),那么此时对应的图片也要变成灰色,而我又不想给每个按钮去再弄一个灰色按钮(主要不会PS),再者以后的系统是让用户可以自定义上传用户喜欢的图片,这样每次都要上传两张图片,甚是麻烦。
  在这里想到一个办法那就是利用Button的filters的属性来使其图片跟着按钮的状态自动改变
  在这里主要利用ColorMatrixFilter,该类是将 4 x 5的 矩阵转换应用于输入图像上的每个像素的 RGBA 颜色和 Alpha 值,以生成具有一组新的 RGBA 颜色和 Alpha 值的结果。可以允许饱和度更改、色相旋转、亮度为 Alpha 以及各种其它效果。它可以应用与基于DisplayObject 的子类,以及BitmapData 对象,这两类的使用:
  1)、DisplayObject 的子类:使用filters 的属性
  2)、BitmapData :使用 applyFilter() 方法获得一个新的过滤对象
  下面看代码:
  <?xml version="1.0" encoding="utf-8"?>
  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
  <mx:Script>
  <![CDATA[
  import mx.core.IFlexDisplayObject;
  [Embed('assets/google.gif')]
  private var google:Class;
  private var rLum:Number = 0.2225;
  private var gLum:Number = 0.7169;
  private var bLum:Number = 0.0606;
  [Bindable]
  private var bwMatrix:Array = [rLum, gLum, bLum, 0, 0,
  rLum, gLum, bLum, 0, 0,
  rLum, gLum, bLum, 0, 0,
  0, 0, 0, 1, 0];
  private var _colorMatrix:ColorMatrixFilter;
  private function get colorMatrix():ColorMatrixFilter
  {
  if(!_colorMatrix)
  {
  _colorMatrix = new ColorMatrixFilter();
  _colorMatrix.matrix = bwMatrix;
  }
  return _colorMatrix;
  }
  [Bindable]
  private var enable:Boolean = true;
  ]]>
  </mx:Script>
  <mx:VBox>
  <mx:Button id="btn1" icon="{google}" enabled="{enable}"
  filters="{btn1.enabled ? null : [colorMatrix]}"/>
  <mx:Button id="btn2" icon="{google}" enabled="{!enable}"
  filters="{btn2.enabled ? null : [colorMatrix]}"/><mx:Button label="切换" click="{enable = !enable}"/>
  </mx:VBox>
  </mx:Application>
  针对不同对象也可以将其封装到一个类中去,如对于按钮就可以进行如下封装,这样就可以直接用这个类了,而不用分别设置了:
  package com.kissjava.controls
  {
  import flash.filters.ColorMatrixFilter;
  import mx.controls.Button;
  public class KJButton extends Button
  {
  public function KJButton()
  {
  super();
  }
  private var rLum:Number = 0.2225;
  private var gLum:Number = 0.7169;
  private var bLum:Number = 0.0606;
  [Bindable]
  private var bwMatrix:Array = [rLum, gLum, bLum, 0, 0,
  rLum, gLum, bLum, 0, 0,
  rLum, gLum, bLum, 0, 0,
  0, 0, 0, 1, 0];
  private var _colorMatrix:ColorMatrixFilter;
  private function get colorMatrix():ColorMatrixFilter
  {
  if(!_colorMatrix)
  {
  _colorMatrix = new ColorMatrixFilter();
  _colorMatrix.matrix = bwMatrix;
  }
  return _colorMatrix;
  }
  override public function set enabled(value:Boolean):void
  {
  super.enabled = velue;
  this.filters = value ? null : [colorMatrix]
  }
  }
  }

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训