Flash Tip(6) 如何处理 Load 图片时 比例缩放失调的问题

如果 一个400,200 的图片 放在800,800的框里,强制使用 Bitmap.draw(800,800)时,并不会画出800*800的图来,而是高度无法画满,图像上下出现白边。我认为这是因为图片拉伸时要按照图片本身的比例。如果任意缩放,因为Flash无法缩放完全,就会出现图片没有占满,却出现讨厌的白边。

这里我使用的一个自定义方法来重绘图片。
  1. package Utilities
  2. {
  3.     import flash.display.BitmapData;
  4.     import flash.geom.Matrix;
  5.     public class BitmapUtil
  6.     {
  7.         public static function GenerateThumbnail( bmp:BitmapData, w:Number,
  8.                                         h:Number, crop:Boolean = false ):BitmapData
  9.         {
  10.             var scale:Number = 1.0;
  11.             if( bmp.width > w || bmp.height > h )
  12.             {
  13.                 scale = Math.min( w / bmp.width, h / bmp.height );
  14.             }
  15.             var m:Matrix = new Matrix();
  16.             m.scale( scale, scale );
  17.             if( !crop )
  18.             {
  19.                 m.tx = ( w / 2 ) - ( ( bmp.width * scale ) / 2 );
  20.                 m.ty = ( h / 2 ) - ( ( bmp.height * scale ) / 2 );
  21.             }
  22.             else
  23.             {
  24.                 w = bmp.width * scale;
  25.                 h = bmp.height * scale;
  26.             }
  27.             var bmd:BitmapData = new BitmapData( w, h, true );
  28.             bmd.draw( bmp, m );
  29.             
  30.             return bmd;
  31.         }
  32.     }
  33. }
在loader 函数中,记录图片的原始尺寸。
  1. actualHeight = loaderInfo.height;
  2. actualWidth = loaderInfo.width;
用于显示图片的 区域大小mLenth, mHeight
  1. //记录图片的比例,要成比例缩放。
  2. var scaleXYFactor : Number = actualHeight / actualWidth; 
  3.             
  4. if(mLenth > (mHeight / scaleXYFactor))
  5. {
  6.    //Make the image align to right
  7.    this.x = mLenth - mHeight / scaleXYFactor;
  8.                 
  9.    mLenth = mHeight / scaleXYFactor;                
  10. }
  11. else 
  12. {
  13.    this.y = mHeight - mLenth * scaleXYFactor;
  14.    mHeight = mLenth * scaleXYFactor;
  15. }
  16.             
  17. var mBitmapData : BitmapData = Utilities.BitmapUtil.GenerateThumbnail(bitmapData,mLenth,mHeight);
  18. mBitmap = new Bitmap(mBitmapData);
  19. return mBitmap;
如果 图片框的 宽度 大于 高度方向上 图片填满图片框所用的拉伸比例 计算出来的宽度,说明图片框的宽度将无法填充满,就使用图片的高度按照比例来计算最终的拉伸后的宽度。
反之,说明图片框的高度将无法填充满,要重新按比例计算 重绘图片的高度。

另外,我们可以重绘加载Loader的元件背景。
  1. var rectangle : Rectangle = this.getBounds(this);
  2. this.graphics.beginFill(0x333333);
  3.             this.graphics.drawRect(0,0,rectangle.width,rectangle.height);
  4. addChild(mBrandImage);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值