【AS3代码】放大镜

package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    
    public class Main extends Sprite
    {        
        private var xiao_tu:Bitmap;        //原小图
        private var da_tu:Bitmap;        //大图
        private var loader:Loader;        //加载图像对象
        private var url:URLRequest;        //图像URL对象
        
        public function Main():void
        {
            init();
        }
        private function init():void
        {
            loader = new Loader();            
            url = new URLRequest("a.jpg");
            loader.load(url);
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadok);
        }
        
        private function loadok(evt:Event):void
        {
            xiao_tu = new Bitmap(evt.target.content.bitmapData);  //把载入的图片放到的原图对象里    
            xiao_tu.width = 400;
            xiao_tu.height = 300;
            this.addChild(xiao_tu);
            
            //创建放大镜的镜片
            var tu:Sprite = new Sprite();
            tu.graphics.beginFill(0x000000);
            tu.graphics.drawCircle(0,0,50);
            tu.graphics.endFill();
            tu.startDrag(true);
            this.addChild(tu);
            
            //绘制大图片
            da_tu = new Bitmap(evt.target.content.bitmapData);
            da_tu.mask = tu;
            this.addChild(da_tu);
            
            stage.addEventListener(MouseEvent.MOUSE_MOVE,onmove);
        }
        
        private function onmove(evt:MouseEvent):void
        {
            da_tu.x = (da_tu.width - 400) * (stage.mouseX / 400) * -1;
            da_tu.y = (da_tu.height - 300) * (stage.mouseY / 300) * -1;
        }
    }
}
posted @ 2012-06-19 11:39  王翔(kingfly)  阅读(1190)  评论(0编辑  收藏  举报