How to resize dynamically loaded image into flash

2019-02-14 19:09发布

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

9条回答
萌系小妹纸
2楼-- · 2019-02-14 19:30

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;
查看更多
看我几分像从前
3楼-- · 2019-02-14 19:32

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

查看更多
Viruses.
4楼-- · 2019-02-14 19:33

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);
  }
 }
}
查看更多
ゆ 、 Hurt°
5楼-- · 2019-02-14 19:36

Try this code:

img1_mc.scaleX = (stage.stageWidth/img1_mc.width)
img1_mc.scaleY = (stage.stageHeight/img1_mc.height)
beautiful°
6楼-- · 2019-02-14 19:37

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";
查看更多
走好不送
7楼-- · 2019-02-14 19:38

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);
   }
查看更多
登录 后发表回答