スポンサーサイト

-------- | --:--

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

[ActionScript 3.0] Flickrから取得した画像をカルーセル表示

2007-09-05 | 21:16

入力フィールドに検索タグを入力して、虫眼鏡のアイコンをクリックしてください。
Flickrより画像を取得します。
画像が表示されたら左右の矢印アイコンで画像がカルーセル移動します。

Carousel.as
package {
    import flash.display.*;
    import flash.filters.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
    import flash.utils.*;
    import flash.system.Security;
 
    import com.adobe.webapis.flickr.FlickrService;
    import com.adobe.webapis.flickr.events.FlickrResultEvent;
    import com.adobe.webapis.flickr.Photo;

    [SWF(width="600", height="250", backgroundColor="#ffffff")]
    public class Carousel extends Sprite{

        private var fl:FlickrService;
        private var bitmaps:Array = new Array();
        private var animeIndex:int = 0;
        private var topIndex:int = 0;
        private var tf:TextField;
        //private var debug:TextField;
        private var mode:int;
        private static var animeFrame:int = 10;
        [Embed(source="right.png")]
            private var buttonRight:Class;
        [Embed(source="left.png")]
            private var buttonLeft:Class;
        [Embed(source="glass.gif")]
            private var buttonGlass:Class;

        private var rad:Number = 0;

        public function Carousel():void
        {
            Security.loadPolicyFile("http://farm2.static.flickr.com/crossdomain.xml");

            tf = new TextField();
            tf.border=true;
            tf.height=20;
            tf.text = "apple";
            tf.type = TextFieldType.INPUT;
            tf.x = 210;
            tf.y = 10;
            addChild(tf);

            //debug = new TextField();
            //debug.x = 300;
            //addChild(debug);

            createButton(320,10,"search", new buttonGlass());
            createButton(50,200,"left", new buttonLeft());
            createButton(400,200,"right", new buttonRight());
        }

        private function createButton(x:int, y:int, name:String, b:DisplayObject):void{
            b.filters = [new DropShadowFilter()];
            var button:SimpleButton = new SimpleButton();
            button.x = x;
            button.y = y;
            button.name = name;
            button.upState = b;
            button.overState = button.upState;
            button.hitTestState = button.upState;
            addChild(button);
            button.addEventListener(MouseEvent.MOUSE_DOWN, onMouseClick);
        }

        private function onMouseClick(e:MouseEvent):void{
            var button:SimpleButton = SimpleButton(e.target);
            if(button.name == "search"){
                //debug.text = "search";
                for each(var bm:ThreeDBitmap in bitmaps)
                    removeChild(bm.parent);
                bitmaps = new Array();
                topIndex = 0;

                var apikey:String = ;
                fl = new FlickrService(apikey);

                fl.photos.search("","","any",tf.text);
                // 検索が終わったら onSearch()を呼び出す
                fl.addEventListener(FlickrResultEvent.PHOTOS_SEARCH, onSearch);

            }else if(button.name == "right"){
                mode = 1;
                startAnimation();
            }else if(button.name == "left"){
                mode = -1;
                startAnimation();
            }
        }

        private function startAnimation():void{
            var animeTimer:Timer = new Timer(50);
            animeIndex = 0;
            animeTimer.addEventListener(TimerEvent.TIMER, onAnimeTimer);
            animeTimer.start();
        }

        private function onSearch(event:FlickrResultEvent):void{
            //debug.text = event.data.photos.photos.length;
            fl.removeEventListener(FlickrResultEvent.PHOTOS_SEARCH,onSearch);
            var count:int = 0;
            // 検索結果を走査
            for each (var photo:Photo in event.data.photos.photos)
            {
                // 写真URLを構築
                var url:String = "http://static.flickr.com/" + photo.server + "/" +
                        photo.id + "_" + photo.secret + "_s.jpg";
                // URLをリクエスト
                var req:URLRequest = new URLRequest(url);
                var loader:Loader = new Loader();
                loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onBitmapLoad);
                loader.load(req);

                if(++count > 10)
                    break;
            }
        }

        private function onBitmapLoad( e:Event ):void
        {
            var loader:Loader = LoaderInfo(e.target).loader;
            var bitmap:Bitmap = Bitmap(loader.content);
            var s:Sprite = new Sprite();
            addChild(s);
            var bm:ThreeDBitmap = new ThreeDBitmap(bitmap, s);
            bm.x = -100;
            bm.y = -100;
            bitmaps.push(bm);
            startAnimation();
        }

        private function onAnimeTimer(e:TimerEvent):void{
            rad += Math.PI/5/animeFrame*mode;
            var index:int = 0;
            for each(var bm:ThreeDBitmap in bitmaps){
                var thisRad:Number = Math.PI/5 * index + rad;
                thisRad %= Math.PI*2;
                if(thisRad > Math.PI/2 && thisRad < Math.PI*3/2){
                    bm.x = 300 + 200*Math.sin(thisRad);
                    setChildIndex(bm.parent,0);
                    bm.alpha = 0;
                }
                else{
                    bm.x = 300 + 200*Math.sin(thisRad);
                    setChildIndex(bm.parent,bitmaps.length-1);
                    bm.alpha = 1;
                }
                bm.rotation = thisRad;
                bm.y = 100;
                index++;
            }
            if(++animeIndex > animeFrame){
                Timer(e.target).removeEventListener(TimerEvent.TIMER, onAnimeTimer);
            }
        }
    }
}
ThreeDBitmap.as
package{
    import flash.display.*;
    import flash.geom.*;
    import flash.system.Security;
    public class ThreeDBitmap{

        private var bitmapSlices:Array = new Array();
        private var _x:int=0;
        private var _y:int=0;
        private var _rotation:Number=0;
        private var _parent:Sprite;

        public function ThreeDBitmap(bm:Bitmap, parent:Sprite){

            for(var i:int =0; i < bm.width; i++){
                var slicedBitmapData:BitmapData = new BitmapData(1,bm.height,true,0x00000000);

                var rect:Rectangle = new Rectangle(i,0,1,bm.height);
                slicedBitmapData.copyPixels(bm.bitmapData,rect,new Point(0,0));	
                var slicedBitmap:Bitmap = new Bitmap(slicedBitmapData);
                bitmapSlices.push(slicedBitmap);

                parent.addChild(slicedBitmap);
                _parent = parent;

                slicedBitmap.x = i;
                slicedBitmap.y = 0;
            }
        }

        public function set x(v:int):void{
            _x = v;
            redraw();
        }

        public function set y(v:int):void{
            _y = v;
            redraw();
        }

        public function set rotation(v:Number):void{
            _rotation = v;
            redraw();
        }

        public function set alpha(v:Number):void{
            for each(var bm:Bitmap in bitmapSlices){
                bm.alpha = v;
            }
        }

        public function redraw():void{
            var i:int = 0;
            for each(var bm:Bitmap in bitmapSlices){
                bm.x = _x + i*Math.cos(_rotation);
                if(i > bitmapSlices.length*2/5 && i < bitmapSlices.length*3/5)
                    bm.scaleX = 1.01;
                var j:Number = i - bitmapSlices.length / 2;
                var t:Number = (j*j/2)*(1/(2*j) - 1/(2*j+(j/2)*Math.sin(_rotation)));
                bm.y = _y + t;
                bm.scaleY = (bm.height - 2*t) / bm.height;
                i++;
            }
        }

        public function get width():int{
            return bitmapSlices.length;
        }

        public function get parent():Sprite{
            return _parent;
        }

        public function get x():int{
            return _x;
        }
    }
}

スポンサーサイト

Comment

Post a comment

Secret

上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。