【AS3代码】遮罩用法
package
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.net.URLRequest;
import flash.filters.BitmapFilterQuality;
public class Main extends Sprite
{
private var _bitmap:Bitmap;
private var _circleMask:Sprite;
public function Main():void
{
//初始化遮罩圆
initMask();
//读取被遮罩的背景图
startLoadImg();
}
private function initMask():void
{
_circleMask = new Sprite();
_circleMask.graphics.beginFill(0xff0000);
_circleMask.graphics.drawCircle(60,60,60);
_circleMask.graphics.endFill();
//用滤镜模糊化
_circleMask.filters = [new BlurFilter(20, 20, BitmapFilterQuality.HIGH)];
_circleMask.cacheAsBitmap = true;
this.addChild(_circleMask);
//遮罩圆可拖拽
_circleMask.startDrag(true);
}
private function startLoadImg():void
{
var loader:Loader = new Loader();
var request:URLRequest = new URLRequest("a.jpg");
loader.load(request);
//a.jpg读取完成后事件触发
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
}
private function loaded(evt:Event):void
{
_bitmap = evt.target.content as Bitmap;
this.addChild(_bitmap);
_bitmap.cacheAsBitmap = true;
//给a.jpg设置一个圆形遮罩
_bitmap.mask = _circleMask;
}
}
}
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.net.URLRequest;
import flash.filters.BitmapFilterQuality;
public class Main extends Sprite
{
private var _bitmap:Bitmap;
private var _circleMask:Sprite;
public function Main():void
{
//初始化遮罩圆
initMask();
//读取被遮罩的背景图
startLoadImg();
}
private function initMask():void
{
_circleMask = new Sprite();
_circleMask.graphics.beginFill(0xff0000);
_circleMask.graphics.drawCircle(60,60,60);
_circleMask.graphics.endFill();
//用滤镜模糊化
_circleMask.filters = [new BlurFilter(20, 20, BitmapFilterQuality.HIGH)];
_circleMask.cacheAsBitmap = true;
this.addChild(_circleMask);
//遮罩圆可拖拽
_circleMask.startDrag(true);
}
private function startLoadImg():void
{
var loader:Loader = new Loader();
var request:URLRequest = new URLRequest("a.jpg");
loader.load(request);
//a.jpg读取完成后事件触发
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
}
private function loaded(evt:Event):void
{
_bitmap = evt.target.content as Bitmap;
this.addChild(_bitmap);
_bitmap.cacheAsBitmap = true;
//给a.jpg设置一个圆形遮罩
_bitmap.mask = _circleMask;
}
}
}