RSS
email

Resize image Dynamically(proportionally) in flash as2/as3



1)Open new document in flash(ctrl+N)

2)Select first frame, right-click and paste the below code in first frame

3)Code contains explanations commented
//variable declarations to hold the values
var thisWidth:Number;
var thisHeight:Number;
var maximumHeight:Number;//height to which movieclip to be resized
var maximumWidth:Number;//width to which movieclip to be resized
var oldx:Number;
var oldy:Number;
var ratio:Number;
var mclis:Object = new Object();//An object that listens for a callback notification from the MovieClipLoader event handlers.


mclis.onLoadInit = function(target_mc:MovieClip) {//Invoked when the actions on the first frame of the loaded clip have been executed
_root.thisHeight = target_mc._height;//loaded movieclip height
_root.maximumHeight = 100;
_root.thisWidth = target_mc._width;//loaded movieclip width
_root.maximumWidth = 100;
ratio = thisHeight/thisWidth;//calculation ratio to which resize takes place

if (thisWidth>maximumWidth) {
thisWidth = maximumWidth;
thisHeight = Math.round(thisWidth*ratio);
}
if (thisHeight>maximumHeight) {
thisHeight = maximumHeight;
thisWidth = Math.round(thisHeight/ratio);
}
target_mc._width = thisWidth;//applying new width
target_mc._height = thisHeight;//applying new height
target_mc._x = 10;
target_mc._y = 10;
};
var mcl:MovieClipLoader = new MovieClipLoader();//MovieClipLoader class lets you implement listener callbacks that provide status information while SWF, JPEG, GIF, and PNG files are being loaded into movie clips.
mcl.addListener(mclis);//add the object as listener for event handlers
_root.createEmptyMovieClip("holder_mc", _root.getNextHighestDepth());//holder movieclip
_root.mcl.loadClip("http://www.atpm.com/11.02/nature/images/blue-flower.jpg", _root.holder_mc);//You can use the loadClip() method to load one or more files into a single movie clip or level;


__________________________________________________________________

Actionscript3.0

Resize.as
************

/**Do not delete the lines below
* @author: Saravanan
* @url: http://www.sara-intop.blogspot.com.com
* ---------------------------------------------------------------------
* @usage: Resize the assets dynamically
* ---------------------------------------------------------------------
*/
package com.classes{
import flash.display.MovieClip;
public class Resize extends MovieClip {
/*Constant variables-----------*/
/*Private variables-----------*/
private var _thisWidth:Number;
private var _thisHeight:Number;
private var _maximumHeight:Number;
private var _maximumWidth:Number;
private var _ratio:Number;
/*Public variables-----------*/
public var __height:int;
public var __width:int;

/**
* Constructor Function Resize
* Calculating the Height and Width from input values against Maximum height and Width
* @param thisWidth representing orginal height of the Object
* @param thisHeight representing orginal width of the Object
* @param maximumHeight||maximumWidth is the bound of the result
*/
public function Resize(thisWidth:int,thisHeight:int,maximumHeight:int,maximumWidth:int) {
this._thisWidth = thisWidth;
this._thisHeight = thisHeight;
this._maximumHeight = maximumHeight;
this._maximumWidth = maximumWidth;
this._ratio = this._thisHeight/ this._thisWidth;
calculate()
}
private function calculate() {

if ( this._thisWidth>this._maximumWidth) {
this._thisWidth = this._maximumWidth;
this._thisHeight = Math.round( this._thisWidth*this._ratio);
}
if ( this._thisHeight>this._maximumHeight) {
this._thisHeight = this._maximumHeight;
this._thisWidth = Math.round(this._thisHeight/this._ratio);
}
this.__height=this._thisHeight;
this.__width=this._thisWidth;
}
}
}


Usage

import com.classes.Resize;
var _resize:Resize=new Resize(_material_mc.width,_material_mc.height,300,200);


access the result height and width by
_resize.__width
_resize.__height


Related posts

Preloading in FlashCS3

3D rotation of Images using FlashCS3:

Bookmark and Share

6 comments:

kadarkarai said...

Thank you friend it is very nice onw

neeraj said...

how to resize image dynamically in a flash slideshow
2 suppose image is small then it come to center.

is it posible

Aaron said...

thanks dude. you have gotten me one step closer to my super slideshow.

Rauf said...

I am loading image using xml using actionscript2. I have tried your code but not working. Can you tell me please how can i do that.

Jobz said...

its very useful

 

Recent Posts

Recent Visitors

Donate Me

About Me

My photo
Chennai, Tamil nadu, India
Nothing more to say about me.. Just a Action Script programmer / Flex developer having 4.5 years of experience.