0

我正在尝试将 DisplayObject 缩放到某个点。我认为这很容易,但我现在花了一天时间试图弄清楚。

基本上,我认为这应该有效。强调应该。

//newPoint is the point being centered. There is no initial scaling, so I do not need to compensate for that (yet)
//scale is the zoom level
//container is the parent of the obj
//obj is the object being scaled/panned
var p:Point = new Point(
    ( this.container.width - this.obj.width * scale + newPoint.x * scale ) / 2, 
    ( this.container.height - this.obj.height * scale + newPoint.y * scale ) / 2 
);

this.obj.scaleX = this.obj.scaleY = scale;
this.obj.x = p.x;
this.obj.y = p.y;

如果比例为 1,它将使点居中,但随着比例的增加,它会越来越远离中心。我已经尝试了几十种不同的方法。我在几个站点上看到的这种方法产生了相同的确切结果。有人知道如何让它工作吗?

编辑 10-1-12:作为后续,我将 LondonDrugs_MediaServices 提供的代码片段作为我最初问题的基础。我需要能够以相对于未缩放图像的特定比例缩放到特定点(想想谷歌地图如何缩放到特定位置)。为此,我必须在运行翻译代码之前将我的图像集中在该点上。我已经在下面发布了附加代码。对于其他用途(捏缩放、滚动和双击),我使用了 Vesper 提供的代码,效果很好。

//obj is the object being translated
//container is its parent
//x and y are the coordinates to be zoomed to, in untranslated scaling
//obj.scaleX and obj.scaleY are always identical in my class, so there is no need to account for that


//calculates current center point, with scaling
var center:Point = new Point( ( this.container.width - this.obj.width * this.obj.scaleX ) / 2, ( this.container.height - this.obj.height * this.obj.scaleX ) / 2 );

//calulcates the distance from center the point is, with scaling
var distanceFromCenter:Point = new Point( this.obj.width * this.obj.scaleX / 2 - x * this.obj.scaleX, this.obj.height * this.obj.scaleX / 2 - y * this.obj.scaleX );

//center the object on that specific point
this.obj.x = center.x + distanceFromCenter.x;
this.obj.y = center.y + distanceFromCenter.y;
4

4 回答 4

1
var mat:Matrix=new Matrix();
mat.translate(-p.x,-p.y);
mat.scale(desiredScale,desiredScale);
mat.translate(p.x,p.y);
yourObject.transform.matrix=mat;

核心点是缩放是在 (0,0) 附近完成的,但是您可以使用描述仿射变换的矩阵来完成。您首先制作一个空矩阵(即不转换的矩阵),然后对其应用一组转换。首先,通过平移 -1* 坐标将所需点放置在 (0,0) 处,然后缩放,然后平移回来。

于 2012-09-29T04:33:46.133 回答
1

嗨,伙计们....谢谢您的评论...我找到了答案...代码:

gambar.addEventListener(TransformGestureEvent.GESTURE_ZOOM , onZoom);
函数onZoom(事件:TransformGestureEvent):无效{

    var locX:Number=event.localX;

    var locY:Number=event.localY;

    var stX:Number=event.stageX;

    var stY:Number=event.stageY;

    var prevScaleX:Number=gambar.scaleX;

    var prevScaleY:Number=gambar.scaleY;

    var mat:Matrix;

    var externalPoint=new Point(stX,stY);

    var internalPoint=new Point(locX,locY);

    gambar.scaleX *= event.scaleX;

    gambar.scaleY *= event.scaleY;

     if(event.scaleX>1 && gambar.scaleX>6){

         gambar.scaleX=prevScaleX;

         gambar.scaleY=prevScaleY;

       }

     if(event.scaleY>1 && gambar.scaleY>6){

         gambar.scaleX=prevScaleX;

         gambar.scaleY=prevScaleY;

       } 

      if(event.scaleX<1 && gambar.scaleX<0.8){

         gambar.scaleX=prevScaleX;

         gambar.scaleY=prevScaleY;

       }

     if(event.scaleY<1 && gambar.scaleY<0.8){

         gambar.scaleX=prevScaleX;

         gambar.scaleY=prevScaleY;

       }  

     mat=gambar.transform.matrix.clone();

     MatrixTransformer.matchInternalPointWithExternal(mat,internalPoint,externalPoint);

     gambar.transform.matrix=mat;

    }
于 2014-01-09T01:36:58.057 回答
0

矩阵的答案是绝对正确的,但是如果您碰巧是 Club GreenSock 会员,您可以使用 TransformAroundPointPlugin 使用非常简单的代码获得一些不错的功能

http://www.greensock.com/as/docs/tween/com/greensock/plugins/TransformAroundPointPlugin.html

您可以在此处的插件资源管理器中看到一个示例:http: //www.greensock.com/tweenlite/#plugins

我用它来补间我所有的缩放,并且比我尝试手动执行此操作时具有更好的性能。IMO 整个图书馆都物有所值(不,除了喜欢图书馆之外,我没有任何联系)。如果您需要任何其他功能,我会考虑它。它还具有 ThrowProps 插件 ( http://www.greensock.com/throwprops/ ),如果您要在移动设备上拥有一个边界框,并且希望顺利返回边界,这非常重要。

于 2012-10-01T21:27:08.480 回答
-1

将 obj.x 设置为 -px,将 obj.y 设置为 -py,将容器 scaleX 和 scaleY 设置为所需的值,并将 px 添加到容器 x,将 py 添加到容器 y。完毕!

于 2012-09-28T23:01:05.863 回答