如果 一个400,200 的图片 放在800,800的框里,强制使用 Bitmap.draw(800,800)时,并不会画出800*800的图来,而是高度无法画满,图像上下出现白边。我认为这是因为图片拉伸时要按照图片本身的比例。如果任意缩放,因为Flash无法缩放完全,就会出现图片没有占满,却出现讨厌的白边。
这里我使用的一个自定义方法来重绘图片。
如果 图片框的 宽度 大于 高度方向上 图片填满图片框所用的拉伸比例 计算出来的宽度,说明图片框的宽度将无法填充满,就使用图片的高度按照比例来计算最终的拉伸后的宽度。
- package Utilities
- {
- import flash.display.BitmapData;
- import flash.geom.Matrix;
- public class BitmapUtil
- {
- public static function GenerateThumbnail( bmp:BitmapData, w:Number,
- h:Number, crop:Boolean = false ):BitmapData
- {
- var scale:Number = 1.0;
- if( bmp.width > w || bmp.height > h )
- {
- scale = Math.min( w / bmp.width, h / bmp.height );
- }
- var m:Matrix = new Matrix();
- m.scale( scale, scale );
- if( !crop )
- {
- m.tx = ( w / 2 ) - ( ( bmp.width * scale ) / 2 );
- m.ty = ( h / 2 ) - ( ( bmp.height * scale ) / 2 );
- }
- else
- {
- w = bmp.width * scale;
- h = bmp.height * scale;
- }
- var bmd:BitmapData = new BitmapData( w, h, true );
- bmd.draw( bmp, m );
- return bmd;
- }
- }
- }
在loader 函数中,记录图片的原始尺寸。
- actualHeight = loaderInfo.height;
- actualWidth = loaderInfo.width;
用于显示图片的 区域大小mLenth, mHeight
- //记录图片的比例,要成比例缩放。
- var scaleXYFactor : Number = actualHeight / actualWidth;
- if(mLenth > (mHeight / scaleXYFactor))
- {
- //Make the image align to right
- this.x = mLenth - mHeight / scaleXYFactor;
- mLenth = mHeight / scaleXYFactor;
- }
- else
- {
- this.y = mHeight - mLenth * scaleXYFactor;
- mHeight = mLenth * scaleXYFactor;
- }
- var mBitmapData : BitmapData = Utilities.BitmapUtil.GenerateThumbnail(bitmapData,mLenth,mHeight);
- mBitmap = new Bitmap(mBitmapData);
- return mBitmap;
反之,说明图片框的高度将无法填充满,要重新按比例计算 重绘图片的高度。
另外,我们可以重绘加载Loader的元件背景。
- var rectangle : Rectangle = this.getBounds(this);
- this.graphics.beginFill(0x333333);
- this.graphics.drawRect(0,0,rectangle.width,rectangle.height);
- addChild(mBrandImage);