效果演示
主要源码如下
package com.topyunp
{
import com.topyunp.fonts.GlobalFonts;
import feathers.controls.Application;
import feathers.controls.Button;
import feathers.controls.LayoutGroup;
import feathers.layout.VerticalLayout;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
[SWF(width="550", height="480")]
public class LoadLocalPhoto extends Application
{
private var imageContainer:Sprite;
public function LoadLocalPhoto()
{
if (stage)
{
boot(null);
}
else
{
addEventListener(Event.ADDED_TO_STAGE, boot);
}
}
private function boot(e:*):void
{
haxe.initSwc(null);
buildUI();
}
private function buildUI():void
{
var container:LayoutGroup = new LayoutGroup();
addChild(container);
var layout:VerticalLayout = new VerticalLayout();
layout = new VerticalLayout();
layout.setPadding(10);
layout.set_gap(5);
container.layout = layout;
var btn:Button = new Button("浏览本地图片", browseToLoadLocalImage);
btn.embedFonts = true;
btn.textFormat = GlobalFonts.DEFAULT_TEXT_FORMAT;
container.addChild(btn);
imageContainer = new Sprite();
container.addChild(imageContainer);
}
private function browseToLoadLocalImage(e:Event):void
{
var fr:FileReference = new FileReference();
fr.browse([new FileFilter("Images", "*.jpg;*.png;*.jpeg")]);
fr.addEventListener(Event.SELECT, function(e:*):void
{
fr.load();
}
);
fr.addEventListener(Event.COMPLETE, function(e:*):void
{
loadAndShowImage(fr.data);
}
);
}
private function loadAndShowImage(data:ByteArray):void
{
imageContainer.removeChildren();
var loader:Loader = new Loader();
loader.loadBytes(data, new LoaderContext(false));
imageContainer.addChild(loader);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:*):void
{
if (loader.height > 400)
{
var scale:Number = 400 / loader.height;
loader.scaleX = scale;
loader.scaleY = scale;
}
}
);
}
}
}