[Jetpack Compose] AnimationVector

유민국·2024년 10월 18일

AnimationVector

sealed class AnimationVector {
	// 각 속성값을 0f로 초기화
    internal abstract fun reset()
    
    // override fun newVector(): AnimationVector1D = AnimationVector1D(0f) 
    // 위와 같은식으로 현재 차원에 맞는 AnimationVector를 반환
    internal abstract fun newVector(): AnimationVector

	// 여기서 index는 해당 차원의 속성을 뜻한다
    // ex) IntSize의 경우 width가 0, height가 1
    // 만약 해당 차원을 넘는 index라면 0f를 반환한다
    internal abstract operator fun get(index: Int): Float
    internal abstract operator fun set(index: Int, value: Float)
    
    // 애니메이션 속성의 개수라고 생각하면 된다
    internal abstract val size: Int
}

애니메이션 중간 상태나 움직임을 벡터 형식으로 표현하기 위한 추상 클래스

  • compose 애니메이션은 화면 상의 다양한 속성(위치, 크기, 색상 등등)을 변경

1,2,3,4 차원에 따라 Float값으로 상태를 표현. 즉 애니메이션 대상이 여러 속성일 때 이를 각각의 차원으로 표현
차원이 늘어날 수록 속성값이 늘어난다고 생가하면 된다

다음과 같이 예를 들 수 있다
AnimationVector1D: 크기 또는 투명도 같은 단일 속성을 애니메이션할 때 사용
AnimationVector2D: 위치(X, Y 좌표)를 애니메이션할 때 사용
AnimationVector3D: 3차원 애니메이션을 할 때 사용
AnimationVector4D: 색상(R, G, B, A 값)과 같은 네 가지 속성을 동시에 애니메이션할 때 사용

EX)

@Composable
fun animateIntSizeAsState(
    targetValue: IntSize,
    animationSpec: AnimationSpec<IntSize> = intSizeDefaultSpring,
    label: String = "IntSizeAnimation",
    finishedListener: ((IntSize) -> Unit)? = null
): State<IntSize> {
    return animateValueAsState(
        targetValue,
        IntSize.VectorConverter,
        animationSpec,
        label = label,
        finishedListener = finishedListener
    )
}

animateValueAsState의 두 번째 인자가 V: AnimationVector 타입이며 animateIntSizeAsState는 width와 height의 속성이 있기때문에 IntSize의 VectorConvertor를 통해 AnimationVector2D를 사용하는걸 확인 할 수 있다.

profile
안녕하세요 😊

0개의 댓글