Flash photo Album loading picture complete and so on than scaling the class code

Source: Internet
Author: User

Code and Comment Description:

/*
Scaling by equal ratio
as1984-qq:38657783
20091221
Please note the package path and my as package is placed under the tools directory. So the package path is tools
If your path is not the same, please note that the change
*/
Package Tools
{
public class Imgzoom {

Variable declaration
Private Var iszoom:boolean;//whether scaling
Private Var srcwidth:number;//original width
Private Var srcheight:number;//original high
Private Var maxwidth:number;//limited width
Private Var maxheight:number;//limit high
Private var newwidth:number;//new width
Private var newheight:number;//new record

Public Function Imgzoom (srcwidth:number,srcheight:number,maxwidth:number,maxheight:number):
void
{
this.srcwidth=srcwidth;//Get original width
this.srcheight=srcheight;//get the original height
this.maxwidth=maxwidth;//Get a limited width
this.maxheight=maxheight;//Get a limited height
if (this.srcwidth>0 && this.srcwidth>0) {//Check picture height is normal
this.iszoom=true;//width Normal, perform zoom processing
}else{
this.iszoom=false;//not normal, return 0
}
CONDUCTIMG ()//Execute scaling algorithm
}
Public function width (): number{//Returns the width of the processing, accurate to 2 decimal points
Return Number (This.newWidth.toFixed (2));
}
Public Function height (): number{//Returns the height of the processing, accurate to 2 decimal points
Return Number (This.newHeight.toFixed (2));
}
Private Function Conductimg (): void{
if (this.iszoom) {//If width is normal, start calculation
if (this.srcwidth/this.srcheight>=this.maxwidth/this.maxheight) {
Compare the height-width ratio to determine whether the calculation is based on a wide or high base.
if (this.srcwidth>this.maxwidth) {///is based on a wide base,
When width is greater than the limit width, start scaling
This.newwidth=this.maxwidth;
this.newheight= (this.srcheight*this.maxwidth)/this.srcwidth
}else{
Returns the original value directly when the width is less than the specified width.
This.newwidth=this.srcwidth;
This.newheight=this.srcheight;
}
}else{
if (this.srcheight>this.maxheight) {//is calculated with a high base
When the height is greater than the specified height, start scaling.
This.newheight=this.maxheight;
This.newwidth= (this.srcwidth*this.maxheight)/this.srcheight
}else{
When the height is less than the specified height, the original value is returned directly.
This.newwidth=this.srcwidth;
This.newheight=this.srcheight;
}
}
}else{//not normal, return 0
this.newwidth=0;
this.newheight=0;
}
}
}
}

application Example:

Import Tools.imgzoom;
function when the load is complete {
var t:sprite=new Sprite ();
var w:number=685;//-Qualified width
var h:number=450;//-defined Height
var t:sprite=new Sprite ()//create something to install a picture
var myzoom:imgzoom=new imgzoom (E.TARGET.CONTENT.WIDTH,E.TARGET.CONTENT.HEIGHT,W,H);//instantiation algorithm
E.target.content.width=myzoom.width ()//The width of the loaded things
E.target.content.height=myzoom.height ()//To get the loaded things height
T.addchild (e.target.content)//join T display list

}

File Download: Imgzoom.rar

It is recommended to use BitmapData combination matrix to deal with this problem, directly to the need to display the image data (more save resources), you can not frequently set the coordinates and scaling, the code will be much less.

Post Key code:

public static function Getzoomdraw (Targetisplayobject, Tarw:int, tarh:int,full:boolean=true): BitmapData {
Get the Display object rectangle range
var rect:rectangle = target.getbounds (target);
Calculate the proportions that should be scaled
var perw = tarw/rect.width;
var perh = tarh/rect.height;
var scale = full? ((perw <= perh) perwerh) (Perw <= Perh) perherw);
Calculates the offset between scaling and a specified size
var offerw = (Tarw-rect.width * scale)/2;
var offerh = (Tarh-rect.height * scale)/2;
Start drawing a snapshot (here the transparent parameter is false, is convenient to observe, the actual application can be changed to true)
var bmd:bitmapdata = new BitmapData (TARW, Tarh, False, 0);
var Matrix:matrix = new Matrix ();
Matrix.scale (scale, scale);
Matrix.translate (Offerw, Offerh);
Bmd.draw (target, matrix);
If it is a bitmap object, release the bitmap resource
if (target is Bitmap) (target as Bitmap). Bitmapdata.dispose ();
Back to screenshot data
return to BMD;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.