ActionScript图形之滤镜
ActionScript图形之滤镜
2011-03-10 11:24:31| 分类: flex | 标签:actionscript图形之滤镜 |字号大中小 订阅
什么是滤镜?
滤镜主要是用来实现图像的各种特殊效果。如:钝化,斜角边缘,发光,扭曲图像...
flash.filters包提供如下滤镜:
BlurFilter
模糊滤镜的属性:
blurX:在X方向上模糊的像素数量。默认是4
blurY:在Y方向上模糊的像素数量。默认是4
quality:执行模糊处理的次数。范围0-15,默认是1。
DropShadowFilter
投影滤镜的属性:
blurX,blurY,quality
distance:从显示对象到阴影的偏移像素数量。默认是4
angle:光源角度。默认是45度
color:阴影的颜色。默认是0x000000
alpha:阴影的alpha值,0-1。默认是1,不透视。
strength:阴影的长度,0-255。默认是1
inner:阴影是否应该应用到对象内部。默认是false
knockout:原始内容是否应该透明显示。默认是false
hideObject:是否隐藏原始内容。默认是false
BevelFilter
斜角滤镜
GlowFilter
发光滤镜
GradientBevelFilter
渐进斜角滤镜
GradientGlowFilter
渐进发光滤镜
ColorMatrixFilter
ConvolutionFilter
DisplacementMapFilter
转换滤镜--扭曲
例子:
package {
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.filters.DropShadowFilter;
import flash.text.TextField;
public class TestFilter extends Sprite
{
private var testTxt:TextField;
private var btn:SimpleButton;//按钮
private var txt:TextField;//按钮上的文字
private var ds:DropShadowFilter;
public function TestFilter()
{
testTxt = new TextField();
ds = new DropShadowFilter();//投影滤镜
//ds.distance = 100;//比较效果
testTxt.filters = [ds];
ds.distance = 100;//不起作用,必须在前面修改
testTxt.text = "Test DropShadowFilter";
testTxt.width = 200;
testTxt.x = 10;
testTxt.y = 60;
this.addChild(testTxt);
var d:Sprite = new Sprite();
d.graphics.beginFill(0x00ff00,1);
d.graphics.drawRect(400,60,30,30);
d.graphics.endFill();
btn = new SimpleButton(d, d, d, d);
txt = new TextField();
txt.text = "变化";
txt.x = 400;
txt.y = 60;
this.addChild(btn);
this.addChild(txt);
txt.addEventListener(MouseEvent.CLICK, onTxtClick);
}
private function onTxtClick(evt:MouseEvent):void
{
var dis:int = DropShadowFilter(testTxt.filters[0]).distance;
if(dis == 100)
{
ds = new DropShadowFilter();
ds.distance = 4;
testTxt.filters = [ds];
}
else
{
ds = new DropShadowFilter();
ds.distance = 100;
testTxt.filters = [ds];
}
}
}
}
注意:
1.滤镜应用到某个对象后,就不能修改滤镜的属性。
2.修改对象的滤镜,必须创建一个新的滤镜,并将它应用到该对象上。
添加多个滤镜
xxx.filters = [ds1, ds2, ds2];
旋转对象
1.尝试对TextField对象旋转(ratation属性)。失败
2.将TextField转换为位图,然后再旋转。
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
public class TestRotation extends Sprite
{
private var testTxt:TextField;
private var btn:SimpleButton;//按钮
private var txt:TextField;//按钮上的文字
private var bmpData:BitmapData;
private var bmp:Bitmap;
private var newsprite:Sprite;
public function TestRotation()
{
testTxt = new TextField();
testTxt.text = "Test Ratation";
//picture
bmpData = new BitmapData(testTxt.width, testTxt.height);
bmpData.draw(testTxt);
bmp = new Bitmap(bmpData);
newsprite = new Sprite();
newsprite.x = 200;
newsprite.y = 150;
this.addChild(testTxt);
this.addChild(newsprite);
newsprite.addChild(bmp);
var d:Sprite = new Sprite();
d.graphics.beginFill(0x00ff00,1);
d.graphics.drawRect(400,60,30,30);
d.graphics.endFill();
btn = new SimpleButton(d, d, d, d);
txt = new TextField();
txt.text = "变化";
txt.x = 400;
txt.y = 60;
this.addChild(btn);
this.addChild(txt);
txt.addEventListener(MouseEvent.CLICK, onTxtClick);
}
private function onTxtClick(evt:MouseEvent):void
{
trace("click...");
newsprite.rotation += 90;
}
}
}