Flash/Flex学习笔记(22):滤镜学习
Silverlight中称之为“效果(Effect)”的东东,在Flash里叫“滤镜(Filter)",而且Flash里内置的滤镜要比Silverlight丰富很多,几乎所有对象都有一个filters的属性,它是一个数组,可以将一个或多个滤镜同时应用于某一对象,类似的,如果要去除已经应用的滤镜效果,只要将filters数组清空即可
01
var
_data:DataProvider =
new
DataProvider();
02
03
_data.addItem({
label
:
"不应用任何滤镜"
,data:
""
});
04
_data.addItem({
label
:
"Blur(模糊滤镜)"
,data:
"Blur"
});
05
_data.addItem({
label
:
"Drop shadow(投影滤镜)"
,data:
"DropShadow"
});
06
_data.addItem({
label
:
"Glow(发光滤镜)"
,data:
"Glow"
});
07
_data.addItem({
label
:
"Gradient bevel(渐变斜角滤镜)"
,data:
"GradientBevel"
});
08
_data.addItem({
label
:
"Convolution(卷积滤镜)"
,data:
"Convolution"
});
09
_data.addItem({
label
:
"Blur(模糊滤镜)+DropShadow(投影滤镜)"
,data:
"Blur&DropShadow"
});
10
11
cbo1.dataProvider=_data;
12
13
cbo1.addEventListener(Event.CHANGE,ChangeHandler);
14
15
function
ChangeHandler(e:Event):
void
{
16
var
_item:
Object
= (e.currentTarget
as
ComboBox).selectedItem;
17
//trace(_item.data);
18
switch
(_item.data) {
19
case
"Blur"
:
20
img.filters=[
new
BlurFilter(
5
,
5
,
3
)];
21
break
;
22
case
"DropShadow"
:
23
var
_shadow:DropShadowFilter =
new
DropShadowFilter();
24
_shadow.distance=
10
;
25
_shadow.alpha=
0.3
;
26
img.filters=[_shadow];
27
break
;
28
case
"Glow"
:
29
img.filters=[
new
GlowFilter(
0xffff99
,
0.5
,
40
,
40
,
3
,
1
,
false
)];
30
break
;
31
case
"GradientBevel"
:
32
var
bgColor:
uint
=
0xCCCCCC
;
33
var
size:
uint
=
80
;
34
var
offset:
uint
=
50
;
35
var
distance:
Number
=
5
;
36
var
angleInDegrees:
Number
=
225
;
// opposite 45 degrees
37
var
colors:
Array
=[
0xFFFFFF
,
0xCCCCCC
,
0x000000
];
38
var
alphas:
Array
=[
1
,
0.1
,
1
];
39
var
ratios:
Array
=[
0
,
128
,
255
];
40
var
blurX:
Number
=
8
;
41
var
blurY:
Number
=
8
;
42
var
strength:
Number
=
2
;
43
var
quality:
Number
=BitmapFilterQuality.HIGH;
44
var
type:
String
=BitmapFilterType.FULL;
45
var
knockout:
Boolean
=
false
;
46
47
img.filters = [
new
GradientBevelFilter(distance,
48
angleInDegrees,
49
colors,
50
alphas,
51
ratios,
52
blurX,
53
blurY,
54
strength,
55
quality,
56
type,
57
knockout)];
58
break
;
59
case
"Convolution"
:
60
var
clamp:
Boolean
=
false
;
61
var
clampColor:
Number
=
0xFF0000
;
62
var
clampAlpha:
Number
=
1
;
63
var
bias:
Number
=
0
;
64
var
preserveAlpha:
Boolean
=
false
;
65
var
matrixCols:
Number
=
3
;
66
var
matrixRows:
Number
=
3
;
67
var
matrix:
Array
= [
1
,
1
,
1
,
68
1
,
1
,
1
,
69
1
,
1
,
1
];
70
71
var
filter:ConvolutionFilter=
new
ConvolutionFilter(matrixCols,matrixRows,matrix,matrix.length,bias,preserveAlpha,clamp,clampColor,clampAlpha);
72
73
img.filters=[filter];
74
break
;
75
case
"Blur&DropShadow"
:
76
var
_shadow2:DropShadowFilter =
new
DropShadowFilter();
77
_shadow2.distance=
10
;
78
_shadow2.alpha=
0.3
;
79
img.filters=[
new
BlurFilter(
5
,
5
,
3
),_shadow2];
80
break
;
81
case
""
:
82
default
:
83
img.filters=[];
84
break
;
85
}
86
87
}
01
package
{
02
import
flash.display.Sprite;
03
import
flash.events.Event;
04
import
flash.filters.DropShadowFilter;
05
import
flash.filters.BlurFilter;
06
public
class
DynamicFilter
extends
Sprite {
07
08
private
var
dropShadowFilter:DropShadowFilter;
09
private
var
blurFilter:BlurFilter;
10
11
12
public
function
DynamicFilter() {
13
init();
14
}
15
16
private
function
init():
void
{
17
//注:mm是舞台上的一个图片实例
18
mm.x = stage.stageWidth/
2
;
19
mm.y = stage.stageHeight/
2
;
20
21
dropShadowFilter =
new
DropShadowFilter();
22
dropShadowFilter.alpha =
0.3
;
23
blurFilter =
new
BlurFilter();
24
25
addEventListener(Event.ENTER_FRAME, EnterFrameHandler);
26
}
27
28
private
function
EnterFrameHandler(event:Event):
void
{
29
var
dx:
Number
= mouseX - mm.x;
30
var
dy:
Number
= mouseY - mm.y;
31
dropShadowFilter.distance = - Math.sqrt(dx*dx+dy*dy)/
10
;
32
dropShadowFilter.angle = Math.atan2(dy,dx)*
180
/Math.PI;
33
blurFilter.blurX = Math.abs(dx)/
30
;
34
blurFilter.blurY = Math.abs(dy)/
30
;
35
mm.filters = [blurFilter,dropShadowFilter];
36
}
37
}
38
}