利用BitmapData.draw对位图进行翻转缩放操作

由此为基础,使用BitmapData.draw,配合Matrix,就可以实现一个位图或者对象的水平翻转和垂直翻转,下面是实现代码:

package org.easily.utils
{
import flash.display.BitmapData;
import flash.geom.Matrix;

public class BitmapDataUtils
{
//水平翻转一个位图
public static function flipHorizontal(bmpData:BitmapData, transparent:Boolean = true, fillColor:uint = 0):BitmapData
{
var matrix:Matrix = new Matrix();
matrix.a = -1;
matrix.tx = bmpData.width;
var bmpData_:BitmapData = new BitmapData(bmpData.width, bmpData.height, transparent, fillColor);
bmpData_.draw(bmpData, matrix);
return bmpData_;
}

//垂直翻转一个位图
public static function flipVertical(bmpData:BitmapData, transparent:Boolean = true, fillColor:uint = 0):BitmapData
{
var matrix:Matrix = new Matrix();
matrix.d = -1;
matrix.ty = bmpData.height;
var bmpData_:BitmapData = new BitmapData(bmpData.width, bmpData.height, transparent, fillColor);
bmpData_.draw(bmpData, matrix);
return bmpData_;
}

//缩放位图
public static function scaleBitmapData(bmpData:BitmapData, scaleX:Number, scaleY:Number):BitmapData
{
var matrix:Matrix = new Matrix();
matrix.scale(scaleX, scaleY);
var bmpData_:BitmapData = new BitmapData(scaleX * bmpData.width, scaleY * bmpData.height, true, 0);
bmpData_.draw(bmpData, matrix);
return bmpData_;
}
}
}

添加新评论