Apr 27
在编写相册等一些图片应用的时候,将彩色照片转化为黑白的是一个必不可少的功能。接下来这个例子演示了如何利用ColorMatrixFilter将图片转换为黑白图片。这其实是Flash文档的“Learning ActionScript 2.0″这本书中一个例子(click here)。这里只不过是将其在Flex中实现。更多的相关信息,可以看ColorMatrixFilter - Flex 3 Language Reference。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" backgroundColor="white">
- <mx:Script>
- <![CDATA[
- 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];
- [Bindable]
- [Embed('assets/image2.jpg')]
- private var image2:Class;
- ]]>
- </mx:Script>
- <mx:ColorMatrixFilter id="cmf" matrix="{bwMatrix}" />
- <mx:VBox>
- <mx:Label text="Black and white" />
- <mx:Image source="{image2}" filters="{[cmf]}" scaleX="0.5" scaleY="0.5" />
- </mx:VBox>
- <mx:VBox>
- <mx:Label text="Original" />
- <mx:Image source="{image2}" scaleX="0.5" scaleY="0.5" />
- </mx:VBox>
- </mx:Application>
代码:Peter deHaan 翻译/整理/编译:minidxer
