(Android/안드로이드) LottieAnimation & 메인 스레드(UI Thread) 사용 정리

개요

Lottie는 “벡터 애니메이션을 쉽게” 보여주지만, 막상 프로젝트에 넣으면 이런 이슈를 자주 만납니다.

요약


1. Lottie는 “어느 스레드에서” 동작하나?


2. 왜 메인 스레드가 막히면 Lottie가 끊기나?

Lottie는 결과적으로 화면에 프레임을 그려야 합니다.

흔한 원인


3. 로딩/파싱은 메인에서 하면 안 되는가?


4. 메인 스레드 안전 호출 패턴(실무용)

“Lottie start/stop을 어디서 호출할지”가 흔한 문제라서, 아래처럼 래핑하면 안전합니다.

fun LottieAnimationView.safePlay() {
    if (Looper.myLooper() == Looper.getMainLooper()) {
        playAnimation()
    } else {
        post { playAnimation() }
    }
}

fun LottieAnimationView.safeStopAndClear() {
    if (Looper.myLooper() == Looper.getMainLooper()) {
        cancelAnimation()
        progress = 0f
    } else {
        post {
            cancelAnimation()
            progress = 0f
        }
    }
}

5. Dialog/Progress에서 Lottie를 쓸 때 “안 꺼지는 것처럼 보이는” 이유

✅ 추천: Dialog/Fragment의 라이프사이클에 맞춰 명확히 stop

override fun onStart() {
    super.onStart()
    binding.lottieView.safePlay()
}

override fun onStop() {
    binding.lottieView.safeStopAndClear()
    super.onStop()
}


Related Posts