• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download

<lambda>null1 package com.airbnb.lottie.samples
2 
3 import android.annotation.SuppressLint
4 import android.graphics.PointF
5 import android.os.Bundle
6 import android.util.Log
7 import androidx.annotation.FloatRange
8 import androidx.appcompat.app.AppCompatActivity
9 import com.airbnb.lottie.LottieProperty
10 import com.airbnb.lottie.model.KeyPath
11 import com.airbnb.lottie.samples.databinding.DynamicActivityBinding
12 import com.airbnb.lottie.samples.utils.viewBinding
13 
14 private val COLORS = arrayOf(
15     0xff5a5f,
16     0x008489,
17     0xa61d55
18 )
19 private val EXTRA_JUMP = arrayOf(0f, 20f, 50f)
20 
21 class DynamicActivity : AppCompatActivity() {
22     private val binding: DynamicActivityBinding by viewBinding()
23 
24     private var speed = 1
25     private var colorIndex = 0
26     private var extraJumpIndex = 0
27 
28     override fun onCreate(savedInstanceState: Bundle?) {
29         super.onCreate(savedInstanceState)
30 
31         binding.speedButton.setOnClickListener {
32             speed = ++speed % 4
33             updateButtonText()
34         }
35 
36         binding.colorButton.setOnClickListener {
37             colorIndex = (colorIndex + 1) % COLORS.size
38             updateButtonText()
39         }
40 
41         binding.jumpHeight.setOnClickListener {
42             extraJumpIndex = (extraJumpIndex + 1) % EXTRA_JUMP.size
43             updateButtonText()
44             setupValueCallbacks()
45         }
46 
47         binding.animationView.addLottieOnCompositionLoadedListener { _ ->
48             binding.animationView.resolveKeyPath(KeyPath("**")).forEach {
49                 Log.d(TAG, it.keysToString())
50                 setupValueCallbacks()
51             }
52         }
53         binding.animationView.setFailureListener { e ->
54             Log.e(TAG, "Failed to load animation!", e)
55         }
56 
57         updateButtonText()
58     }
59 
60     private fun setupValueCallbacks() {
61         binding.animationView.addValueCallback(KeyPath("LeftArmWave"), LottieProperty.TIME_REMAP) { frameInfo ->
62             2 * speed.toFloat() * frameInfo.overallProgress
63         }
64 
65         val shirt = KeyPath("Shirt", "Group 5", "Fill 1")
66         val leftArm = KeyPath("LeftArmWave", "LeftArm", "Group 6", "Fill 1")
67         val rightArm = KeyPath("RightArm", "Group 6", "Fill 1")
68 
69         binding.animationView.addValueCallback(shirt, LottieProperty.COLOR) { COLORS[colorIndex] }
70         binding.animationView.addValueCallback(leftArm, LottieProperty.COLOR) { COLORS[colorIndex] }
71         binding.animationView.addValueCallback(rightArm, LottieProperty.COLOR) { COLORS[colorIndex] }
72         val point = PointF()
73         binding.animationView.addValueCallback(
74             KeyPath("Body"),
75             LottieProperty.TRANSFORM_POSITION
76         ) { frameInfo ->
77             val startX = frameInfo.startValue.x
78             var startY = frameInfo.startValue.y
79             var endY = frameInfo.endValue.y
80 
81             if (startY > endY) {
82                 startY += EXTRA_JUMP[extraJumpIndex]
83             } else if (endY > startY) {
84                 endY += EXTRA_JUMP[extraJumpIndex]
85             }
86             point.set(startX, lerp(startY, endY, frameInfo.interpolatedKeyframeProgress))
87             point
88         }
89     }
90 
91     @SuppressLint("SetTextI18n")
92     private fun updateButtonText() {
93         binding.speedButton.text = "Wave: ${speed}x Speed"
94         binding.jumpHeight.text = "Extra jump height ${EXTRA_JUMP[extraJumpIndex]}"
95     }
96 
97     fun lerp(a: Float, b: Float, @FloatRange(from = 0.0, to = 1.0) percentage: Float) = a + percentage * (b - a)
98 
99     companion object {
100         val TAG = DynamicActivity::class.simpleName
101     }
102 }
103