如何调整动态加载图像到闪存(as3)的大小

发布于 2024-09-25 01:21:24 字数 530 浏览 7 评论 0原文

我正在努力寻找正确的 as3 代码来调整图像的大小,一旦图像被动态调用到舞台并放置在 MC 中。 我正在加载使用:

var myLoader :Loader = new Loader(); 
mc.addChild(myLoader);
var url :URLRequest = new URLRequest("myimage.jpg"); 
myLoader .load(url );

舞台最终将打开为全屏(工作正常),因此我需要将图像保持其原始尺寸,该尺寸比舞台大得多。

我需要做的是在加载时将其缩小到与舞台相同的高度,同时保持宽度成比例(哦并将其居中)。 我已经尝试了各种代码,但找不到任何可以工作的东西,因为我所做的就是调整包含图像的 MC 的大小,而不是图像本身。 任何有关正确代码的指导将不胜感激。

我猜它就像这样简单,

 "myimage".x=600;

但如果是这样,编写图像名称的正确方法是什么,正如我所写的,它似乎是错误的。 非常感谢

艾德

Am struggling to find the right as3 code to resize an image once it is dynamically called into the stage and placed in a MC.
I am loading using:

var myLoader :Loader = new Loader(); 
mc.addChild(myLoader);
var url :URLRequest = new URLRequest("myimage.jpg"); 
myLoader .load(url );

The stage will eventually open up into fullscreen (works ok) so I need to keep the image in its original size which is much bigger than the stage.

What I need to do is shrink it on loading to the same height as the stage whilst keeping the width in proportion (oh and center it).
I have tried all sorts of codes but cant find anything to work as all I have managed to do is resize the MC containing the image but NOT the image itself.
Any guidance as to the correct code would be greatly appreciated.

I am guessng it is as simple as something like

 "myimage".x=600;

but if so what is the correct way to write the image name, as I have written it seems erroneous.
Many thanks

Ed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

后来的我们 2024-10-02 01:21:25

我也遇到过这样的问题,你可以试试这个......它对我有用

var my_loader:Loader = new Loader();
        my_loader.load(new URLRequest("underwater.jpg"));
        var holder:MovieClip=new MovieClip();
        my_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(){
my_loader.content.width = stage.stageWidth;
my_loader.content.height = stage.stageHeight;

I also had such problems, you can try this ..... it worked for me

var my_loader:Loader = new Loader();
        my_loader.load(new URLRequest("underwater.jpg"));
        var holder:MovieClip=new MovieClip();
        my_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(){
my_loader.content.width = stage.stageWidth;
my_loader.content.height = stage.stageHeight;
烛影斜 2024-10-02 01:21:24

我尝试回答你的问题

 import flash.display.MovieClip;
 import flash.display.Sprite;
 import flash.display.DisplayObject;
 import flash.display.Loader;
 import flash.display.LoaderInfo;
 import flash.events.Event;

   var myLoader:Loader = new Loader(); 
   var image:Bitmap;
   var url :URLRequest = new URLRequest("im1.jpg");
   myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
   myLoader.load(url);

   function onImageLoaded(e:Event):void {
      image = new Bitmap(e.target.content.bitmapData);
      var mw:Number = stage.stageWidth;
      var mh:Number = stage.stageHeight;   
      /* if you set width and height image same with the stage use this */
      image.width = mw;
      image.height = mh;
      mc.addChild(image);
   }

I try answer your questions

 import flash.display.MovieClip;
 import flash.display.Sprite;
 import flash.display.DisplayObject;
 import flash.display.Loader;
 import flash.display.LoaderInfo;
 import flash.events.Event;

   var myLoader:Loader = new Loader(); 
   var image:Bitmap;
   var url :URLRequest = new URLRequest("im1.jpg");
   myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
   myLoader.load(url);

   function onImageLoaded(e:Event):void {
      image = new Bitmap(e.target.content.bitmapData);
      var mw:Number = stage.stageWidth;
      var mh:Number = stage.stageHeight;   
      /* if you set width and height image same with the stage use this */
      image.width = mw;
      image.height = mh;
      mc.addChild(image);
   }
故笙诉离歌 2024-10-02 01:21:24

您必须等待图像加载,然后您可以调整加载程序的大小,例如:

package {
 import flash.display.MovieClip;
 import flash.display.Sprite;

 import flash.display.DisplayObject;
 import flash.display.Loader;
 import flash.display.LoaderInfo;
 import flash.events.Event;

 public class Test extends Sprite {

  public function Test(){
    super();

    if (stage) {
      init();
    } else {
     addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
    }
  }

  public var mc:MovieClip;

  private function init(e:Event=null):void{
   if (e!=null) {
     e.target.removeEventListener(e.type, arguments.callee);
   }

   mc = new MovieClip();
   addChild(mc);

   var myLoader:Loader = new Loader(); 
   mc.addChild(myLoader);

   // listen when the image is fully loaded
   myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);


   var url :URLRequest = new URLRequest("myimage.jpg"); 
   myLoader.load(url);
  }

  private function onImageLoaded(e:Event):void {
   var li:LoaderInfo = e.target as LoaderInfo;
   li.removeEventListener(e.type, arguments.callee);

   // resize the loader while preserving ratio
   var ldr:Loader=li.loader;
   var mw:Number = stage.stageWidth / ldr.width;
   var mh:Number = stage.stageHeight / ldr.height;

   var ratio:Number = (mw < mh) ? mw : mh;

   ldr.width *= ratio;  // you can also use ldr.scaleX=ratio;
   ldr.height *= ratio; // you can also use ldr.scaleY=ratio;

   // center mc on stage
   mc.x=0.5*(stage.stageWidth-mc.width);
   mc.y=0.5*(stage.stageHeight-mc.height);
  }
 }
}

You have to wait the image is loaded and then you can resize your loader for example:

package {
 import flash.display.MovieClip;
 import flash.display.Sprite;

 import flash.display.DisplayObject;
 import flash.display.Loader;
 import flash.display.LoaderInfo;
 import flash.events.Event;

 public class Test extends Sprite {

  public function Test(){
    super();

    if (stage) {
      init();
    } else {
     addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
    }
  }

  public var mc:MovieClip;

  private function init(e:Event=null):void{
   if (e!=null) {
     e.target.removeEventListener(e.type, arguments.callee);
   }

   mc = new MovieClip();
   addChild(mc);

   var myLoader:Loader = new Loader(); 
   mc.addChild(myLoader);

   // listen when the image is fully loaded
   myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);


   var url :URLRequest = new URLRequest("myimage.jpg"); 
   myLoader.load(url);
  }

  private function onImageLoaded(e:Event):void {
   var li:LoaderInfo = e.target as LoaderInfo;
   li.removeEventListener(e.type, arguments.callee);

   // resize the loader while preserving ratio
   var ldr:Loader=li.loader;
   var mw:Number = stage.stageWidth / ldr.width;
   var mh:Number = stage.stageHeight / ldr.height;

   var ratio:Number = (mw < mh) ? mw : mh;

   ldr.width *= ratio;  // you can also use ldr.scaleX=ratio;
   ldr.height *= ratio; // you can also use ldr.scaleY=ratio;

   // center mc on stage
   mc.x=0.5*(stage.stageWidth-mc.width);
   mc.y=0.5*(stage.stageHeight-mc.height);
  }
 }
}
全部不再 2024-10-02 01:21:24

试试这个代码:

img1_mc.scaleX = (stage.stageWidth/img1_mc.width)
img1_mc.scaleY = (stage.stageHeight/img1_mc.height)

Try this code:

img1_mc.scaleX = (stage.stageWidth/img1_mc.width)
img1_mc.scaleY = (stage.stageHeight/img1_mc.height)
知足的幸福 2024-10-02 01:21:24

您还可以尝试
的 Loader 类
http://www.greensock.com/loadermax/

他们在简化和简化方面做得很好。简化加载过程,除此之外还有一些不错的福利。

检查ImageLoader类
http://www.greensock.com/as/docs /tween/com/greensock/loading/ImageLoader.html

特别是如何在加载之前设置一堆可选的 vars 属性。

这是文档中给出的示例的一小段摘录......

//create an ImageLoader:
 var loader:ImageLoader = new ImageLoader("img/photo1.jpg", 
 //here's the sweet part :)
 {name:"photo1", 
 container:this, 
 x:180, 
 y:100, 
 width:200, //here you could set the stageWidth for instance
 height:150, //and the stageHeight here
 scaleMode:"proportionalInside", //this one will definitely help you , check the docs!
 centerRegistration:true, 
 onComplete:onImageLoad});

You could also try the Loader classes at
http://www.greensock.com/loadermax/

They do a great job of simplifying & streamlining the loading process , with a few nice perks on top of it all.

Check the ImageLoader class
http://www.greensock.com/as/docs/tween/com/greensock/loading/ImageLoader.html

particularly how you can set a bunch of optional vars properties before loading.

Here's a small extract from the example given in the docs...

//create an ImageLoader:
 var loader:ImageLoader = new ImageLoader("img/photo1.jpg", 
 //here's the sweet part :)
 {name:"photo1", 
 container:this, 
 x:180, 
 y:100, 
 width:200, //here you could set the stageWidth for instance
 height:150, //and the stageHeight here
 scaleMode:"proportionalInside", //this one will definitely help you , check the docs!
 centerRegistration:true, 
 onComplete:onImageLoad});

夏见 2024-10-02 01:21:24

我在上面的代码中遇到了很多错误,显然是说不能使用嵌套的公共类或其他东西,所以我删除了包和公共函数,并且我让它与这段代码一起工作,

     import flash.display.MovieClip;
 import flash.display.Sprite;
 import flash.display.DisplayObject;
 import flash.display.Loader;
 import flash.display.LoaderInfo;
 import flash.events.Event;
   var myLoader:Loader = new Loader(); 
   mc.addChild(myLoader);
   myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
   var url :URLRequest = new URLRequest("im1.jpg"); 
   myLoader.load(url);
function onImageLoaded(e:Event):void {
   var bit:Bitmap = e.target.content;
    if(bit != null)
    bit.smoothing = true;
   var li:LoaderInfo = e.target as LoaderInfo;
   li.removeEventListener(e.type, arguments.callee);
   var ldr:Loader=li.loader;
   var mw:Number = stage.stageWidth / ldr.width;
   var mh:Number = stage.stageHeight / ldr.height;
   var ratio:Number = (mw < mh) ? mw : mh;
   ldr.width *= ratio;  // you can also use ldr.scaleX=ratio;
   ldr.height *= ratio; // you can also use ldr.scaleY=ratio;
   mc.x=0.5*(stage.stageWidth-mc.width);
   mc.y=0.5*(stage.stageHeight-mc.height);
  }

甚至在其中添加了一些平滑。
我需要学习,因为我不确定公共职能和私人职能之间有什么区别?

另一个问题是,如果图像与加载图像的 swf 位于同一文件夹中,我可以成功动态加载图像,但如果我尝试将图像保存在单独的文件中并使用

var url:URLRequest 之类的内容调用它们...等(“剧照/image1.jpg”);他们只是不会加载。看出什么不对了吗??

帮助

再次非常感谢埃德的

I had loads of errors with the above code, apparently said cannot use nested public class or soemthing, so I removed the package and public functions and I got it working with this code

     import flash.display.MovieClip;
 import flash.display.Sprite;
 import flash.display.DisplayObject;
 import flash.display.Loader;
 import flash.display.LoaderInfo;
 import flash.events.Event;
   var myLoader:Loader = new Loader(); 
   mc.addChild(myLoader);
   myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
   var url :URLRequest = new URLRequest("im1.jpg"); 
   myLoader.load(url);
function onImageLoaded(e:Event):void {
   var bit:Bitmap = e.target.content;
    if(bit != null)
    bit.smoothing = true;
   var li:LoaderInfo = e.target as LoaderInfo;
   li.removeEventListener(e.type, arguments.callee);
   var ldr:Loader=li.loader;
   var mw:Number = stage.stageWidth / ldr.width;
   var mh:Number = stage.stageHeight / ldr.height;
   var ratio:Number = (mw < mh) ? mw : mh;
   ldr.width *= ratio;  // you can also use ldr.scaleX=ratio;
   ldr.height *= ratio; // you can also use ldr.scaleY=ratio;
   mc.x=0.5*(stage.stageWidth-mc.width);
   mc.y=0.5*(stage.stageHeight-mc.height);
  }

Even added some smoothing in there.
I need to study ´cause Im not sure what the difference is between public and private functions?

One further problem guys, I can successfuly dynamically load images if they are in the same folder as the swf that is loading them but if I try to hold the images in a seperate file and call them with something like

var url:URLRequest...etc ("stills/image1.jpg"); they just wont load. See anything wrong ??

Again MANY THANKS for the assistance

Ed

满地尘埃落定 2024-10-02 01:21:24

您需要添加一个侦听器来侦听正在加载的内容的 INIT 事件。

而且您不需要调整 image.jpg 文件本身的大小(并且您尝试解决该问题的方式不起作用)。您可以改为调整加载程序的大小。

如果您查看 LoaderInfo 的 AS3 文档,您会在末尾找到一个示例,该示例展示了如何添加侦听器。 (请注意,侦听器被添加到 contentLoaderInfo 对象,而不是 Loader 本身)。

然后,您可以使用事件处理程序来调整大小。 AS3 文档在 initHandler 中显示了一个示例,该示例仅跟踪一些内容。如果你做了 loader.x = 600 那么你就会得到你需要的东西。

you need to add a listener to listen for the INIT event on what you're loading.

And you don't need to resize the image.jpg file itself (and the way you're trying to address it won't work). You can instead resize the loader.

If you check out the AS3 Docs for LoaderInfo you'll find an example at the end that shows how to add the listeners. (Note that the listener is added to the contentLoaderInfo object, not the Loader itself).

You can then use the event handler to do the resize. The AS3 Docs show an example in the initHandler that just trace some stuff out. If you did loader.x = 600 then you'd get what you need.

樱娆 2024-10-02 01:21:24

我只是在查找如何调整图像大小,发现了这个方便的库 http://jacwright.com/blog/221/high-quality-high-performance-thumbnails-in-flash/

当 Flash 缩小图像尺寸时,将其尺寸调整一半时,其质量最佳然后继续调整大小,直到达到所需的大小

该库为您完成了所有这些

该网站有一些示例,并且指向他的谷歌代码页的链接

可能不完全是您想要的,但它确实工作得很好

I was just looking up how to resize an image and I found this handy library http://jacwright.com/blog/221/high-quality-high-performance-thumbnails-in-flash/

when flash downsizes an image it has the best quality when you resize it by half then keep resizing by half until you get to the desired size

the library does all that for you

the website has some examples and links to his google code page

might not be exactly what you want, but it does work pretty good

鱼窥荷 2024-10-02 01:21:24

我为此苦苦挣扎了一段时间,但如果你使用的是 AS3,只需将 uiloader 对象拖到舞台上并将其源设置为 true,最重要的是,将 scaleContent 属性设置为 true...

这对我来说效果很好,并且需要更少代码:

uiloader.autoLoad = true;
uiloader.scaleContent = true;
uiloader.source = "photos/yourphoto.jpg";

I struggled with this for a while folks, but if you're using AS3, just drag a uiloader object onto the stage and set it's source and most importantly, scaleContent property to true....

This works just fine for me and requires less code:

uiloader.autoLoad = true;
uiloader.scaleContent = true;
uiloader.source = "photos/yourphoto.jpg";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文