仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 719|回复: 7
打印 上一主题 下一主题

[学习教程] 动画教程之Flash相册图片加载终了等比缩放的类-Flash actionscript

[复制链接]
海妖 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-15 20:41:16 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Flash特别适用于创建通过Internet提供的内容,因为它的文件非常小。Flash是通过广泛使用矢量图形做到这一点的。
poluoluo中心提醒:flash加载内部图片等比缩放类(as3)
每次做相册类的flash的时分,图片加载终了以后都要写等比缩放的函数来处置加载图片。
明天又碰到了,也不晓得哪根筋不合错误了,弄了十几分钟总是算法有毛病。好好的一个玉人图片被弄的惨绝人寰。以是写了这个类间接放到我的as包里,今后不要再这么贫苦了。
贴一下,能用的伴侣就用一用。看不上眼的也不要拍砖啊。
下边是代码及正文申明:
/*
等比缩放
as1984-qq:38657783
20091221
请注重包路径,我的as包都放在tools目次下。以是包的路径是tools
假如你的路径纷歧样,请注重修正
*/
packagetools
{
publicclassimgzoom{

//变量声明
privatevarisZoom:Boolean;//是不是缩放
privatevarsrcWidth:Number;//原始宽
privatevarsrcHeight:Number;//原始高
privatevarmaxWidth:Number;//限定宽
privatevarmaxHeight:Number;//限定高
privatevarnewWidth:Number;//新宽
privatevarnewHeight:Number;//新高

publicfunctionimgzoom(srcWidth:Number,srcHeight:Number,maxWidth:Number,maxHeight:Number):
void
{
this.srcWidth=srcWidth;//取得原始宽度
this.srcHeight=srcHeight;//取得原始高度
this.maxWidth=maxWidth;//取得限制宽度
this.maxHeight=maxHeight;//取得限制高度
if(this.srcWidth>0&&this.srcWidth>0){//反省图片高度是不是一般
this.isZoom=true;//高宽一般,实行缩放处置
}else{
this.isZoom=false;//不一般,前往0
}
conductimg();//实行缩放算法
}
publicfunctionwidth():Number{//前往处置后的宽度,准确到2个小数点
returnNumber(this.newWidth.toFixed(2));
}
publicfunctionheight():Number{//前往处置后的高度,准确到2个小数点
returnNumber(this.newHeight.toFixed(2));
}
privatefunctionconductimg():void{
if(this.isZoom){//假如高宽一般,入手下手盘算
if(this.srcWidth/this.srcHeight>=this.maxWidth/this.maxHeight){
//对照高宽比例,断定以宽大概是高为基准举行盘算。
if(this.srcWidth>this.maxWidth){//以宽为基准入手下手盘算,
//当宽度年夜于限制宽度,入手下手缩放
this.newWidth=this.maxWidth;
this.newHeight=(this.srcHeight*this.maxWidth)/this.srcWidth
}else{
//当宽度小于限制宽度,间接前往原始数值。
this.newWidth=this.srcWidth;
this.newHeight=this.srcHeight;
}
}else{
if(this.srcHeight>this.maxHeight){//以高为基准,举行盘算
//当高度年夜于限制高度,入手下手缩放。
this.newHeight=this.maxHeight;
this.newWidth=(this.srcWidth*this.maxHeight)/this.srcHeight
}else{
//当高度小于限制高度,间接前往原始数值。
this.newWidth=this.srcWidth;
this.newHeight=this.srcHeight;
}
}
}else{//不一般,前往0
this.newWidth=0;
this.newHeight=0;
}
}
}
}
使用典范
importtools.imgzoom;
函数当加载终了时{
vart:Sprite=newSprite();
varw:Number=685;//限制的宽度
varh:Number=450;//限制的高度
vart:Sprite=newSprite();//造个器材筹办装图片
varmyZoom:imgzoom=newimgzoom(e.target.content.width,e.target.content.height,w,h);//实例化算法
e.target.content.width=myZoom.width();//把加载过去的器材宽度弄了
e.target.content.height=myZoom.height();//把加载过去的器材高度弄了
t.addChild(e.target.content);//到场t的显现列表

}
文件下载:imgzoom.rar
倡议用bitmapdata分离matrix来处置这个成绩,间接得出必要显现的那块图象数据(更勤俭资本),能够不必频仍设置坐标和缩放,代码量也会少良多。
贴出关头代码:
publicstaticfunctiongetZoomDraw(targetisplayObject,tarW:int,tarH:int,full:Boolean=true):BitmapData{
//猎取显现工具矩形局限
varrect:Rectangle=target.getBounds(target);
//盘算出应该缩放的比例
varperw=tarW/rect.width;
varperh=tarH/rect.height;
varscale=full?((perw<=perh)?perwerh)(perw<=perh)?perherw);
//盘算缩放后与划定尺寸之间的偏移量
varofferW=(tarW-rect.width*scale)/2;
varofferH=(tarH-rect.height*scale)/2;
//入手下手绘制快照(这里通明参数是false,是便利察看效果,实践使用可改成true)
varbmd:BitmapData=newBitmapData(tarW,tarH,false,0);
varmatrix:Matrix=newMatrix();
matrix.scale(scale,scale);
matrix.translate(offerW,offerH);
bmd.draw(target,matrix);
//假如是bitmap工具,开释位图资本
if(targetisBitmap)(targetasBitmap).bitmapData.dispose();
//前往截图数据
returnbmd;
}
源文件实例:zoom.rar

ActionScript是一种程序语言的简单文本文件.FLA档案能够直接包含ActionScript。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-13 07:10

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表