AS3.0 图片自适应效果实例

效果演示:(IE8下可正常浏览)

下载地址:/Files/zhoujunfeng2011/SWF/Test.swf

各位朋友可下载到本地进行缩放窗口进行测试!

源码:

Main.as:

 

package 
{
    import flash.display.Sprite;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.display.Bitmap;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.display.StageScaleMode;
    import flash.display.Shape;
    import Effters.loading;
    import flash.events.IOErrorEvent;

    public class Main extends Sprite
    {
        
// 首次加载的图片url
        private  var  url:String  =  " http://******????????" ;
        
// 后续加载的图片url
        private  var  _URL:String  =   " http://******?????? " ;
        private 
var  _URL2:String  =   " .*???????? " ;
        private 
var  LoadURL:String;
        private 
var  loader:Loader;
        private 
var  test:Sprite  =   new  Sprite();
        private 
var  stage_width:Number;
        private 
var  stage_height:Number;
        private 
var  test_width:Number;
        private 
var  test_height:Number;
        private 
var  _X:TextField  =   new  TextField();
        private 
var  _Y:TextField  =   new  TextField();
        private 
var  _W:TextField  =   new  TextField();
        private 
var  _H:TextField  =   new  TextField();
        private 
var  _RECT:TextField  =   new  TextField();
        private 
var  _F:TextFormat  =   new  TextFormat();
        private 
var  ErrorText:TextField  =   new  TextField();
        private 
var  rect:Shape  =   new  Shape();
        private 
var  loadingeffters:loading;
        private 
var  upbtn:Sprite;
        private 
var  downbtn:Sprite;
        private 
var  index: int   =   0 ;
        public 
function  Main(): void
        {
            init();
        }
        private 
function  init(): void
        {
            stage.scaleMode 
=  StageScaleMode.NO_SCALE;
            stage_width 
=   this .stage.stageWidth;
            stage_height 
=   this .stage.stageHeight;
            rect.graphics.beginFill(
0xffff00 , 0.6 );
            rect.graphics.drawRect(
0 , 0 , 20 , 20 );
            rect.graphics.endFill();
            addChild(rect);
            rect.y 
=   this .stage.stageHeight  -  rect.height;
            _F.color 
=   0xffffff ;
            _F.size 
=   14 ;
            _X.text 
=   " 鼠标X轴: "   +   " 0 " ;
            _Y.text 
=   " 鼠标Y轴: "   +   " 0 " ;
            _W.text 
=   " 当前舞台宽度: "   +   this .stage.stageWidth;
            _H.text 
=   " 当前舞台高度: "   +   this .stage.stageHeight;
            _RECT.text 
=   " 方块X、Y: "   +  rect.x  +   " , "   +  rect.y;
            _X.setTextFormat(_F);
            _Y.setTextFormat(_F);
            _W.setTextFormat(_F);
            _H.setTextFormat(_F);
            _RECT.setTextFormat(_F);
            addChild(_X);
            addChild(_Y);
            addChild(_W);
            addChild(_H);
            addChild(_RECT);
            _X.y 
=   10 ;
            _Y.y 
=   30 ;
            _W.y 
=   50 ;
            _H.y 
=   70 ;
            _RECT.y 
=   90 ;
            _X.height 
=   20 ;
            _Y.height 
=   20 ;
            _W.height 
=   20 ;
            _H.height 
=   20 ;
            _RECT.height 
=   20 ;
            _W.width 
=   150 ;
            _H.width 
=   150 ;
            _RECT.width 
=   150 ;
            upbtn 
=  Draws( 40 , 15 , 6 , 0xFF99FF );
            downbtn 
=  Draws( 40 , 15 , 6 , 0xFFCCFF );
            
// 添加按钮
            addChild(downbtn);
            downbtn.x 
=  rect.x  +   25 ;
            downbtn.y 
=   this .stage.stageHeight  -   20 ;
            downbtn.buttonMode 
=   true ;
            addChild(upbtn);
            upbtn.x 
=  downbtn.x  +   45 ;
            upbtn.y 
=  downbtn.y;
            upbtn.buttonMode 
=   true ;
            
var  upbtntext:TextField  =   new  TextField();
            
var  downbtntext:TextField  =   new  TextField();
            upbtntext.text 
=   " 下一张 " ;
            upbtntext.width 
=   40 ;
            downbtntext.text 
=   " 上一张 " ;
            downbtntext.width 
=   40 ;
            upbtn.addChild(upbtntext);
            downbtn.addChild(downbtntext);
            upbtntext.y 
=  downbtntext.y  =   - 2 ;
            upbtntext.selectable 
=  downbtntext.selectable  =   false ;
            upbtntext.mouseEnabled 
=  downbtntext.mouseEnabled  =   false ;
            
// 监听舞台改变尺寸事件
             this .stage.addEventListener(Event.RESIZE,ReSizeHalder);
            
// 监听鼠标移动事件;
             this .stage.addEventListener(MouseEvent.MOUSE_MOVE,MoveHalder);
            
// 监听鼠标点击按钮事件;
            upbtn.addEventListener(MouseEvent.CLICK,up_click);
            downbtn.addEventListener(MouseEvent.CLICK,down_click);
            ErrorText.y 
=   110 ;
            ErrorText.height 
=   20 ;
            ErrorText.width 
=   200 ;
            ErrorText.textColor 
=   0xffffff ;
            loader 
=   new  Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE,com);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,error);
            loader.load(
new  URLRequest(url));
            ErrorText.text 
=   " 图片加载中... " ;
            addChild(ErrorText);
            
// 添加loading效果
            loadingeffters  =   new  loading();
            addChild(loadingeffters);
            loadingeffters.x 
=  stage.stageWidth  /   3.5 ;
            loadingeffters.y 
=  stage.stageHeight  /   3.5 ;
        }
        private 
function  error(evt:IOErrorEvent): void
        {
            removeChild(loadingeffters);
            ErrorText.textColor 
=   0xFF0000 ;
            ErrorText.text 
=   " 图片加载失败 " ;
            loader.unload();
            loader 
=   null ;
        }
        private 
function  com(evt:Event): void
        {
            
// 移除loader监听事件
            ErrorText.text  =   "" ;
            loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,com);
            loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,error);
            
// 加载完成,移除loading效果;
            removeChild(loadingeffters);
            
// 显示图片等
             var  bitmap:Bitmap  =  Bitmap(loader.content);
            test.addChild(bitmap);
            test_width 
=  test.width;
            test_height 
=  test.height;
            addChild(test);
            
// 设置图片大小位置
            setvideosize(test,stage_width,stage_height, this .stage.stageWidth, this .stage.stageHeight,test_width,test_height);
            loader.unload();
            loader 
=   null ;
            

        }
        private 
function  MoveHalder(evt:MouseEvent): void
        {
            _X.text 
=   " 鼠标X轴: "   +   this .mouseX;
            _Y.text 
=   " 鼠标Y轴: "   +   this .mouseY;
            _X.setTextFormat(_F);
            _Y.setTextFormat(_F);
        }
        private 
function  ReSizeHalder(evt:Event): void
        {
            rect.x 
=  (stage_width  -   this .stage.stageWidth) / 2;
            rect.y  =  (stage_height  -   this .stage.stageHeight) / 2 + this.stage.stageHeight - rect.height;
            downbtn.x  =  (stage_width  -   this .stage.stageWidth) / 2 + 25;
            downbtn.y  =  (stage_height  -   this .stage.stageHeight) / 2 + this.stage.stageHeight - 20;
            upbtn.x  =  downbtn.x  +   45 ;
            upbtn.y 
=  downbtn.y;
            
if  (loadingeffters.parent  !=   true )
            {
                loadingeffters.x 
=   this .stage.stageWidth  /   3.5 ;
                loadingeffters.y 
=   this .stage.stageHeight  /   3.5 ;
            }
            _W.text 
=   " 当前舞台宽度: "   +   this .stage.stageWidth;
            _H.text 
=   " 当前舞台高度: "   +   this .stage.stageHeight;
            _RECT.text 
=   " 方块X、Y: "   +  rect.x  +   " , "   +  rect.y;
            _W.setTextFormat(_F);
            _H.setTextFormat(_F);
            _RECT.setTextFormat(_F);
            setvideosize(test,stage_width,stage_height,
this .stage.stageWidth, this .stage.stageHeight,test_width,test_height);
        }
        private 
function  setvideosize(video:Sprite,stage_width:Number,stage_height:Number,StageWidth:Number,StageHeight:Number,loadvideowidth:Number, loadvideoheight:Number): void
        {
            
if  ((loadvideowidth  /  loadvideoheight)  >  (StageWidth  /  StageHeight))
            {
                video.width 
=  StageWidth;
                video.height 
=  StageWidth  /  (loadvideowidth  /  loadvideoheight);
                video.x 
=  (stage_width  -  StageWidth) / 2;
                video.y  =  (stage_height  -  StageHeight) / 2 + Math.abs((StageHeight - video.height) / 2 );
            }
            
else   if  ((loadvideowidth  /  loadvideoheight)  <  (StageWidth  /  StageHeight))
            {
                video.height 
=  StageHeight;
                video.width 
=  StageHeight  *  (loadvideowidth  /  loadvideoheight);
                video.x 
=  (stage_width  -  StageWidth) / 2 + (Math.abs(StageWidth - video.width) / 2 );
                video.y 
=  (stage_height  -  StageHeight) / 2;
            }
            
else   if  ((loadvideowidth  /  loadvideoheight)  ==  (StageWidth  /  StageHeight))
            {
                video.width 
=  StageWidth;
                video.height 
=  StageHeight;
                video.x 
=  (stage_width  -  StageWidth) / 2;
                video.y  =  (stage_height  -  StageHeight) / 2;
            }
        }
        private 
function  Draws(drawwidth:Number,drawheight:Number,ellipseWidth:Number,colors:uint):Sprite
        {
            
var  drawsprite:Sprite  =   new  Sprite();
            drawsprite.graphics.beginFill(colors,
0.8 );
            drawsprite.graphics.drawRoundRect(
0 , 0 ,drawwidth,drawheight,ellipseWidth);
            drawsprite.graphics.endFill();

            
return  drawsprite;
        }
        private 
function  up_click(evt:MouseEvent): void
        {
            
if  (loader  ==   null )
            {
                index
-- ;
                
if  (index  <   1 )
                {
                    index 
=   26 ;
                }
                loadpictures();
            }
        }
        private 
function  down_click(evt:MouseEvent): void
        {
            
if  (loader  ==   null )
            {
                index
++ ;
                
if  (index  >   26 )
                {
                    index 
=   1 ;
                }
                loadpictures();
            }
        }
        private 
function  loadpictures(): void
        {
            LoadURL 
=  _URL  +  index  +  _URL2;
            loader 
=   new  Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE,piccom);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,error);
            loader.load(
new  URLRequest(LoadURL));
            ErrorText.textColor 
=   0xffffff ;
            ErrorText.text 
=   " 图片加载中... " ;
            
// 添加loading特效
             if  (loadingeffters.parent  ==   null )
            {
                addChild(loadingeffters);
            }
        }
        private 
function  piccom(evt:Event): void
        {
            ErrorText.text 
=   "" ;
            loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,piccom);
            loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,error);
            
if  (loadingeffters.parent  !=   null )
            {
                removeChild(loadingeffters);
            }
            
var  bitmap:Bitmap  =  Bitmap(loader.content);
            
if  (test.numChildren  !=   0 )
            {
                test.removeChildAt(
0 );
            }
            
this .addChildAt(test, this .parent.numChildren  - 1 );
            test.addChild(bitmap);
            test_width 
=  test.width;
            test_height 
=  test.height;
            setvideosize(test,stage_width,stage_height,
this .stage.stageWidth, this .stage.stageHeight,test_width,test_height);
            
if  (loader  !=   null )
            {
                loader.unload();
                loader 
=   null ;
            }
        }
    }
}

loading效果,可以在我前面发的几篇文章中查看,以上效果仅供参考...

你可能感兴趣的:(as3)