2011-09-05 57 views

回答

1

的BitmapData有scroll()方法,你可以使用的最好的方法 - http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#scroll()

基本上每一幀,你只畫像素的價值的數據的一列(你的BitmapData將是查看你的聲音,並且每列像素是一次捕獲的數據) - 大多數情況下,你可能只會使用setPixel()來設置音量值。

繼續前進,一幀數據的一列像素,然後當你達到你的閾值(BitmapData的右側)時,使用scroll()將數據移動一個像素(你會丟失最左側的像素列),然後繼續在像素的最後一列(右側)繪圖。您將創建一個不斷更新的音量顯示。

最後使用lock()unlock()之前和之後繪製停止更新屏幕Flash的同時。

我建議卻有着某種限制 - 即成長無限=大問題後

編輯

下面是一些代碼項目。這花了我0ms(在調試播放器上使用getTimer())來渲染。你最終會遇到問題,因爲卷矢量增長無限,這從來都不是好事。如果你只需要保留最後的x幀,那麼這段代碼更容易。

package 
{ 
    import flash.display.Bitmap; 
    import flash.display.BitmapData; 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.utils.getTimer; 

    public class Main extends Sprite 
    { 
     private var m_bmd:BitmapData = null; 
     private var m_vol:Vector.<int> = null; 
     private var m_currVol:int  = 0; 

     public function Main():void 
     { 
      // create our bmd 
      this.m_bmd = new BitmapData(400.0, 200.0, false, 0); 

      // add it to the stage 
      var b:Bitmap = new Bitmap(this.m_bmd); 
      this.addChild(b); 

      // create our volume array 
      this.m_vol = new Vector.<int>(); 

      // set our current volume 
      this.m_currVol = this.m_bmd.height/2; 

      // start our volume listener 
      this.addEventListener(Event.ENTER_FRAME, this._onEnterFrame); 
     } 

     private function _onEnterFrame(e:Event):void 
     { 
      var start:int = getTimer(); 

      // update our volume and add it to our array 
      this.m_currVol += int((Math.random() * 10.0) - 5.0); // adds a random int between -5 and 5 
      this.m_currVol = (this.m_currVol < 0) ? 0 : (this.m_currVol > this.m_bmd.height) ? this.m_bmd.height : this.m_currVol; // clamp it to the bitmap data height 
      this.m_vol[this.m_vol.length] = this.m_currVol; 

      // lock our bitmap 
      this.m_bmd.lock(); 

      // clear the bitmap 
      this.m_bmd.fillRect(this.m_bmd.rect, 0); 

      // go through and draw our volumes (for max the size of the bmd) 
      var len:int  = (this.m_vol.length < this.m_bmd.width) ? this.m_vol.length : this.m_bmd.width; 
      var startX:int = (this.m_vol.length < this.m_bmd.width) ? 0 : this.m_vol.length - this.m_bmd.width; 
      for (var i:int = 0; i < len; i++) 
       this.m_bmd.setPixel(i, this.m_vol[startX + i], 0xffff00); 

      // unlock our bitmap 
      this.m_bmd.unlock(); 

      trace("This took " + (getTimer() - start) + "ms to render"); 
     }  

    } 

} 

添加一個滾動條來改變startX變量(默認情況下,它會始終顯示數據的最後量),以及你在那裏

+0

Divilly嗨, 感謝您抽出寶貴的時間來解釋,但我需要的是一個可滾動的數據,即我不想丟失左側的數據,而是想要將其屏蔽掉,並且一旦滾動條越過指定的寬度就會看到滾動條,所以在任何點時間,人們可以滾動到左側,看到整個圖表。至少這對我來說是一個挑戰。 – mobdev999

+0

在這種情況下,只需保留一個卷的數字矢量。從矢量的末尾繪製數據vis的(end - numItems),並使用滾動條(不連接任何東西 - 只需要該值)以確定該起點(如果需要)。 – divillysausages

+0

命中cpu ... – mobdev999