快速业务通道

Flash AS:加载外部图片等比缩放问题详解

作者 佚名技术 来源 服务器技术 浏览 发布时间 2012-07-07

每次做相册类的Flash的时候,图片加载完毕之后都要写等比缩放的函数来处理加载图片。 下边是代码及注释说明: /*
等比缩放
as1984 - qq:38657783
20091221
请注意包路径,我的as包都放在tools目录下。所以包的路径是 tools
如果你的路径不一样,请注意修改
*/
package tools
{
public class imgzoom {

// 变量声明
private var isZoom:Boolean;//是否缩放
private var srcWidth:Number;//原始宽
private var srcHeight:Number;//原始高
private var maxWidth:Number;//限制宽
private var maxHeight:Number;//限制高
private var newWidth:Number;//新宽
private var newHeight:Number;//新高

public function imgzoom(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();//执行缩放算法
}
public function width():Number{//返回处理后的宽度,精确到2个小数点
return Number(this.newWidth.toFixed(2));
}
public function height():Number{//返回处理后的高度,精确到2个小数点
return Number(this.newHeight.toFixed(2));
}
private function conductimg():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;
}
}
}
} 应用范例 import tools.imgzoom;
函数 当加载完毕时{
var t:Sprite=new Sprite();
var w:Number=685;//限定的宽度
var h:Number=450;//限定的高度
var t:Sprite=new Sprite();//造个东西准备装图片
var myZoom:imgzoom=new imgzoom(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来处理这个问题,直接得出需要显示的那块图像数据(更节约资源),可以不用频繁设置坐标和缩放,代码量也会少很多。 贴出关键代码: public static function getZoomDraw(targetisplayObject, tarW:int, tarH:int,full:Boolean=true):BitmapData {
//获取显示对象矩形范围
var rect:Rectangle = target.getBounds(target);
//计算出应当缩放的比例
var perw = tarW / rect.width;
var perh = tarH / rect.height;
var scale = full?((perw <= perh)?perwerh)(perw <= perh)?perherw);
//计算缩放后与规定尺寸之间的偏移量
var offerW = (tarW - rect.width * scale) / 2;
var offerH = (tarH - rect.height * scale) / 2;
//开始绘制快照(这里透明参数是false,是方便观察效果,实际应用可改为true)
var bmd:BitmapData = new BitmapData(tarW, tarH, false, 0);
var matrix:Matrix = new Matrix();
matrix.scale(scale, scale);
matrix.translate( offerW, offerH);
bmd.draw(target, matrix);
//如果是bitmap对象,释放位图资源
if (target is Bitmap) (target as Bitmap).bitmapData.dispose();
//返回截图数据
return bmd;
} 源文件实例: zoom.rar 关键词:

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号