スポンサーサイト

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

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

[ActionScript 3.0] Flickrから取得した画像をiTunesのカバーアート風に表示

2007-09-05 | 21:10

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

CoverArt.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="450", height="250", backgroundColor="#ffffff")]
    public class CoverArt 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;

        public function CoverArt():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" && topIndex < bitmaps.length - 1){
                topIndex++;
                mode = 0;
                startAnimation();
            }else if(button.name == "left" && topIndex > 0){
                topIndex--;
                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);
            }
        }

        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);
            if(bitmaps.length == 11)
                startAnimation();
        }

        private function onAnimeTimer(e:TimerEvent):void{
            var r:Number = Math.PI/(180/70);
            var num:int = 0;
            for(var i:int = topIndex ; i < Math.min(topIndex+11, bitmaps.length) ; i++,num++){
                var bm:ThreeDBitmap = bitmaps[i];
                bm.alpha = 1;
                bm.y = 100;
                var targetX:int = (num <= 5) ? num*35 : num*35 + 50;
                var diff:int;
                if(mode == 0){
                    diff = (num == 5)
                        ? 85 * (animeFrame - animeIndex) / animeFrame
                        : bm.width*Math.cos(r) * (animeFrame - animeIndex) / animeFrame;
                    bm.x = targetX + diff;
                    if(num<4)
                        bm.rotation = r;
                    else if(num == 4)
                        bm.rotation = r / animeFrame * animeIndex;
                    else if(num == 5)
                        bm.rotation = -r + r / animeFrame * animeIndex;
                    else if(num > 5)
                        bm.rotation = -r;
                }else{
                    diff = (num == 6)
                        ? 85 * (animeFrame - animeIndex) / animeFrame
                        : bm.width*Math.cos(r) * (animeFrame - animeIndex) / animeFrame;
                    bm.x = targetX - diff;
                    if(num<5)
                        bm.rotation = r;
                    else if(num == 5)
                        bm.rotation = r - r / animeFrame * animeIndex;
                    else if(num == 6)
                        bm.rotation = -r / animeFrame * animeIndex;
                    else if(num > 6)
                        bm.rotation = -r;
                }
            }
            if(++animeIndex > animeFrame){
                Timer(e.target).removeEventListener(TimerEvent.TIMER, onAnimeTimer);

                for (var j:int = 0 ; j < bitmaps.length ; j++){
                    // ローテーションさせたままだとリソースを浪費するので、ローテーションさせる必要のない
                    // ビットマップは元に戻す
                    if(j < topIndex || j > topIndex+11){
                        bitmaps[j].alpha = 0;
                        bitmaps[j].x = -100;
                        bitmaps[j].y = -100;
                        bitmaps[j].rotation = 0;
                    }
                }
            }
        }
    }
}
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ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。