·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3加载外部jpg图片实现等比缩放类源代码实例

时间:2013-09-03 14:13blueidea.com
[AS3]as3加载外部jpg图片实现等比缩放类源代码实例,每次做相册类的flash的时候,图片加载完毕之后都要写等比缩放的函数来处理加载图片

每次做相册类的flash的时候,图片加载完毕之后都要写等比缩放的函数来处理加载图片。
今天又遇到了,也不知道哪根筋不对了,弄了十几分钟老是算法有错误。好好的一个美女图片被搞的惨不忍睹。
所以写了这个类 直接放到我的as包里,以后不要再这么麻烦了。
贴一下,能用的朋友就用一用。看不上眼的也不要拍砖啊。下边是代码及注释说明。

  1. /* 
  2.   等比缩放 
  3.   as1984 - qq:38657783 
  4.   20091221 
  5.   请注意包路径,我的as包都放在tools目录下。所以包的路径是 tools 
  6.   如果你的路径不一样,请注意修改 
  7. */ 
  8. package tools 
  9.         public class imgzoom { 
  10.                  
  11.                 // 变量声明 
  12.                 private var isZoom:Boolean;//是否缩放 
  13.                 private var srcWidth:Number;//原始宽 
  14.                 private var srcHeight:Number;//原始高 
  15.                 private var maxWidth:Number;//限制宽 
  16.                 private var maxHeight:Number;//限制高 
  17.                 private var newWidth:Number;//新宽 
  18.                 private var newHeight:Number;//新高 
  19.                  
  20.                 public function imgzoom(srcWidth:Number,srcHeight:Number,maxWidth:Number,maxHeight:Number):void 
  21.                 { 
  22.                         this.srcWidth=srcWidth;//获得原始宽度 
  23.                         this.srcHeight=srcHeight;//获得原始高度 
  24.                         this.maxWidth=maxWidth;//获得限定宽度 
  25.                         this.maxHeight=maxHeight;//获得限定高度 
  26.                         if(this.srcWidth>0 && this.srcWidth>0){//检查图片高度是否正常 
  27.                                 this.isZoom=true;//高宽正常,执行缩放处理 
  28.                         }else{ 
  29.                                 this.isZoom=false;//不正常,返回0 
  30.                         } 
  31.                         conductimg();//执行缩放算法 
  32.                 } 
  33.                 public function width():Number{//返回处理后的宽度,精确到2个小数点 
  34.                         return Number(this.newWidth.toFixed(2)); 
  35.                 } 
  36.                 public function height():Number{//返回处理后的高度,精确到2个小数点 
  37.                         return Number(this.newHeight.toFixed(2)); 
  38.                 } 
  39.                 private function conductimg():void{ 
  40.                         if(this.isZoom){//如果高宽正常,开始计算 
  41.                                 if(this.srcWidth/this.srcHeight>=this.maxWidth/this.maxHeight){ 
  42.                                        //比较高宽比例,确定以宽或者是高为基准进行计算。 
  43.                                         if(this.srcWidth>this.maxWidth){//以宽为基准开始计算, 
  44.                                                 //当宽度大于限定宽度,开始缩放 
  45.                                                 thisthis.newWidth=this.maxWidth; 
  46.                                                 this.newHeight=(this.srcHeight*this.maxWidth)/this.srcWidth 
  47.                                         }else{ 
  48.                                                 //当宽度小于限定宽度,直接返回原始数值。 
  49.                                                 thisthis.newWidth=this.srcWidth; 
  50.                                                 thisthis.newHeight=this.srcHeight; 
  51.                                         } 
  52.                                 }else{ 
  53.                                         if(this.srcHeight>this.maxHeight){//以高为基准,进行计算 
  54.                                                 //当高度大于限定高度,开始缩放。 
  55.                                                 thisthis.newHeight=this.maxHeight; 
  56.                                                 this.newWidth=(this.srcWidth*this.maxHeight)/this.srcHeight 
  57.                                         }else{ 
  58.                                                 //当高度小于限定高度,直接返回原始数值。 
  59.                                                 thisthis.newWidth=this.srcWidth; 
  60.                                                 thisthis.newHeight=this.srcHeight; 
  61.                                         } 
  62.                                 } 
  63.                         }else{//不正常,返回0 
  64.                                 this.newWidth=0
  65.                                 this.newHeight=0
  66.                         } 
  67.                 } 
  68.         } 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器