AS2.0ÖÐÓÐMovieClipºÍÈ«¾ÖÖÐÓÐloadMovieº¯Êý¿ÉÒÔ¶¯Ì¬¼ÓÔØͼƬ£¬µ«ÊÇÕâ²»ÊÇÒì²½¼ÓÔØ£¬¶øÇÒû·¨ÖªµÀͼƬ´óС¡£ÔÚAS3.0ÖУ¬ÓÐLoaderÀàÀ´ÊµÏÖ´ÓÍⲿÔØÈëswfºÍ¸÷ÖÖͼƬ
AS2.0ÖÐÓÐMovieClipºÍÈ«¾ÖÖÐÓÐloadMovieº¯Êý¿ÉÒÔ¶¯Ì¬¼ÓÔØͼƬ£¬µ«ÊÇÕâ²»ÊÇÒì²½¼ÓÔØ£¬¶øÇÒû·¨ÖªµÀͼƬ´óС¡£ÔÚAS3.0ÖУ¬ÓÐLoaderÀàÀ´ÊµÏÖ´ÓÍⲿÔØÈëswfºÍ¸÷ÖÖͼƬ¡£
Ч¹ûÈçÏ£¬ÔØÈëµÄ¹ý³ÌÖлáÓнø¶ÈÌõÌáʾ£¬ÔØÈëÍê³É¹ýºó¿ÉÒÔ¸ù¾ÝͼƬµÄ³ß´ç½øÐÐËõ·Å£¬°´±ÈÀýµÄËõС·ÅÔÚ400×300µÄ·½¿òÖУ¬¾ÓÖÐÏÔʾ
AS´úÂë
- progress.visible = false;
- btnSubmit.addEventListener(MouseEvent.CLICK,btnSubmitClickHandler);
- function btnSubmitClickHandler(e:MouseEvent) {
- loadPicture(ddlUrl.value);
- }
-
- var loader:Loader;
- function loadPicture(url:String) {
- if(loader == null){
- loader = new Loader();
- }else{
- loader.unload();
- txtAlert.text = "";
- }
- loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHd);
- loader.contentLoaderInfo.addEventListener("ioError",errorHd);
- var request:URLRequest = new URLRequest(url);
- var context:LoaderContext = new LoaderContext(true);
- loader.load(request,context);
- progress.visible = true;
- }
-
- var maxHeight:int = container.height;
- var maxWidth:int = container.width;
- function completeHd(e:Event) {
- txtAlert.text = "* Load success!";
- var pic:Bitmap = loader.content as Bitmap;
- pic.smoothing = true;
- var currentWidth:int = loader.width;
- var currentHeight:int = loader.height;
- if(currentWidth > maxWidth){
- currentWidth = maxWidth;
- currentHeight = currentWidth/loader.width * loader.height;
- }
- if(currentHeight > maxHeight){
- currentHeight = maxHeight;
- currentWidth = currentHeight/loader.height * loader.width;
- }
- loader.content.width = currentWidth;
- loader.content.height = currentHeight;
-
- loader.x = (maxWidth - currentWidth)/2;
- loader.y = (maxHeight - currentHeight)/2;
- container.addChild(loader);
- progress.visible = false;
- }
- function errorHd(e:Event){
- progress.visible = false;
- txtAlert.text = "* Url is invalid, Please try again for another url!";
- }
¹Ø¼üÔÚÓÚ¶ÔLoaderÀàµÄʹÓã¬Loader¾ßÓÐcontentLoaderInfoÊôÐÔ£¬´ËÊôÐÔÊÇÒ»¸öLoaderInfoÀàµÄʵÀý£¬ÓÃÀ´¿ØÖƼÓÔØÍⲿ×ÊÔ´µÄ½ø¶È£¬Òò´Ë¾Í¿ÉÒÔÔÚcontentLoaderInfoÉÏ×¢²á¸÷ÖÖʼþ£¬ÕâÀïÖ»ÓÃÁËCompleteʼþ£¬ÓÃÓÚ¼ÓÔØÍê³Éʱµ÷Óá£
Loader»¹¾ßÓÐcontentÊôÐÔ£¬LoaderʵÀý±¾Éí¾ÍÊÇÒ»¸ö¿ÉÊÓ¶ÔÏó£¬contentÒ²ÊÇÒ»¸ö¿ÉÊÓ¶ÔÏó£¬Èç¹û¼ÓÔصĵØÖ·ÊÇÒ»¸öͼƬµÄ»°£¬contentÊÇÒ»¸öBitmap¶ÔÏó£¬ËùÒÔ¿ÉÒÔͨ¹ýתÐÍÀ´Ê¹µÃÔØÈëµÄͼƬÔÚËõСµÄʱºòÏû³ý¾â³Ý£¬½«BitmapµÄsmoothingÊôÐÔÉèÖÃΪtrue¼´¿É¡£