수동 비트맵 캐싱

BitmapData 클래스를 통해 사용자 정의 비트맵 캐싱 비헤이비어를 만듭니다.

다음 예제에서는 표시 객체의 래스터화된 단일 비트맵 버전을 사용하고 같은 BitmapData 객체를 참조합니다. 각 표시 객체의 크기를 조절하는 경우 메모리의 원래 BitmapData 객체가 업데이트되지 않고 다시 그려지지 않습니다. 이 방법은 CPU 리소스를 절약하고 응용 프로그램 실행 속도를 향상시킵니다. 표시 객체의 크기를 조절하면 포함된 비트맵이 확대됩니다.

다음은 업데이트된 BitmapApple 클래스입니다.

package org.bytearray.bitmap 
{ 
    import flash.display.Bitmap; 
    import flash.display.BitmapData; 
    import flash.events.Event; 
     
    public class BitmapApple extends Bitmap 
    { 
        private var destinationX:Number; 
        private var destinationY:Number; 
         
        public function BitmapApple(buffer:BitmapData) 
        { 
            super(buffer); 
 
            addEventListener(Event.ADDED_TO_STAGE,activation); 
            addEventListener(Event.REMOVED_FROM_STAGE,deactivation);     
        } 
         
        private function activation(e:Event):void  
        { 
            initPos();     
            addEventListener(Event.ENTER_FRAME,handleMovement); 
        } 
         
        private function deactivation(e:Event):void  
        { 
            removeEventListener(Event.ENTER_FRAME,handleMovement);     
        } 
         
        private function initPos():void 
        { 
            destinationX = Math.random()*(stage.stageWidth - (width>>1)); 
            destinationY = Math.random()*(stage.stageHeight - (height>>1)); 
        } 
         
        private function handleMovement(e:Event):void  
        {             
            alpha = Math.random(); 
             
            x -= (x - destinationX)*.5; 
            y -= (y - destinationY)*.5; 
             
            if ( Math.abs(x - destinationX) < 1 && Math.abs(y - destinationY) < 1) 
                initPos(); 
        } 
    } 
}

알파 값은 각 프레임에서 계속 수정됩니다. 다음 코드에서는 각 BitmapApple 인스턴스에 원본 소스 버퍼를 전달합니다.

import org.bytearray.bitmap.BitmapApple; 
 
const MAX_NUM:int = 100; 
var holder:Sprite = new Sprite(); 
 
addChild(holder); 
 
var holderVector:Vector.<BitmapApple> = new Vector.<BitmapApple>(MAX_NUM, true); 
var source:AppleSource = new AppleSource(); 
var bounds:Object = source.getBounds(source); 
     
var mat:Matrix = new Matrix(); 
mat.translate(-bounds.x,-bounds.y); 
 
var buffer:BitmapData = new BitmapData(source.width+1, source.height+1, true, 0); 
buffer.draw(source,mat); 
     
var bitmapApple:BitmapApple; 
     
for (var i:int = 0; i< MAX_NUM; i++) 
{ 
    bitmapApple = new BitmapApple(buffer); 
     
    holderVector[i] = bitmapApple; 
         
    holder.addChild(bitmapApple); 
}

메모리에서 하나의 캐시된 비트맵만 사용되고 모든 BitmapApple 인스턴스에서 공유되므로 이 기법에는 적은 양의 메모리만 사용됩니다. 또한 알파, 회전 또는 크기 조절 등 BitmapApple 인스턴스가 변경되더라도 원본 소스 비트맵이 업데이트되지 않습니다. 이 기술을 사용하면 결과적으로 성능이 저하되는 것을 막을 수 있습니다.

매끄러운 최종 비트맵을 얻으려면 smoothing 속성을 true 로 설정합니다.

public function BitmapApple(buffer:BitmapData) 
{ 
    super (buffer); 
  
    smoothing = true; 
  
    addEventListener(Event.ADDED_TO_STAGE, activation); 
     addEventListener(Event.REMOVED_FROM_STAGE, deactivation);     
}

스테이지 품질을 조정하면 성능도 향상시킬 수 있습니다. 래스터화 전에 스테이지 품질을 HIGH 로 설정한 후 나중에 LOW 로 전환합니다.

import org.bytearray.bitmap.BitmapApple; 
  
const MAX_NUM:int = 100; 
var holder:Sprite = new Sprite(); 
  
addChild ( holder ); 
  
var holderVector:Vector.<BitmapApple> = new Vector.<BitmapApple>(MAX_NUM, true); 
var source:AppleSource = new AppleSource(); 
var bounds:Object = source.getBounds ( source ); 
      
var mat:Matrix = new Matrix(); 
mat.translate ( -bounds.x, -bounds.y ); 
  
var buffer:BitmapData = new BitmapData ( source.width+1, source.height+1, true, 0 ); 
  
stage.quality = StageQuality.HIGH; 
  
buffer.draw ( source, mat ); 
  
stage.quality = StageQuality.LOW; 
      
var bitmapApple:BitmapApple; 
      
for (var i:int = 0; i< MAX_NUM; i++ ) 
{ 
    bitmapApple = new BitmapApple( buffer ); 
     
    holderVector[i] = bitmapApple; 
         
    holder.addChild ( bitmapApple ); 
}

벡터를 비트맵에 그리기 전과 후에 스테이지 품질을 전환하면 화면에 앤티앨리어싱된 내용을 효과적으로 표시할 수 있습니다. 이 기법은 최종 스테이지 품질과 상관없이 효과적일 수 있습니다. 예를 들어 스테이지 품질이 LOW 로 설정된 경우에도 앤티앨리어싱된 텍스트가 포함된 앤티앨리어싱 비트맵을 표시할 수 있습니다. 이 기술은 cacheAsBitmap 속성과 함께 사용할 수 없습니다. 이 경우 스테이지 품질을 LOW 로 설정하면 벡터 품질이 업데이트되고, 이에 따라 비트맵 표면이 메모리에서 업데이트되고 최종 품질이 업데이트됩니다.