通過(guò)filters設(shè)置enabled=false時(shí)的Icon

字號(hào):

對(duì)于某些頁(yè)面,對(duì)應(yīng)的ToolBar上的按鈕要置灰(即enabled=false),那么此時(shí)對(duì)應(yīng)的圖片也要變成灰色,而我又不想給每個(gè)按鈕去再弄一個(gè)灰色按鈕(主要不會(huì)PS),再者以后的系統(tǒng)是讓用戶可以自定義上傳用戶喜歡的圖片,這樣每次都要上傳兩張圖片,甚是麻煩。
    在這里想到一個(gè)辦法那就是利用Button的filters的屬性來(lái)使其圖片跟著按鈕的狀態(tài)自動(dòng)改變
    在這里主要利用ColorMatrixFilter,該類是將 4 x 5的 矩陣轉(zhuǎn)換應(yīng)用于輸入圖像上的每個(gè)像素的 RGBA 顏色和 Alpha 值,以生成具有一組新的 RGBA 顏色和 Alpha 值的結(jié)果??梢栽试S飽和度更改、色相旋轉(zhuǎn)、亮度為 Alpha 以及各種其它效果。它可以應(yīng)用與基于DisplayObject 的子類,以及BitmapData 對(duì)象,這兩類的使用:
    1)、DisplayObject 的子類:使用filters 的屬性
    2)、BitmapData :使用 applyFilter() 方法獲得一個(gè)新的過(guò)濾對(duì)象
    下面看代碼:
    
    
    
        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;
    ]]>
    

    
        filters="{btn1.enabled ? null : [colorMatrix]}"/>
        filters="{btn2.enabled ? null : [colorMatrix]}"/>
    

    

    針對(duì)不同對(duì)象也可以將其封裝到一個(gè)類中去,如對(duì)于按鈕就可以進(jìn)行如下封裝,這樣就可以直接用這個(gè)類了,而不用分別設(shè)置了:
    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]
    }
    }
    }