2010-10-22 103 views
1

我有一個Flash文件,用於加載透明PNG(線條圖),並使用Flash來允許用戶選擇繪圖的顏色,並立即更改顏色。如何在Flash中反動態加載動態加載的PNG

這是所有的工作,它應該是。我使用UILoader組件來加載通過flashvars接收的圖像,然後根據需要更改顏色。

唯一的事情是,圖像有時看起來不是很平滑。我看過各種文章,人們談論在位圖上使用smoothing = true。但我不確定在哪裏設置它,或者它如何工作。

我是Flash和ActionScript的新手,任何人都可以幫助我嗎?以下是我的代碼(main.as)。我遺漏了changeColor()和getFlashvars()函數以及導入行。因爲我認爲這並不重要,並且儘可能保持簡單和簡短。

非常感謝任何幫助和或指針,你可以給我!

package { 
public class Main extends MovieClip { 
    public function Main() { 
     init(); 

     if(getFlashvars().img != undefined) { 
      if(getFlashvars().clr != undefined) { 
       loadImage(getFlashvars().img, getFlashvars().clr); 
      } else { 
       loadImage(getFlashvars().img); 
      } 
     } else { 
      loadImage('example.png', 'FFFFFF'); 
     } 
    } 

    private function init() { 
     ExternalInterface.addCallback('changeColor', this.changeColor); 

     progressBar.visible = false; 
     progressBar.mode = 'manual'; 
     progressBar.source = uiLoader; 

     uiLoader.scaleContent = true; 
     uiLoader.addEventListener(ProgressEvent.PROGRESS, eventImageProgress); 
     uiLoader.addEventListener(Event.COMPLETE, eventImageLoaded); 
    } 

    /** 
    * Load a new image from the given url. Replace the color in it with newColor 
    */ 
    public function loadImage(url:String, newColor:String='') { 
     //progressBar.visible = true; 
     uiLoader.load(new URLRequest(url)); 

     if(newColor.length > 0) { 
      changeColor(newColor); 
     } 
    } 
} 

回答

3

as stated clearly in the documentation,平滑是一個位圖的公共屬性。

myBitmap.smoothing = true; 

調用一個新的位圖的構造

var myBitmap:Bitmap = new Bitmap(myBitmapData, PixelSnapping.AUTO, true); 
         //where the last boolean represents the smoothing property 

,你也可以嘗試你的顯示對象上設置一個光模糊濾鏡,如果你覺得平滑不夠時,也可以設置。

0

我在Flex中做的是創建一個新組件,如果可能的話,它會在其內部位圖上設置平滑屬性。類似的東西可能在Flash中可行。

public class SmoothImage extends Image 
{ 

    override protected function updateDisplayList(w:Number, h:Number):void 
    { 
     super.updateDisplayList(w, h); 
     if (content is Bitmap) 
     { 
     var bitmap : Bitmap = content as Bitmap; 

     if (bitmap != null && 
      bitmap.smoothing == false) 
     { 
      bitmap.smoothing = true; 
     } 
     } 
    } 
} 

然後你可以使用它像:

image = new SmoothImage(); 
image.source = url; 

可能有助於!主要想法是訪問位圖組件本身來設置平滑屬性,無論是在您的文件還是單獨的組件中發生,這就是您需要執行的操作。

0

如果您在使用此平滑代碼時遇到問題,則僅適用於從相同域或已將您添加到其crossdomain.xml文件的域中獲取圖像的情況。

在你完成處理程序中,你可以做這樣的事情

// First, you need to copy the loaded image. 
var displayObject :DisplayObject = uiLoader.content; 
var bitmapData :BitmapData = new BitmapData(displayObject.width, displayObject.height); 
bitmapData.draw(displayObject); 

// Then create a new image from the loaded image. 
var bitmap :Bitmap = new Bitmap(bitmapData); 
bitmap.smoothing = true; // This is what actually does the anti-aliasing. 

addChild(bitmap); 
0

但我安靜不知道在哪裏設置 這個

我要說的是,在最簡單的形式,你可以在Event.COMPLETE的處理程序的加載圖像(位圖)上將平滑設置爲true,因此eventImageLoaded就是您的情況。

事情是這樣的例子:

var loader:Loader = new Loader(); 
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded); 
loader.load(new URLRequest("http://www.google.com/images/logos/ps_logo.png")); 

function imageLoaded(e:Event):void { 
    e.target.content.smoothing = true; 
    // Or Bitmap(e.target.content).smoothing = true; if you prefer. 
} 
+0

謝謝,這樣的事情是什麼,我期待的!我試過了,只設置了imageLoaded()中的任一行。因爲我已經通過UILoader加載了圖像。但這似乎不起作用,但也不會給出任何錯誤。有沒有可能在UILoader組件上設置它?或者我必須使用不同的組件來加載圖像? - 如果可能,我想繼續使用UILoader,因爲它也會自動爲我調整圖像大小。 – 2010-10-24 07:25:57

2

幾乎沒有拉斯,解決的辦法是

Bitmap(UILoaderTarget.content).smoothing = true;