1、创建as类
package com.dse.app.ydcj.widget
{
import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
import mx.controls.Alert;
import mx.controls.Image;
public class ShowErJinImg extends mx.controls.Image
{
private var _loader:Loader = new Loader();
private var _bFillUp:Boolean = false; //是/否平铺
private var _bonthebl:Boolean=true;//是否按照比例缩放全图显示。
public function Image():void {}
override protected function createChildren():void
{
for(var i:int=numChildren-1;i>=0;i--){
this.removeChildAt(i);
}
addChild(_loader);
}
public function get bonthebl():Boolean{
return _bonthebl;
}
public function set bonthebl(value:Boolean):void{
this._bonthebl = value;
}
public function get bFillUp():Boolean{
return _bFillUp;
}
public function set bFillUp(value:Boolean):void{
this._bFillUp = value;
}
public function loadBytes(bytes:ByteArray,context:LoaderContext=null):void
{
try
{
_loader.loadBytes(bytes);
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onBytesLoaded);
_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
createChildren();
}catch(e:Error)
{
Alert.show("该类型的文件不支持预览!");
}
}
private function onError(event:IOErrorEvent):void
{
Alert.show("该类型的文件不支持预览!");
}
private function onBytesLoaded(e:Event):void
{
this.width=555;
this.height=380;
if(_bonthebl){
var ih:int=e.currentTarget.height;
var iw:int=e.currentTarget.width;
var blh:Number=this.height/ih;
var blw:Number=this.width/iw;
if(blh>1&&blw>1){
_loader.width=e.currentTarget.width;
_loader.height=e.currentTarget.height;
}
else if(blh>1||blw>1){
if(blh<1){
_loader.width = Math.round(iw*blh);
_loader.height = this.height;
}else{
_loader.width = this.width;
_loader.height = Math.round(ih*blw);
}
}else{
if(blh>blw){
_loader.width = Math.round(iw*blw);
_loader.height = Math.round(ih*blw);
}
else{
_loader.width = Math.round(iw*blh);
_loader.height = Math.round(ih*blh);
}
}
this.width=_loader.width;
this.height=_loader.height;
}
if(_bFillUp)
{
_loader.width = this.width;
_loader.height = this.height;
}
}
public function SetWidth(newWidth:Number):void{
this.width = newWidth;
this._loader.width = this.width;
}
}
}
2、展示调用
private function dY:void{
img.loadBytes(imglist[imgnum].imgurl);
}
<widget:ShowErJinImg id="img" height="380" width="555" mouseWheel="{sco(event)}"/>