Flash动态加载外部图片
2009-01-05 20:27
411 查看
.paragraph{text-indent:2em;}
效果如下,载入的过程中会有进度条提示,载入完成过后可以根据图片的尺寸进行缩放,按比例的缩小放在400×300的方框中,居中显示:
btnSubmit.addEventListener(MouseEvent.CLICK,btnSubmitClickHandler);
function btnSubmitClickHandler(e:MouseEvent) {
loadPicture(ddlUrl.value);
}
var loader:Loader;
function loadPicture(url:String) {
if(loader == null){
loader = new Loader();
}else{
loader.unload();
txtAlert.text = "";
}
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHd);
loader.contentLoaderInfo.addEventListener("ioError",errorHd);
var request:URLRequest = new URLRequest(url);
var context:LoaderContext = new LoaderContext(true);
loader.load(request,context);
progress.visible = true;
}
var maxHeight:int = container.height;
var maxWidth:int = container.width;
function completeHd(e:Event) {
txtAlert.text = "* Load success!";
var pic:Bitmap = loader.content as Bitmap;
pic.smoothing = true;
var currentWidth:int = loader.width;
var currentHeight:int = loader.height;
if(currentWidth > maxWidth){
currentWidth = maxWidth;
currentHeight = currentWidth/loader.width * loader.height;
}
if(currentHeight > maxHeight){
currentHeight = maxHeight;
currentWidth = currentHeight/loader.height * loader.width;
}
loader.content.width = currentWidth;
loader.content.height = currentHeight;
loader.x = (maxWidth - currentWidth)/2;
loader.y = (maxHeight - currentHeight)/2;
container.addChild(loader);
progress.visible = false;
}
function errorHd(e:Event){
progress.visible = false;
txtAlert.text = "* Url is invalid, Please try again for another url!";
}
Loader还具有content属性,Loader实例本身就是一个可视对象,content也是一个可视对象,如果加载的地址是一个图片的话,content是一个Bitmap对象,所以可以通过转型来使得载入的图片在缩小的时候消除锯齿,将Bitmap的smoothing属性设置为true即可。
====>>欢迎访问 搜学通培训信息网,网罗全国英语培训课程
一、简介
初学AS,做了一个从外部调用图片的例子。AS2.0中有MovieClip和全局中有loadMovie函数可以动态加载图片,但是这不是异步加载,而且没法知道图片大小。在AS3.0中,有Loader类来实现从外部载入swf和各种图片。效果如下,载入的过程中会有进度条提示,载入完成过后可以根据图片的尺寸进行缩放,按比例的缩小放在400×300的方框中,居中显示:
二、代码
progress.visible = false;btnSubmit.addEventListener(MouseEvent.CLICK,btnSubmitClickHandler);
function btnSubmitClickHandler(e:MouseEvent) {
loadPicture(ddlUrl.value);
}
var loader:Loader;
function loadPicture(url:String) {
if(loader == null){
loader = new Loader();
}else{
loader.unload();
txtAlert.text = "";
}
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHd);
loader.contentLoaderInfo.addEventListener("ioError",errorHd);
var request:URLRequest = new URLRequest(url);
var context:LoaderContext = new LoaderContext(true);
loader.load(request,context);
progress.visible = true;
}
var maxHeight:int = container.height;
var maxWidth:int = container.width;
function completeHd(e:Event) {
txtAlert.text = "* Load success!";
var pic:Bitmap = loader.content as Bitmap;
pic.smoothing = true;
var currentWidth:int = loader.width;
var currentHeight:int = loader.height;
if(currentWidth > maxWidth){
currentWidth = maxWidth;
currentHeight = currentWidth/loader.width * loader.height;
}
if(currentHeight > maxHeight){
currentHeight = maxHeight;
currentWidth = currentHeight/loader.height * loader.width;
}
loader.content.width = currentWidth;
loader.content.height = currentHeight;
loader.x = (maxWidth - currentWidth)/2;
loader.y = (maxHeight - currentHeight)/2;
container.addChild(loader);
progress.visible = false;
}
function errorHd(e:Event){
progress.visible = false;
txtAlert.text = "* Url is invalid, Please try again for another url!";
}
三、原理
关键在于对Loader类的使用,Loader具有contentLoaderInfo属性,此属性是一个LoaderInfo类的实例,用来控制加载外部资源的进度,因此就可以在contentLoaderInfo上注册各种事件,这里只用了Complete事件,用于加载完成时调用。Loader还具有content属性,Loader实例本身就是一个可视对象,content也是一个可视对象,如果加载的地址是一个图片的话,content是一个Bitmap对象,所以可以通过转型来使得载入的图片在缩小的时候消除锯齿,将Bitmap的smoothing属性设置为true即可。
====>>欢迎访问 搜学通培训信息网,网罗全国英语培训课程
相关文章推荐
- flash如何控制外部加载的图片大小的解决方法
- FLASH中如何加载外部图片
- 如何让Flash加载调用外部图片和元件
- flash动态加载多张图片
- cocos2d js 如何动态加载外部图片
- flash加载外部图片并设定位置及大小
- 在Flash的AS2中加载外部文件(图片/SWF/文本)的办法
- 每天学一点flash (20) flash cs3.0 外部加载图片
- 动态加载并复制外部图片
- flash调用外部图片(加载到影片剪辑中)
- 每天学一点flash (20) flash cs3.0 外部加载图片
- Android动态加载外部jar包及jar包中图片等资源文件
- flash加载外部图片
- 每天学一点flash (20) flash cs3.0 外部加载图片
- flash加载外部图片进度显示
- Flash 加载外部数据-ajax与图片加载
- flash加载外部图片等比缩放类(as3)
- Flash通过外部文本动态载入图片
- flash如何控制外部加载的图片大小的解决方法
- python学习(7):python爬虫之爬取动态加载的图片,以百度图片为例