簡體   English   中英

使用as3調整圖像大小

[英]Resizing image using as3

我正在構建一個投資組合網站,當用戶單擊縮略圖時,該網站會動態加載大型高分辨率圖像。 但是,在調整原始圖像的大小以適合當前瀏覽器時,我在保持原始圖像的質量方面遇到問題。

目前,我只是按比例調整圖像的width和height屬性,使其達到舞台的寬度,這樣可以很好地適應它,但是我不知道如何保持圖像質量? 我是否需要將其加載為位圖並重繪/平滑或類似內容?

這是我當前正在使用的適當代碼:

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);

看一下位圖平滑

您的Loader將具有content屬性 ,該屬性將是Bitmap 將其smoothing設置為true。

function imageLoaded(e:Event):void
{
    //add image to stage
    addChild(loader);
    Bitmap(loader.content).smoothing = true;

    // ...

它使圖像有點模糊,但是在大多數情況下是一個很大的改進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM