Archive

Posts Tagged ‘saling dynamic images in flash’

Scaling a dynamic background image in proportion using flash

August 13th, 2009 11 comments

Recently I was playing around with scaling background images and had issues with how the scaling behaved and image quality.  When scaling an image in proportion you want to make sure that the image container is centered to the movie clip it belongs to.

My example below is the scaling code within a stage listener which will re size the background image according to the stage width & height.

  1. Stage.align = "TL";
  2. Stage.scaleMode = "noScale";
  3.  
  4. //Get Background Image Container Movie Clip Name (instance)
  5. var backgroundImage:movieClip = "MovieClip Instance Name"
  6.  
  7. // Create an object for the stage listener
  8. stageListener:Object = new Object();
  9.  
  10. // Create a function for the event you want to listen for
  11. stageListener.onResize = function() {
  12.  
  13. //Get the  stage width & height
  14. var sw:Number = Stage.width;
  15. var sh:Number = Stage.height;
  16.  
  17. //Check to see if image width or height needs adjusting
  18. if ((sh / sw) > (backgroundImage._height / backgroundImage._width)) {
  19.  
  20. //get image width and adjust height to fit
  21. scale = backgroundImage._width / backgroundImage._height;
  22. backgroundImage._height = sh;
  23. backgroundImage._width = sh * scale;
  24. } else {
  25.  
  26. //get image height and adjust width to fit
  27. scale =backgroundImage._height / backgroundImage._width;
  28. backgroundImage_width = sw;
  29. backgroundImage._height = sw * scale;
  30. }
  31.  
  32. //Center the background Image
  33. backgroundImage._x = (sw - backgroundImage._width) / 2;
  34. backgroundImage._y = (sh - backgroundImage._height) / 2;
  35.  
  36. };
  37. // Add listener for the Stage object
  38. Stage.addListener(stageListener);

Now the the simple line of code that makes this work so well is the quality preservation part. Just add this after your dynamic image has loaded.

  1. backgroundImage.forceSmoothing = true;

An actionscript 3 version would not be much different to this and should be fairly easy to workout. If your struggling just let me know.

Buy great image scaling software after you learn the roulette slang and online roulette history you need to beat roulette at CasinoBonus.org!

SEO Powered by Platinum SEO from Techblissonline