效果:
转载请注明:http://www.cnblogs.com/TheViper/
主要是flash组件的tilelist,这个很有用。还有对flash组件源码的一点修改hack。
还是代码很简单,很不规范
package
{
import flash.display.*;
import flash.events.*;
import fl.data.DataProvider;
import fl.controls.TileList;
import flash.events.MouseEvent;
import fl.events.*;
import flash.net.*;
import flash.ui.*;
import fl.controls.listClasses.*;
import flash.external.*;
import fl.controls.ScrollBarDirection;
import fl.containers.*;
import flash.utils.Timer; public class PhotoUpload extends MovieClip
{
private var myTileList;
private var fileRef1:FileReference=new FileReference();
private var loader1:Loader;
private var fileRefList:CustomFileReferenceList;
private var fileList:Array = new Array ;
private var uploadlist:Array = new Array ;
//private var itemsArray:Array = new Array();
private var ori_index;
private var target_index;
private var hover_index;
private var max_x;
private var max_y;
private var url:String;
private var urlrequest:URLRequest = new URLRequest(url);
private var imgSizeList:Array = new Array ;
private var myTimer:Timer = new Timer(10);
private var myTimer1:Timer = new Timer(10); public function PhotoUpload()
{
myTimer.addEventListener("timer", timerHandler);
myTimer1.addEventListener("timer", timerHandler1);
myTileList = new TileList();
myTileList.direction = ScrollBarDirection.VERTICAL;
myTileList.columnWidth = 150;
myTileList.rowHeight = 150;
myTileList.rowCount = 6;
myTileList.move(14,14);
myTileList.setSize(990,660);
stage.addChildAt(myTileList,0);
myTileList.addEventListener(ListEvent.ITEM_ROLL_OVER, onItemHover);
wrap_control.addEventListener(MouseEvent.ROLL_OUT, onItemOut);
myTileList.addEventListener(ListEvent.ITEM_CLICK, onItemClick);
myTileList.addEventListener(MouseEvent.MOUSE_DOWN, onItemPress);
myTileList.addEventListener(MouseEvent.MOUSE_UP, onItemRelease);
boot.select_btn.addEventListener(MouseEvent.CLICK,selectFilesHandler1);
bottom_area.add_image1.addEventListener(MouseEvent.CLICK,selectFilesHandler1);
wrap_control.control.item_delete.addEventListener(MouseEvent.CLICK,deleteHandler);
bottom_area.upload_btn.addEventListener(MouseEvent.MOUSE_DOWN,function(e:Event){
do_upload(e,0);
});
bottom_area.visible = false;
}
public function timerHandler(event:TimerEvent):void
{
myTileList.verticalScrollPosition = myTileList.verticalScrollPosition + 5;
onItemMove(new MouseEvent(MouseEvent.MOUSE_MOVE));
}
public function timerHandler1(event:TimerEvent):void
{
myTileList.verticalScrollPosition = myTileList.verticalScrollPosition - 5;
onItemMove(new MouseEvent(MouseEvent.MOUSE_MOVE));
}
function selectFilesHandler1(event:MouseEvent):void
{
fileRefList = new CustomFileReferenceList ;
fileRefList.browse(getFilterTypes());
fileRefList.addEventListener(CustomFileReferenceList.SELECT_COMPLETE,selectHandler);
}
function getImagesFilter():FileFilter
{
return new FileFilter("images(*.jpg, *.gif, *.png, *.jpg)", "*.jpg;*.jpeg;*.gif;*.png");
}
function getFilterTypes():Array
{
return [getImagesFilter()];
}
function selectHandler(evt:Event):void
{
//if (uploadlist.length > 0)
//{
//itemsArray.pop();
//}
var fileRefList:CustomFileReferenceList = evt.target as CustomFileReferenceList;
fileRefList.removeEventListener(CustomFileReferenceList.SELECT_COMPLETE,selectHandler);
fileList = fileRefList.getSelectedFiles();
//var len:Number = uploadlist.length + fileList.length;
if (fileList.length > 0)
{
if ((uploadlist.length > 100))
{ }
else
{
if (myTileList.length > 0)
{
myTileList.removeItemAt(myTileList.length-1);
}
a(0,fileList);
}
}
}
function a(idx:Number,fileList:Array):void
{
var fileRef:FileReference = fileList[idx] as FileReference;
loadImage(fileRef,idx);
fileRef.load();
} function loadImage(fileRef:FileReference,idx:Number):void
{
fileRef.addEventListener(Event.COMPLETE,function()
{
var loader:Loader=new Loader();
resizeImage(loader, idx, fileRef);
loader.loadBytes(fileRef.data);
fileRef.removeEventListener(Event.COMPLETE, arguments.callee);
});
}
function resizeImage(loader:Loader,idx:Number,fileRef:FileReference):void
{
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event)
{
xmj(loader, idx, fileRef);
});
}
function xmj(loader:Loader,idx:Number,fileRef:FileReference)
{
var w:Number = loader.width;
var h:Number = loader.height;
var obj:Object = {w:w,h:h};
imgSizeList.push(obj);
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,arguments.callee);
var wrap_item:Sprite=new Sprite();
wrap_item.addChild(loader);
myTileList.addItem({source:wrap_item});
//itemsArray.push( {source:wrap_item} );
if ((idx < fileList.length - 1))
{
idx++;
if ((idx+uploadlist.length> 100))
{
//myTileList.addItem({source:add_image});
myTileList.addItem({source:add_image});
uploadlist = uploadlist.concat(fileList.slice(0,11));
trace("fffsafa");
}
else
{
//uploadlist = uploadlist.concat(fileList.slice(idx-1,idx));
bottom_area.visible = true;
boot.visible = false;
a(idx,fileList);
}
}
else
{
//itemsArray.push( {source:add_image} );
//var dp:DataProvider = new DataProvider(itemsArray);
//myTileList.dataProvider = dp;
//stage.addChildAt(myTileList,0);
myTileList.addItem({source:add_image});
trace(fileList.length);
if (fileList.length > 100)
{
uploadlist = uploadlist.concat(fileList.slice(0,9));
}
else
{
uploadlist = uploadlist.concat(fileList);
}
}
}
function onItemClick(event :ListEvent):void
{
if (event.index == uploadlist.length)
{
selectFilesHandler1(new MouseEvent(MouseEvent.CLICK));
}
}
function onItemHover(event :ListEvent):void
{
trace(event.index+" "+ uploadlist.length);
if (event.index < uploadlist.length)
{
wrap_control.visible = true;
max_x = uploadlist.length % 6;
max_y = Math.floor(uploadlist.length / 6);
var x_index:int = event.index % 6;
var y_index:int = event.index / 6;
if (y_index>max_y)
{
y_index = max_y;
}
if (y_index>=max_y&&x_index>max_x)
{
x_index = max_x;
}
wrap_control.x = 164 * (x_index ) + 14;
wrap_control.y = 164 * y_index + 100 - myTileList.verticalScrollPosition;
hover_index = event.index;
}
}
function deleteHandler(event:MouseEvent):void
{
myTileList.removeItemAt(hover_index);
uploadlist.splice(hover_index, 1);
wrap_control.visible = false;
if (uploadlist.length == 0)
{
bottom_area.visible =false;
boot.visible = true;
myTileList.removeItemAt(0);
}
}
function onItemOut(event :MouseEvent):void
{
wrap_control.visible = false;
}
function onItemPress(event : MouseEvent):void
{
wrap_control.visible = false;
if (event.target is ImageCell)
{
event.target.alpha = 0.2;
ori_index = event.target.listData.index;
var pb = event.target.getChildAt(1).getChildAt(1);
pb.width = 30;
wrap_control.visible = false;
myTileList.removeEventListener(ListEvent.ITEM_ROLL_OVER, onItemHover);
if (ori_index!=uploadlist.length)
{
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE, onItemMove);
}
}
event.stopPropagation();
}
function onItemMove(event : MouseEvent):void
{
max_x = uploadlist.length % 6;
max_y = Math.floor(uploadlist.length / 6);
var x_index:int = myTileList.mouseX / 164;
var y_index:int = (myTileList.mouseY+myTileList.verticalScrollPosition) / 164;
if (y_index>max_y)
{
y_index = max_y;
}
if (y_index>=max_y&&x_index>max_x)
{
x_index = max_x;
}
custom_cursor.visible = true;
insert_flag.visible = true;
custom_cursor.x = stage.mouseX;
custom_cursor.y = stage.mouseY;
target_index = 6 * y_index + x_index;
insert_flag.x = 164 * (x_index) + 7;
insert_flag.y = 164 * y_index + 15 - myTileList.verticalScrollPosition;
if (stage.mouseY > 620)
{
myTimer.start();
}
else
{
myTimer.stop();
}
if (stage.mouseY < 40)
{
myTimer1.start();
}
else
{
myTimer1.stop();
}
}
function onItemRelease(event : MouseEvent):void
{
myTimer.stop();
myTimer1.stop();
event.target.alpha = 1;
//bottom_area.ab.text = "" + event.target;
if (ori_index != target_index && typeof target_index != "undefined" && ori_index != -1 && stage.hasEventListener(MouseEvent.MOUSE_MOVE) && target_index != -1)
{
var proxy = myTileList.getItemAt(ori_index);
myTileList.removeItemAt(ori_index);
if (target_index==uploadlist.length)
{
target_index = target_index - 1;
}
myTileList.addEventListener(ListEvent.ITEM_ROLL_OVER, onItemHover);
myTileList.addItemAt(proxy,target_index);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onItemMove);
Mouse.show();
}
ori_index = -1;
target_index = -1;
insert_flag.visible = false;
custom_cursor.visible = false;
}
private function do_upload(e:Event,i:int):void
{
var f:FileReference = uploadlist[i] as FileReference;
var obj:Object = imgSizeList[i];
try
{
f.upload(new URLRequest(this.url + "&w=" + obj["w"] + "&h=" + obj["h"]));
}
catch (e:Error)
{
trace(e.message);
}
f.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,function(e:DataEvent){
if(i<uploadlist.length-1){
i++;
do_upload(e,i);
}else{
ExternalInterface.call('img_upload_complete_all');
myTileList.removeAll();
uploadlist.length=0;
}
ExternalInterface.call('img_upload_complete',e.data);
});
}
}
}
改过的flash组件 http://files.cnblogs.com/TheViper/fl.zip