Flex implements image playback solution 2: pre-load temporary 3 images into memory

Source: Internet
Author: User

This scheme is pre-loaded: the first, current, and next image, a total of three images are put into the memory. In this way, the memory consumption can be very small, and the Loaded Images will release the memory.

The following example uses the imageplayers class and the index. mxml class.

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 implements image playback solution 2: pre-load temporary 3 images into memory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.