[Kotlin][Android] ์๋๋ก์ด๋ ๋กํฐ์ ๋๋ฉ์ด์ (Lottie Animation) ํ์ฉํ๊ธฐ
๋จผ์ ๋กํฐ์ ๋๋ฉ์ด์ ์ด๋ After Effect์์ ์ ์ํ Motion Graphic์ ์ดํ๋ฆฌ์ผ์ ์์ ๊ทธ๋๋ก ๋ณด์ฌ์ค ์ ์๊ฒ ๋ง๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก์จ OOM(OUT OF MEMORY)์ ๋ํด์๋ ์ ๊ฒฝ์ ๋ง์ด ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ ๋๋ค.
๋กํฐ ์ ๋๋ฉ์ด์ ์ ์ฌ์ฉํ๊ธฐ์ ์์
๋ชจ๋ gradle ํ์ผ์ dependency๋ฅผ ์ถ๊ฐํด์ฃผ๋๋ก ํฉ๋๋ค.
dependencies {
...
def lottieVersion = '์ฌ์ฉํ ๋ฒ์ '
implementation "com.airbnb.android:lottie:$lottieVersion"
...
}
์์ ๊ฐ์ด ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ฉฐ ๊ธ์ด์ด ๊ฐ์ ๊ฒฝ์ฐ๋ ๋น์ ์ต์ ๋ฒ์ ์ ํ์ฉํ์์ต๋๋ค.
๊ด๋ จ ์ ๋ณด๋ http://airbnb.io/lottie/#/android?id=sample-app๋ฅผ ํตํด ํ์ธ์ด ๊ฐ๋ฅํฉ๋๋ค.
Lottie Docs
airbnb.io
์ ์ฌ์ดํธ์์ documet๋ฅผ ๋ณด๋ฉด ์ฌ๋ฌ ์ฌ์ฉ๋ฒ์ด ๋์ ์์ต๋๋ค.
์ค๋์ ๊ทธ์ค์ ์ปค์คํ ์ ๋๋ฉ์ด์ ์ ์ฌ์ฉ๋ฒ์ ๋ํด ์์ ๋ณด๊ฒ ์ต๋๋ค.
์ฌ์ฉํ๊ณ ํ lottie ์ ๋๋ฉ์ด์ ํ์ผ์ ๋ด๋ ค ๋ฐ์ ํ assetํด๋์ ๋ณต์ฌํด์ค๋๋ค.
์๋์ ๊ฐ์ด xml์ ๊ตฌ์ฑํด์ค๋๋ค.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/like_button"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
app:lottie_autoPlay="false"
app:lottie_fileName="heart.json"
app:lottie_loop="false" />
</RelativeLayout>
Kotlinํ์ผ์ ์๋์ ๊ฐ์ด ์์ฑํด์ค๋๋ค.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
like_button.setOnClickListener {
// ํด๋ฆญ ๋ฆฌ์ค๋ ํจ์
clickLikeButton()
}
}
fun clickLikeButton() {
if(!isLiked){
//์ข์์ ๋๋ฅด์ง ์์ ์ํ
//ofFloat์ ์คํํ ๋ฒ์๋ฅผ ๋ํ๋ธ๋ค. (0f = 0% , 0.5f = 50%)
//setDuration์ ์ ๋๋ฉ์ด์
์ด ์คํ๋๋ ์๊ฐ์ ์ค์ ํ๋ค.ํ
//animator.start๋ฅผ ํตํด ์ ๋๋ฉ์ด์
์ ์คํ
val animator = ValueAnimator.ofFloat(0f, 0.5f).setDuration(1000)
isLiked = true
animator.addUpdateListener {
//updateListener๋ฅผ ํตํด ์ ๋๋ฉ์ด์
์ด ๊ฐฑ์ ๋๋๋ก ํด์ค๋ค.
//getAnimateValue ํ์ฌ ์ ๋๋ฉ์ด์
์ด ์คํ๋๋ ๊ตฌ๊ฐ์ Object๊ฐ์ผ๋ก ๋ฐํ ํด์ฃผ๋ ํจ์
//setProgress๋ float๊ฐ์ ์ธ์๋ก ๋ฐ๊ธฐ์ ํด๋น ๊ฐ์ float๊ฐ์ผ๋ก ์บ์คํ
ํด์ฃผ์๋ค.
like_button.setProgress(animator.getAnimatedValue() as Float)
}
animator.start()
}
else{
//์ข์์ ๋๋ฅธ ์ํ
val animator = ValueAnimator.ofFloat(0.5f, 1.0f).setDuration(200)
isLiked = false
animator.addUpdateListener {
like_button.setProgress(animator.getAnimatedValue() as Float)
}
animator.start()
}
}
๋ง์ง๋ง์ผ๋ก ์คํ ํ๋ฉด์ผ๋ก ๋ง๋ฌด๋ฆฌํ๋๋ก ํ๊ฒ ์ต๋๋ค.