<lambda>null1 package com.airbnb.lottie.samples
2
3 import android.os.Bundle
4 import androidx.fragment.app.Fragment
5 import android.view.LayoutInflater
6 import android.view.View
7 import android.view.ViewGroup
8 import android.view.animation.Interpolator
9 import android.widget.Scroller
10 import com.airbnb.lottie.LottieAnimationView
11 import com.matthewtamlin.sliding_intro_screen_library.buttons.IntroButton
12 import com.matthewtamlin.sliding_intro_screen_library.core.IntroActivity
13 import com.matthewtamlin.sliding_intro_screen_library.core.LockableViewPager
14
15
16 class AppIntroActivity : IntroActivity() {
17 private val ANIMATION_TIMES = floatArrayOf(0f, 0.3333f, 0.6666f, 1f, 1f)
18
19 private val animationView: LottieAnimationView by lazy {
20 rootView.inflate(R.layout.app_intro_animation_view, false) as LottieAnimationView
21 }
22 private val viewPager: LockableViewPager by lazy {
23 findViewById<LockableViewPager>(R.id.intro_activity_viewPager)
24 }
25
26 override fun generatePages(savedInstanceState: Bundle?): Collection<Fragment> {
27 return listOf(
28 EmptyFragment.newInstance(),
29 EmptyFragment.newInstance(),
30 EmptyFragment.newInstance(),
31 EmptyFragment.newInstance()
32 )
33 }
34
35 override fun onCreate(savedInstanceState: Bundle?) {
36 super.onCreate(savedInstanceState)
37 rootView.addView(animationView, 0)
38 setViewPagerScroller()
39
40 addPageChangeListener(OnPageChangeListenerAdapter(
41 onPageScrolled = { position, positionOffset, _ ->
42 setAnimationProgress(position, positionOffset)
43 }
44 ))
45 }
46
47 override fun generateFinalButtonBehaviour(): IntroButton.Behaviour {
48 return object : IntroButton.Behaviour {
49 override fun setActivity(activity: IntroActivity) { finish() }
50 override fun getActivity(): IntroActivity? = null
51 override fun run() {}
52 }
53 }
54
55 private fun setAnimationProgress(position: Int, positionOffset: Float) {
56 val startProgress = ANIMATION_TIMES[position]
57 val endProgress = ANIMATION_TIMES[position + 1]
58
59 animationView.progress = startProgress.lerp(endProgress, positionOffset)
60 }
61
62 class EmptyFragment : androidx.fragment.app.Fragment() {
63
64 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
65 return container!!.inflate(R.layout.fragment_empty, false)
66 }
67
68 companion object {
69 internal fun newInstance(): EmptyFragment {
70 return EmptyFragment()
71 }
72 }
73 }
74
75 private fun setViewPagerScroller() {
76 try {
77 val scrollerField = androidx.viewpager.widget.ViewPager::class.java.getDeclaredField("mScroller")
78 scrollerField.isAccessible = true
79 val interpolator = androidx.viewpager.widget.ViewPager::class.java.getDeclaredField("sInterpolator")
80 interpolator.isAccessible = true
81
82 val scroller = object : Scroller(this, interpolator.get(null) as Interpolator) {
83 override fun startScroll(startX: Int, startY: Int, dx: Int, dy: Int, duration: Int) {
84 super.startScroll(startX, startY, dx, dy, duration * 7)
85 }
86 }
87 scrollerField.set(viewPager, scroller)
88 } catch (e: NoSuchFieldException) {
89 // Do nothing.
90 } catch (e: IllegalAccessException) {
91 // Do nothing.
92 }
93 }
94 }
95