使用AS3放大和缩小

时间:2010-04-07 21:49:28

标签: flex actionscript-3 zoom out

你们都知道:“在Flash文件中右键单击 - >放大或缩小”,好吧,我需要这样做但是使用,例如,点击按钮。

这只能使用AS3代码吗?

THX!

4 个答案:

答案 0 :(得分:1)

你必须将所有图形放在一个精灵中,例如“场景”,然后修改它的比例......

答案 1 :(得分:0)

据我所知。很想看到其他答案。

想到一个hacky的事情,也许你可以使用javascript 控制包含你的swf的div,以便div变大(放大), 但显示在同一'scrollRect'矩形内。你会打电话给 javascript函数使用ExternalInterface来做到这一点。

我使用flash的scrollPane组件进行的快速测试:

//zoomIn,zoomOut = button
//sp = scrollPane, scrollDrag = true
zoomIn.addEventListener(MouseEvent.CLICK, zoom);
zoomOut.addEventListener(MouseEvent.CLICK, zoom);

function zoom(event:MouseEvent) {
    var scale:Number = event.currentTarget == zoomIn ? 1 : -1;
    sp.content.scaleX += scale;
    sp.content.scaleY += scale;
    sp.update();
}

然后我注意到你正在使用flex,所以肯定必须有一个允许你有类似功能的容器。

HTH, 乔治

答案 2 :(得分:0)

我也会使用scaleX和scaleY,但只使用数字而不是George的解决方案和var。所以,像

zoomInButton.addEventListener(MouseEvent.MOUSE_DOWN, zoomIn);
zoomOutButton.addEventListener(MouseEvent.MOUSE_DOWN, zoomOut);

function zoomIn(e:MouseEvent) {
  //actions for zoom-in function
  myPicture.scaleX += 10;
  myPicture.scaleY += 10;
}
function zoomOut(e:MouseEvent) {
  //actions for zoom-out function
  myPicture.scaleX += 10;
  myPicture.scaleY += 10;
}

你可能会偶然发现另一个障碍,即图片从左上角缩放到左上角。在这种情况下,尝试使用缩放移动图片。像

function zoomIn(e:MouseEvent) {
  //actions for zoom-in function
  myPicture.scaleX += 10;
  myPicture.scaleY += 10;
  myPicture.x -= 5;
  myPicture.y -= 5;
}

答案 3 :(得分:0)

实际上,如果你想缩放/缩放/缩小舞台或视频对象或其他任何东西,应该使用乘法和除法。像这样(这是我在需要为网络摄像头创建缩放/缩放功能时使用的 - 当然使用缩放功能进行数字缩放):

假设:


video_width = 640;
video_height = 480;

stage.scaleMode     = StageScaleMode.NO_SCALE; 
stage.align         = StageAlign.TOP_LEFT;
stage.stageWidth    = video_width;
stage.stageHeight   = video_height;


camera = Camera.getCamera();
// some other params

video = new Video( video_width, video_height );
video.attachCamera(camera);
addChild(video);

// to mirror webcam output:
video.scaleX = -1;
video.scaleY = 1;
video.x = video_width;
video.y = 0;


public function zoom(action:String = 'reset')
{
      var step = 1.1, // adjust this multiplyer for faster zoom. Eg. 1.2, 1.5, etc.
          x_offset = 0, 
          y_offset = 0;

    if (action == 'in')
    {
        video.scaleX *= step;
        video.scaleY *= step;

        x_offset = (stage.stageWidth - video.width)/2;
        y_offset = (stage.stageHeight - video.height)/2;
                // to center video object on stage by X if mirror effect is used
        video.x = stage.stageWidth - x_offset; 
                // to center video object on stage by X - no mirror
                // video.x = x_offset;
        video.y = y_offset; // to center video object on stage by Y
    }
    else if (action == 'out')
    {
            video.scaleX /= step;
        video.scaleY /= step;

        x_offset = (stage.stageWidth - video.width)/2;
        y_offset = (stage.stageHeight - video.height)/2;
        video.x = stage.stageWidth - x_offset;
        video.y = y_offset;
    }
    else
    {
                // need to be mirrored
        video.scaleX = -1; 
                // no mirror
                 // video.scaleX = 1;
        video.scaleY = 1;
                // if mirror video output
        video.x = stage.stageWidth;
               // no mirroring used
               // video.x = 0;
        video.y = 0;
    }       
}

现在通过将此功能附加到按钮来使用它:


zoom('in');
zoom('out'); 
zoom('reset'); // reset zoom