码迷,mamicode.com
首页 > 其他好文 > 详细

flex 实现图片播放 方案二 把临时3张图片预加载放入内存

时间:2014-10-10 13:26:44      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

该方案,是预加载:前一张,当前,下一张图片,一共3张图片放入内存中。这样对内存的消耗可以非常小,加载之后的图片就释放内存。

下面示例一个是类ImagePlayers,一个是index.mxml

package
{
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.net.URLRequest;
    import flash.utils.Timer;
    
    import mx.collections.ArrayCollection;
    
    import spark.components.Image;

    public class ImagePlayers
    {
        private var bitmapDataArrPre:ArrayCollection=new ArrayCollection();
        private var bitmapDataArrPla:ArrayCollection=new ArrayCollection();
        private var bitmapDataArrNex:ArrayCollection=new ArrayCollection();
        
        
        private var playerxh:int=0;
        private var playTimer:Timer;
        
        public var UrlArr:Array=[];
        public  var show:Image;
        public  var play:Image;
        
        public function ImagePlayers()
        {
            playTimer=new Timer(Number(500));
            playTimer.addEventListener(TimerEvent.TIMER, function(evt:TimerEvent):void
            {
                if (playerxh < (UrlArr.length-1))
                {
                    nextf();
                }
                else
                {
                    playTimer.stop();
                    
                    play.toolTip="播放"
                    play.source="assets/images/play/play.png";
                }
            });
        }
        
        public function start():void
        {
            imgLoadPla(UrlArr[0].url);
        }
        
        private function imgLoadPre(url:String):void
        {
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoadedPre);
            loader.load(new URLRequest(url));
        }
        private function imgLoadedPre(e:Event):void
        {
            var _bitmapData:BitmapData = new BitmapData(e.target.width,e.target.height,false);
            _bitmapData.draw(e.target.content);
            
            bitmapDataArrPre.removeAll();
            bitmapDataArrPre.addItem(_bitmapData);
        }
        private function imgLoadPla(url:String):void
        {
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoadedPla);
            loader.load(new URLRequest(url));
        }
        private function imgLoadedPla(e:Event):void
        {
            var _bitmapData:BitmapData = new BitmapData(e.target.width,e.target.height,false);
            _bitmapData.draw(e.target.content);
            
            bitmapDataArrPla.removeAll();
            bitmapDataArrPla.addItem(_bitmapData);
            
            if(playerxh==0)
            {
                show.source=bitmapDataArrPla[0];
                imgLoadNex(UrlArr[1].url);
            }
        }
        private function imgLoadNex(url:String):void
        {
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoadedNex);
            loader.load(new URLRequest(url));
        }
        private function imgLoadedNex(e:Event):void
        {
            var _bitmapData:BitmapData = new BitmapData(e.target.width,e.target.height,false);
            _bitmapData.draw(e.target.content);
            
            bitmapDataArrNex.removeAll();
            bitmapDataArrNex.addItem(_bitmapData);
            
        }
        //上一张
        public function pref():void
        {
            if(playerxh>0)
            {
                bitmapDataArrNex.removeAll();
                bitmapDataArrNex.addItem(bitmapDataArrPla[0]);
                
                bitmapDataArrPla.removeAll();
                bitmapDataArrPla.addItem(bitmapDataArrPre[0]);
                
                playerxh--;
                if(playerxh!=0)
                {
                    imgLoadPre(UrlArr[playerxh-1].url);
                }
                
                
                show.source=bitmapDataArrPla[0];
            }
            
        }
        //下一张
        public function nextf():void
        {
            if(playerxh<(UrlArr.length-1))
            {
                bitmapDataArrPre.removeAll();
                bitmapDataArrPre.addItem(bitmapDataArrPla[0]);
                
                bitmapDataArrPla.removeAll();
                bitmapDataArrPla.addItem(bitmapDataArrNex[0]);
                
                playerxh++;
                if(playerxh!=(UrlArr.length-1))
                {
                    imgLoadNex(UrlArr[playerxh+1].url);
                }
                
                show.source=bitmapDataArrPla[0];
            }
        }
        //播放
        public function playf(delay:Number=500):void
        {
            if(play.toolTip=="播放")
            {
                play.toolTip="暂停"
                play.source="assets/images/play/pause.png";
                if(delay!=playTimer.delay)
                {
                    playTimer.delay=delay;
                }
                playTimer.start();
            }
            else if(play.toolTip=="暂停")
            {
                play.toolTip="播放"
                play.source="assets/images/play/play.png";
                playTimer.stop();
            }
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" 
                creationComplete="application1_creationCompleteHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            
            private var player:ImagePlayers;
            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                player=new ImagePlayers();
                player.show=show;
                player.play=play;
                
                for(var i:int=5;i<=15;i++)
                {
                    player.UrlArr.push({url:"http://192.168.2.9/png/data/pm25/"+Strings(i)+".gif"});
                }
                
                player.start();
            }
            private function Strings(i:int):String
            {
                if(i<10)
                {
                    return "00"+i.toString();
                }
                if(i<100)
                {
                    return "0"+i.toString();
                }
                return "";
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
    </fx:Declarations>
    <s:VGroup>
        <s:HGroup>
            <s:Image id="play" source="assets/images/play/play.png" toolTip="播放" click="player.playf()" buttonMode="true" useHandCursor="true" />
            <s:Image source="assets/images/play/pre.png" click="player.pref()"  toolTip="上一个" buttonMode="true" useHandCursor="true" />
            <s:Image source="assets/images/play/next.png" click="player.nextf()"  toolTip="下一个" buttonMode="true" useHandCursor="true" />
        </s:HGroup>
        <s:Image id="show"  fillMode="scale" scaleMode="letterbox" smooth="true" smoothingQuality="high"
                 width="1000" height="800" />
    </s:VGroup>
</s:Application>

 

flex 实现图片播放 方案二 把临时3张图片预加载放入内存

标签:style   blog   http   color   io   os   ar   for   sp   

原文地址:http://www.cnblogs.com/tiandi/p/4015481.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!