Flex【原创】移动端预加载图片取代splashScreenImage属性

2019/7/23 10:03:21 人评论 次浏览 分类:学习教程

原文链接:http://www.cnblogs.com/loveFlex/archive/2012/07/16/2593476.html

Flex移动端主程序属性 splashScreenImage : 可以设置预加载图片。

可是他只支持MXML,不支持ActionScript编写

提供解决方案处理此问题。

环境 :Flex 4.6.0 (必须,如果为4.5的话,当旋转屏幕时会报错)

 

 

1:ImagePreloader预加载类

package  
{
    
    import flash.display.Bitmap;
    import flash.display.DisplayObject;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.geom.Matrix;
    import flash.net.URLRequest;
    import flash.utils.getTimer;
    
    import mx.events.FlexEvent;
    import mx.preloaders.IPreloaderDisplay;
    import mx.preloaders.Preloader;
    
    /**
     *<p> Image Preloader</p>
     * replace the property splashScreenImage of Application,
     * it can be dynamic changed by splashImageUrl and suppport ActionScript
     * @author yaoyf
     * @date 2012-07-02
     * */
    public class ImagePreloader extends Sprite implements IPreloaderDisplay
    {
        private const LETTERBOX:String = "letterbox";
        private const ZOOM:String = "zoom";
        private const STRETCH:String = "stretch";
        
        private var splashImage:DisplayObject;     
        private var splashImageWidth:Number;        
        private var splashImageHeight:Number;       
        private var SplashImageClass:Class = null;  
        private var loader:Loader = null;
        private var scaleMode:String = LETTERBOX;      
        private var minimumDisplayTime:Number = 3000;   
        private var checkWaitTime:Boolean = false;      
        private var displayTimeStart:int = -1;        
        private var _stageHeight:Number;
        private var _stageWidth:Number;
        
        private var splashImageUrl:String = null;
        
        public function ImagePreloader()
        {
            super();
        }
        
        /**
         * initialize
         * */
        public function initialize():void
        {
            splashImageUrl = "preloader.jpg";//your image url
            
            checkWaitTime = minimumDisplayTime > 0;
            if (checkWaitTime)
                if(displayTimeStart == -1)
                    this.displayTimeStart = flash.utils.getTimer();
            
            loader = new Loader();
            loader.load(new URLRequest(splashImageUrl));
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onIoEvent);
            
            this.stage.addEventListener(Event.RESIZE, stageResizeHandler, false , 0, true );
        }
        
        private function stageResizeHandler(event:Event):void
        {
            scaleMatrix();
        }
        
        protected function onIoEvent(event:IOErrorEvent):void{trace("IOError has occured : " + event.text);}
        
        protected function onComplete(event:Event):void
        {
            dispose();
            
            splashImage = Bitmap(event.target.content);
            addChildAt(splashImage, 0);
            
            this.splashImageWidth = splashImage.width;
            this.splashImageHeight = splashImage.height;
            
            scaleMatrix();
        }
        
        
        /**
         *  scale matrix of splashImage
         * */
        private function scaleMatrix():void
        {
            if (!splashImage)
                return;
            
            var dpiScale:Number = this.root.scaleX;
            var stageWidth:Number = stage.stageWidth / dpiScale;
            var stageHeight:Number = stage.stageHeight / dpiScale;
            var width:Number = splashImageWidth;
            var height:Number = splashImageHeight;
            var m:Matrix = new Matrix();
            var scaleX:Number = 1;
            var scaleY:Number = 1;
            
            switch(scaleMode)
            {
                case ZOOM:
                    scaleX = Math.max( stageWidth / width, stageHeight / height);
                    scaleY = scaleX;
                    break;
                case LETTERBOX:
                    scaleX = Math.min( stageWidth / width, stageHeight / height);
                    scaleY = scaleX;
                    break;
                case STRETCH:
                    scaleX = stageWidth / width;
                    scaleY = stageHeight / height;
                    break;
            }
            
            if (scaleX != 1 || scaleY != 0)
            {
                width *= scaleX;
                height *= scaleY;
                m.scale(scaleX, scaleY);
            }
            
            m.translate(-width / 2, -height / 2);
            m.translate(stageWidth / 2, stageHeight / 2);
            
            splashImage.transform.matrix = m;
        }
        
        /**
         * get current display time
         * */
        private function get currentDisplayTime():int
        {
            if (-1 == displayTimeStart)
                return -1;
            
            return flash.utils.getTimer() - displayTimeStart;
        }
        
        //implements
        public function set preloader(obj:Sprite):void
        {
            obj.addEventListener(FlexEvent.INIT_COMPLETE, preloader_initCompleteHandler, false, 0, true);
        }
        
        private function preloader_initCompleteHandler(event:Event):void
        {
            if (checkWaitTime && currentDisplayTime < minimumDisplayTime)
                this.addEventListener(Event.ENTER_FRAME, initCompleteEnterFrameHandler);
            else        
                dispatchComplete();
        }
        
        private function initCompleteEnterFrameHandler(event:Event):void
        {
            if (currentDisplayTime <= minimumDisplayTime)return;
            dispatchComplete();
        }
        
        private function dispatchComplete():void
        {
            dispose();
            
            var preloader:Preloader = this.parent as Preloader;
            preloader.removeEventListener(FlexEvent.INIT_COMPLETE, preloader_initCompleteHandler, false);
            this.removeEventListener(Event.ENTER_FRAME, initCompleteEnterFrameHandler);
            
            this.stage.removeEventListener(Event.RESIZE, stageResizeHandler, false);
            
            dispatchEvent(new Event(Event.COMPLETE));
        }
        
        /**
         * dispose resource
         * */
        private function dispose():void
        {
            if(splashImage)
            {
                if(this.contains(splashImage))
                    this.removeChild(splashImage);
                
                var bit:Bitmap = splashImage as Bitmap;
                
                if(bit.bitmapData)
                {
                    bit.bitmapData.dispose();
                    bit.bitmapData = null;
                }
            }
        }
        
        //----------------implements-----------------//
        public function get backgroundAlpha():Number{return 0;}
        public function set backgroundAlpha(value:Number):void{}
        public function get backgroundColor():uint{return 0;}
        public function set backgroundColor(value:uint):void{}
        public function get backgroundImage():Object{return null;}
        public function set backgroundImage(value:Object):void{}
        public function get backgroundSize():String{return null;}
        public function set backgroundSize(value:String):void{}
        public function get stageHeight():Number{return _stageHeight;}
        public function set stageHeight(value:Number):void{_stageHeight = value;}
        public function get stageWidth():Number{return _stageWidth;}
        public function set stageWidth(value:Number):void{_stageWidth = value;}
        
    }
}

 

 

2:调用方式:

  主程序中

<?xml version="1.0" encoding="utf-8"?>
<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                            xmlns:s="library://ns.adobe.com/flex/spark" 
                            firstView="views.Sample_FlexMobilePreloaderView" 
                            applicationDPI="160"
                            preloader="ImagePreloader">
</s:ViewNavigatorApplication>


 

转载于:https://www.cnblogs.com/loveFlex/archive/2012/07/16/2593476.html

相关资讯

    暂无相关的资讯...

共有访客发表了评论 网友评论

验证码: 看不清楚?
    -->