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

<lambda>null1 package com.airbnb.lottie.samples
2 
3 import android.graphics.PointF
4 import android.os.Bundle
5 import androidx.appcompat.app.AppCompatActivity
6 import android.util.Log
7 import com.airbnb.lottie.LottieProperty
8 import com.airbnb.lottie.model.KeyPath
9 import com.airbnb.lottie.utils.MiscUtils
10 import kotlinx.android.synthetic.main.activity_dynamic.*
11 
12 private val COLORS = arrayOf(
13     0xff5a5f,
14     0x008489,
15     0xa61d55
16 )
17 private val EXTRA_JUMP = arrayOf(0f, 20f, 50f)
18 
19 class DynamicActivity : AppCompatActivity() {
20     private var speed = 1
21     private var colorIndex = 0
22     private var extraJumpIndex = 0
23 
24     companion object {
25         val TAG = DynamicActivity::class.simpleName
26     }
27 
28     override fun onCreate(savedInstanceState: Bundle?) {
29         super.onCreate(savedInstanceState)
30         setContentView(R.layout.activity_dynamic)
31 
32         speedButton.setOnClickListener {
33             speed = ++speed % 4
34             updateButtonText()
35         }
36 
37         colorButton.setOnClickListener {
38             colorIndex = (colorIndex + 1) % COLORS.size
39             updateButtonText()
40         }
41 
42         jumpHeight.setOnClickListener {
43             extraJumpIndex = (extraJumpIndex + 1) % EXTRA_JUMP.size
44             updateButtonText()
45         }
46 
47         animationView.addLottieOnCompositionLoadedListener { _ ->
48             animationView.resolveKeyPath(KeyPath("**")).forEach {
49                 Log.d(TAG, it.keysToString())
50             }
51         }
52         animationView.setFailureListener { /* do nothing */ }
53 
54         jumpHeight.postDelayed({ setupValueCallbacks() }, 1000)
55         updateButtonText()
56 
57     }
58 
59     private fun setupValueCallbacks() {
60         animationView.addValueCallback(KeyPath("LeftArmWave"),
61             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         animationView.addValueCallback(shirt, LottieProperty.COLOR) { COLORS[colorIndex] }
70         animationView.addValueCallback(leftArm, LottieProperty.COLOR) { COLORS[colorIndex] }
71         animationView.addValueCallback(rightArm, LottieProperty.COLOR) { COLORS[colorIndex] }
72         val point = PointF()
73         animationView.addValueCallback(KeyPath("Body"),
74             LottieProperty.TRANSFORM_POSITION) { frameInfo ->
75             val startX = frameInfo.startValue.x
76             var startY = frameInfo.startValue.y
77             var endY = frameInfo.endValue.y
78 
79             if (startY > endY) {
80                 startY += EXTRA_JUMP[extraJumpIndex]
81             } else if (endY > startY) {
82                 endY += EXTRA_JUMP[extraJumpIndex]
83             }
84             point.set(startX, MiscUtils.lerp(startY, endY, frameInfo.interpolatedKeyframeProgress))
85             point
86         }
87     }
88 
89     private fun updateButtonText() {
90         speedButton.text = "Wave: ${speed}x Speed"
91         jumpHeight.text = "Extra jump height ${EXTRA_JUMP[extraJumpIndex]}"
92     }
93 }
94