Flash 围绕中心翻转图像
要围绕中心翻转图像,我使用以下代码:Flash 围绕中心翻转图像,flash,actionscript-3,apache-flex,Flash,Actionscript 3,Apache Flex,要围绕中心翻转图像,我使用以下代码: // Calculate offset var offsetWidth:Number = image.contentWidth/2.0; var offsetHeight:Number = image.contentHeight/2.0; // Perform flip var matrix:Matrix = new Matrix(); matrix.trans
// Calculate offset
var offsetWidth:Number = image.contentWidth/2.0;
var offsetHeight:Number = image.contentHeight/2.0;
// Perform flip
var matrix:Matrix = new Matrix();
matrix.translate(-offsetWidth, -offsetHeight);
if(direction=="HORIZONTAL"){
matrix.scale(-1, 1);
}else if(direction=="VERTICAL"){
matrix.scale(1,-1)
}
matrix.translate(+offsetWidth, +offsetHeight);
matrix.concat(image.transform.matrix);
image.transform.matrix = matrix.clone();
这很好。但我的问题是当我尝试从图像中获取位图数据时,如下所示:
var bitmapData:BitmapData = new BitmapData(image.width,image.height);
bitmapData.draw(image);
并将位图数据用作另一个图像的源,不显示图像。将显示断开的图像图标。
此外,我正在围绕图像中心旋转图像,并使用下面提到的类似代码,其工作正常,我能够将位图数据复制到另一个图像。以下代码供参考:
var matrix:Matrix = new Matrix();
matrix.rotate(Math.PI/2);
matrix.tx = img.content.height;
var bd:BitmapData = new BitmapData(img.content.height, img.content.width);
bd.draw(img.content, matrix);
请提供相关帮助。答案在问题中 在第二种情况下,您使用的是矩阵
bd.draw(img.content, matrix);
同样地,翻转也是如此
bitmapData.draw(image, matrix);
编辑:
翻转中矩阵的平移计算不正确。只有在水平和垂直翻转时,才需要X和Y上的平移。
请在下面找到它工作的代码
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.geom.Matrix;
import flash.geom.Rectangle;
[SWF(width="640", height="480")]
public class FlipRotate extends Sprite
{
[Embed(source="away3d.jpg")]
private var Pic:Class;
public function FlipRotate()
{
super();
stage.scaleMode = StageScaleMode.NO_SCALE;
var sourceImage:Bitmap;
var targetImage:Bitmap;
sourceImage = new Pic();
//flip(sourceImage, "BOTH");
flip(sourceImage, "VERTICAL");
//flip(sourceImage, "BOTH");
rotate(sourceImage, Math.PI/3);
targetImage = new Bitmap();
/* Get the current bounds of source image to calculate the dimenstion of new image
Apply translation to bring the content outside into view in new image */
var bounds:Rectangle = sourceImage.getBounds(this);
var matrix:Matrix = sourceImage.transform.matrix;
matrix.translate(-bounds.x, -bounds.y);
/* Draw in new image applying translated matrix of source imgae */
targetImage.bitmapData = new BitmapData(bounds.width, bounds.height);
targetImage.bitmapData.draw(sourceImage, matrix);
/* Add new image and postion at the center of stage */
addChild(targetImage);
targetImage.x = (stage.stageWidth-targetImage.width)/2;
targetImage.y = (stage.stageHeight-targetImage.height)/2;
}
public function flip(image:Bitmap, direction:String):void{
var matrix:Matrix = image.transform.matrix;
if(direction=="HORIZONTAL"){
matrix.scale(-1, 1);
matrix.translate(image.width, 0);
}else if(direction=="VERTICAL"){
matrix.scale(1,-1);
matrix.translate(0, image.height);
}else if(direction == "BOTH"){
matrix.scale(-1,-1);
matrix.translate(image.width, image.height);
}
image.transform.matrix = matrix;
}
public function rotate(image:Bitmap, angle:Number):void{
var matrix:Matrix = image.transform.matrix;
matrix.translate(-image.width/2, -image.height/2);
matrix.rotate(angle);
matrix.translate(image.width/2, image.height/2);
image.transform.matrix = matrix;
}
}
}
你的
image
对象到底是什么?我正在使用mx.controls.image.image()的一个实例……我也在围绕中心旋转图像,使用上面提到的类似代码,并且工作正常。以下是供你参考的:var-matrix:matrix=new-matrix();旋转矩阵(数学PI/2);matrix.tx=img.content.height;var bd:BitmapData=新的BitmapData(img.content.height、img.content.width);bd.draw(img.content,矩阵);最好使用以下附加信息更新您的问题:)您知道您可以使用FlexRotate
effect向右旋转您的对象吗?如果出现任何沙箱冲突错误,您可以查看您的跟踪吗?(不允许您从其他沙盒“绘制”图像)确定。让我尝试一下,并找到一个解决方案。我已经用工作代码更新了答案。如果你需要任何澄清,请告诉我。