使用as3调整图像大小
我正在构建一个投资组合网站,当用户单击缩略图时,该网站会动态加载大型高分辨率图像。但是,在调整原始图像的大小以适应当前浏览器时,我在保持原始图像的质量方面遇到了问题。
目前我只是调整宽度和宽度图像的高度属性与舞台的宽度成比例,这可以很好地使其适合,但是我不知道如何保持图像质量?我是否需要将其加载为位图并重绘/平滑或类似的东西?
这是我当前正在使用的适当代码:
var req:URLRequest = new URLRequest("images/101.jpg");
var loader:Loader = new Loader();
var widthRatio:Number;
var heightRatio:Number;
function imageLoaded(e:Event):void
{
//add image to stage
addChild(loader);
// resize proportionally
if (loader.width > stage.stageWidth){
widthRatio=loader.width/stage.stageWidth;
trace(widthRatio)
trace(loader.width);
}
if (loader.height > stage.stageHeight){
heightRatio=loader.height/stage.stageHeight;
trace(heightRatio)
trace(loader.height)
}
if (widthRatio > heightRatio){
loader.width = stage.stageWidth;
loader.height = loader.height/widthRatio;
} else {
loader.height = stage.stageHeight;
loader.width = loader.width/heightRatio;
}
//centre on stage
loader.x=stage.stageWidth/2 - loader.width/2;
loader.y=stage.stageHeight/2 - loader.height/2;
}
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
loader.load(req);
I am building a portfolio site which dynamically loads large hi-res images when the user clicks on a thumnail. However I'm having problems maintaining the original image's quality when resizing it to fit the current browser.
Currently I'm just adjusting the width & height properties of the image to width to the stage proportionally which is working fine getting it to fit, however I cannot figure out how to maintain image quality? Do I need to load it as a bitmap and redraw/smooth or something similar?
This is the appropriate code that I am currently using:
var req:URLRequest = new URLRequest("images/101.jpg");
var loader:Loader = new Loader();
var widthRatio:Number;
var heightRatio:Number;
function imageLoaded(e:Event):void
{
//add image to stage
addChild(loader);
// resize proportionally
if (loader.width > stage.stageWidth){
widthRatio=loader.width/stage.stageWidth;
trace(widthRatio)
trace(loader.width);
}
if (loader.height > stage.stageHeight){
heightRatio=loader.height/stage.stageHeight;
trace(heightRatio)
trace(loader.height)
}
if (widthRatio > heightRatio){
loader.width = stage.stageWidth;
loader.height = loader.height/widthRatio;
} else {
loader.height = stage.stageHeight;
loader.width = loader.width/heightRatio;
}
//centre on stage
loader.x=stage.stageWidth/2 - loader.width/2;
loader.y=stage.stageHeight/2 - loader.height/2;
}
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
loader.load(req);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看位图平滑。
您的
Loader
将有一个 content 属性 它将是一个位图
。将其smoothing
设置为 true。它使图像有点模糊,但在大多数情况下这是一个显着的改进。
Have a look at Bitmap smoothing.
Your
Loader
will have a content property which will be aBitmap
. Set it'ssmoothing
to true.It makes the image a bit blurry but it is a significant improvement in most cases.