• 仿美图秀秀的自由拼图


    效果:

    转载请注明:http://www.cnblogs.com/TheViper

    用了github上的这个 free-transform-manager,美图秀秀用的变形工具是greensocks的TransformManager,这个体积很大,貌似还要给钱。。。

    代码很简单,很不规范

      1 package 
      2 {
      3     import flash.display.*;
      4     import flash.events.*;
      5     import flash.net.*;
      6     import flash.geom.*;
      7     import flash.ui.Mouse;
      8     import com.adobe.images.JPGEncoder;
      9     import flash.utils.ByteArray;
     10     import com.ryan.geom.*;
     11     import flash.external.ExternalInterface;
     12     import flash.text.*;
     13 
     14     public class puzzle extends MovieClip
     15     {
     16         private var fileRef:FileReference;
     17         private var select_img_loader:Loader;
     18         private static const MAX_UPLOAD_NUM:Number = 9;
     19         private var uploadlist:Array=new Array();
     20         private var fileRefList:CustomFileReferenceList;
     21         private var fileList:Array=new Array();
     22         private var Data:ByteArray;
     23         private var ldr:Loader=new Loader();
     24         private var bitmap:Bitmap;
     25         private var centerx:Number;
     26         private var centery:Number;
     27         private var loader:URLLoader;
     28         public var fts:FreeTransformManager;
     29         private var myBitmapData:BitmapData;
     30         private var myBitmapData1:BitmapData;
     31         private var myBitmapData2:BitmapData;
     32         private var myBitmapData3:BitmapData;
     33         private var bmp:Bitmap;
     34         private var bmp1:Bitmap;
     35         private var bmp2:Bitmap;
     36         private var bmp3:Bitmap;
     37         private var update_container:Sprite;
     38         private var delete_sprite:Sprite;
     39         private var update_sprite:Sprite;
     40         private var update_sprite1:Sprite;
     41         private var update_sprite2:Sprite;
     42         private var file:FileReference;
     43         private var extra_img_loader:Loader;
     44         private var cur_target:Sprite;
     45 
     46         public function puzzle()
     47         {
     48             fts = new FreeTransformManager();
     49             fts.boundingBoxOutlineThickness = 1;
     50             fts.handleRadius = 5;
     51             fts.handleOutlineThickness = 0.5;
     52             update_container=new Sprite();
     53             draw_delete_option();
     54             draw_update_option();
     55             draw_update_item();
     56             delete_sprite.addEventListener(MouseEvent.CLICK,delete_txt_handler);
     57             update_container.getChildAt(1).addEventListener(MouseEvent.CLICK,update_txt_handler);
     58             update_container.addEventListener(MouseEvent.MOUSE_OVER,update_hover_handler);
     59             left.addEventListener(MouseEvent.MOUSE_MOVE,again_handler);
     60             wrap_small_img.addEventListener(MouseEvent.CLICK,left_click_handler);
     61             wrap_small_img.buttonMode = true;
     62             right.right_bottom.add_img.addEventListener(MouseEvent.CLICK,selectFilesHandler);
     63             right.right_bottom.upload_btn.addEventListener(MouseEvent.CLICK,upload_handler);
     64             this.setChildIndex(left, 1);
     65             load_basic_img("1_1");
     66             right.right_bottom.upload_btn.visible = false;
     67         }
     68         private function update_out_handler(event:MouseEvent):void
     69         {
     70             update_sprite1.visible = false;
     71             update_sprite2.visible = false;
     72         }
     73         private function update_hover_handler(event:MouseEvent):void
     74         {
     75             update_sprite1.visible = true;
     76             update_sprite2.visible = true;
     77         }
     78         private function update_item_over(event:MouseEvent):void
     79         {
     80             event.target.getChildAt(0).opaqueBackground = 0xFFFFFF;
     81         }
     82         private function update_item_out(event:MouseEvent):void
     83         {
     84             event.target.getChildAt(0).opaqueBackground = 0x000000;
     85         }
     86         //绘制更换图片
     87         private function draw_update_item()
     88         {
     89             update_sprite1=new Sprite();
     90             myBitmapData2 = new BitmapData(48,16,true,0x000000);
     91             myBitmapData2.draw(local_update);
     92             bmp2 = new Bitmap(myBitmapData2);
     93             bmp2.opaqueBackground = 0x000000;
     94             bmp2.x = bmp.width + 12;
     95             bmp2.y = bmp.height;
     96             update_sprite1.addChild(bmp2);
     97             update_sprite1.buttonMode = true;
     98             update_container.addChild(update_sprite1);
     99             update_sprite2=new Sprite();
    100             myBitmapData3 = new BitmapData(48,16,true,0x000000);
    101             myBitmapData3.draw(album_update);
    102             bmp3 = new Bitmap(myBitmapData3);
    103             bmp3.opaqueBackground = 0x000000;
    104             bmp3.x = bmp.width + 12;
    105             bmp3.y = 32;
    106             update_sprite2.addChild(bmp3);
    107             update_sprite2.buttonMode = true;
    108             update_container.addChild(update_sprite2);
    109             update_sprite1.visible = false;
    110             update_sprite2.visible = false;
    111             update_sprite1.addEventListener(MouseEvent.MOUSE_OVER,update_item_over);
    112             update_sprite1.addEventListener(MouseEvent.MOUSE_OUT,update_item_out);
    113             update_sprite2.addEventListener(MouseEvent.MOUSE_OVER,update_item_over);
    114             update_sprite2.addEventListener(MouseEvent.MOUSE_OUT,update_item_out);
    115             fts.addChild(update_container);
    116             //trace(update_container.numChildren);
    117         }
    118         private function draw_delete_option()
    119         {
    120             delete_sprite=new Sprite();
    121             myBitmapData = new BitmapData(24,16,true,0x000000);
    122             bmp = new Bitmap(myBitmapData);
    123             bmp.x = 10;
    124             myBitmapData.draw(delete_txt);
    125             delete_sprite.addChild(bmp);
    126             delete_sprite.buttonMode = true;
    127             fts.addChild(delete_sprite);
    128         }
    129         private function draw_update_option()
    130         {
    131             update_sprite=new Sprite();
    132             myBitmapData1 = new BitmapData(48,16,true,0x000000);
    133             myBitmapData1.draw(update_txt);
    134             bmp1 = new Bitmap(myBitmapData1);
    135             bmp1.x = bmp.width + 12;
    136             update_sprite.addChild(bmp1);
    137             update_sprite.buttonMode = true;
    138             update_container.addChild(update_sprite);
    139         }
    140         //变形事件回调
    141         private function onTransform(e:FreeTransformEvent)
    142         {
    143             update_sprite2.x = update_sprite1.x = update_sprite.x = delete_sprite.x = e.targetObject.x;
    144             update_sprite2.y = update_sprite1.y = update_sprite.y = delete_sprite.y = e.targetObject.y;
    145             update_sprite2.rotation = update_sprite1.rotation = update_sprite.rotation = delete_sprite.rotation = e.targetObject.rotation;
    146         }
    147         private function delete_txt_handler(event:MouseEvent):void
    148         {
    149             var target:Sprite = event.target.parent.getDispObj();
    150             right.load_img.removeChild(target);
    151             fts.visible = false;
    152         }
    153         private function update_txt_handler(event:MouseEvent):void
    154         {
    155             var target:Sprite = event.target.parent.parent.getDispObj();
    156             cur_target = target;
    157             file= new FileReference();
    158             file.browse(getFilterTypes());
    159             file.addEventListener(Event.SELECT, selectHandler1);
    160         }
    161         private function selectHandler1(evt:Event):void
    162         {
    163             file.load();
    164             file.addEventListener(Event.COMPLETE,onComplete);
    165             file.removeEventListener(Event.SELECT,selectHandler1);
    166         }
    167         private function onComplete(e:Event):void
    168         {
    169             extra_img_loader=new Loader();
    170             file.removeEventListener(Event.COMPLETE,onComplete);
    171             extra_img_loader.loadBytes(file.data);
    172             extra_img_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);
    173         }
    174         private function onLoadComplete(e:Event):void
    175         {
    176             var bitmap:Bitmap = e.target.content;
    177             scaleInBox(bitmap,150,150);
    178             cur_target.removeChildAt(0);
    179             cur_target.addChild(bitmap);
    180             fts.updateAfterChange();
    181             extra_img_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoadComplete);
    182         }
    183         private function upload_handler(event:MouseEvent):void
    184         {
    185             fts.visible = false;
    186             var bitmapData:BitmapData = new BitmapData(555,420);
    187             bitmapData.draw(right.load_img);
    188             var _encoder:JPGEncoder = new JPGEncoder(100);
    189             bitmapData.draw(bitmapData);
    190             Data = _encoder.encode(bitmapData);
    191             var req:URLRequest = new URLRequest("http://localhost:8888/image/flash_puzzle_upload?file_name=mix.jpeg");
    192             req.data = Data;
    193             req.method = URLRequestMethod.POST;
    194             req.contentType = "application/octet-stream";
    195             loader = new URLLoader();
    196             loader.dataFormat = URLLoaderDataFormat.BINARY;
    197             loader.load(req);
    198             loader.addEventListener(Event.COMPLETE, upload_complete);
    199         }
    200         public function scaleBitmapData(bmpData:BitmapData, scaleX:Number, scaleY:Number):BitmapData
    201         {
    202             var matrix:Matrix = new Matrix();
    203             matrix.scale(scaleX, scaleY);
    204             var bmpData_:BitmapData = new BitmapData(scaleX * (bmpData.width), scaleY * (bmpData.height), true, 0);
    205             bmpData_.draw(bmpData, matrix,null,null,null,true);
    206             return bmpData_;
    207         }
    208         private function upload_complete(event:Event):void
    209         {
    210             var loader:URLLoader = URLLoader(event.target);
    211             var vars:URLVariables = new URLVariables(loader.data);
    212             ExternalInterface.call('img_upload_complete',vars.src);
    213             ExternalInterface.call('img_upload_complete_all');
    214         }
    215         private function left_click_handler(event:MouseEvent):void
    216         {
    217             var basic_img:Loader = right.load_img.getChildByName("basic_img") as Loader;
    218             if (basic_img!=null)
    219             {
    220                 right.load_img.removeChild(basic_img);
    221             }
    222             var a:int = event.stageX / 90 + 1;
    223             var b:int = event.stageY / 100 + 1;
    224             load_basic_img(a+"_"+b);
    225         }
    226         private function again_handler(event:MouseEvent):void
    227         {
    228             var a:int = event.localX / 85;
    229             var b:int = event.localY / 85;
    230             if (a<2&&b<6)
    231             {
    232                 wrap_small_img.x=(a==0?5:a*90);
    233                 wrap_small_img.y=(b==0?5:b*97);
    234             }
    235         }
    236         private function getImagesFilter():FileFilter
    237         {
    238             return new FileFilter("images(*.jpg, *.gif, *.png, *.jpg)", "*.jpg;*.jpeg;*.gif;*.png");
    239         }
    240         private function getFilterTypes():Array
    241         {
    242             return [getImagesFilter()];
    243         }
    244         private function add_img_handler(event:MouseEvent):void
    245         {
    246             this.fileRef=new FileReference();
    247             fileRef.browse(getFilterTypes());
    248             fileRef.addEventListener(Event.SELECT, selectHandler);
    249         }
    250         private function selectFilesHandler(event:Event):void
    251         {
    252             fileRefList=new CustomFileReferenceList();
    253             fileRefList.browse(getFilterTypes());
    254             fileRefList.addEventListener(CustomFileReferenceList.SELECT_COMPLETE, selectHandler);
    255         }
    256         private function selectHandler(evt:Event):void
    257         {
    258             var fileRefList:CustomFileReferenceList = evt.target as CustomFileReferenceList;
    259             fileRefList.removeEventListener(CustomFileReferenceList.SELECT_COMPLETE, selectHandler);
    260             fileList = fileRefList.getSelectedFiles();
    261             var len:Number = uploadlist.length + fileList.length;
    262             if (fileList.length > 0)
    263             {
    264                 if (len > MAX_UPLOAD_NUM)
    265                 {
    266                     right.right_bottom.tips.text = "最多只能添加9张图片哦";
    267                 }
    268                 else
    269                 {
    270                     a(0, fileList);
    271                 }
    272             }
    273         }
    274         private function a(idx:Number, fileList:Array):void
    275         {
    276             var fileRef:FileReference = fileList[idx] as FileReference;
    277             loadImage(fileRef, idx);
    278             fileRef.load();
    279         }
    280         //依次载入本地图片
    281         private function loadImage(fileRef:FileReference, idx:Number):void
    282         {
    283             fileRef.addEventListener(Event.COMPLETE, function()
    284             {
    285             var loader:Loader=new Loader();
    286             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event)
    287             {
    288             var _bitmap:Bitmap = e.target.content as Bitmap;
    289             scaleInBox(_bitmap,150,150);
    290             var rotation:Number=180*Math.random();
    291             this.bitmap=_bitmap;
    292             var s:Sprite=new Sprite();
    293             fts.registerSprite(s,{minScale:0.7});
    294             s.x=620*Math.random();
    295             s.y=450*Math.random();
    296             s.rotation=rotation;
    297             s.addChild(_bitmap);
    298             fts.addEventListener(FreeTransformEvent.ON_TRANSFORM, onTransform);
    299             update_container.addEventListener(MouseEvent.MOUSE_OUT,update_out_handler);
    300             right.load_img.addChild(s);
    301             if (idx < fileList.length - 1)
    302             {
    303             idx++;
    304             a(idx, fileList);
    305             }else{
    306             right.right_bottom.upload_btn.visible=true;
    307             right.right_bottom.tips.text="";
    308             uploadlist = uploadlist.concat(fileList);
    309             }
    310             });
    311             loader.loadBytes(fileRef.data);
    312             fileRef.removeEventListener(Event.COMPLETE, arguments.callee);
    313             });
    314         }
    315         //加载第一张背景图片
    316         private function load_basic_img(i:String):void
    317         {
    318             //var url:String = "http://localhost:8080/facebook/img/puzzle/"+i+".jpg";
    319             var url:String = "http://localhost/twitter/images/puzzle/big_img/" + i + ".jpg";
    320             var urlReq:URLRequest = new URLRequest(url);
    321             ldr.load(urlReq);
    322             ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, function()
    323             {
    324             ldr.scaleX=0.54;
    325             ldr.scaleY=0.55;
    326             ldr.name='basic_img';
    327             right.load_img.addChild(ldr);
    328             right.load_img.setChildIndex(ldr,0);
    329             });
    330         }
    331         //根据盒子尺寸等比例缩放
    332         private function scaleInBox(target:DisplayObject, boxWidth:Number, boxHeight:Number):void
    333         {
    334             var scale:Number = Math.min(boxWidth / target.width, boxHeight / target.height);
    335             target.scaleX = scale;
    336             target.scaleY = scale;
    337         }
    338     }
    339 }
  • 相关阅读:
    C++ 在dynamic_cast&lt;&gt;用法
    Solr入门指南
    Android学习4、Android该Adapter
    在这些形式的验证码
    智课雅思词汇---四、clos和cap和ced是什么意思
    js插件---强大的图片裁剪Cropper
    Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported
    js插件---图片裁剪photoClip
    base64格式的图片数据如何转成图片
    smarty课程---最最最简单的smarty例子
  • 原文地址:https://www.cnblogs.com/TheViper/p/4059697.html
Copyright © 2020-2023  润新知