@@ -7,24 +7,24 @@ import androidx.dynamicanimation.animation.FloatPropertyCompat
77import androidx.dynamicanimation.animation.SpringAnimation
88import androidx.dynamicanimation.animation.SpringForce
99
10- class ViewPropertySpringAnimator (
11- private val view : View
10+ class ViewPropertySpringAnimator < T : View > (
11+ private val view : T
1212) {
1313
14- interface AnimatorListener {
15- fun onAnimationStart (animator : ViewPropertySpringAnimator ) {}
16- fun onAnimationCancel (animator : ViewPropertySpringAnimator ) {}
17- fun onAnimationEnd (animator : ViewPropertySpringAnimator ) {}
14+ interface AnimatorListener < T : View > {
15+ fun onAnimationStart (animator : ViewPropertySpringAnimator < T > ) {}
16+ fun onAnimationCancel (animator : ViewPropertySpringAnimator < T > ) {}
17+ fun onAnimationEnd (animator : ViewPropertySpringAnimator < T > ) {}
1818 }
1919
2020 private val pendingAnimations = mutableListOf<SpringAnimationHolder >()
21- private val runningAnimations = mutableMapOf<FloatPropertyCompat <View >, SpringAnimation > ()
21+ private val runningAnimations = mutableMapOf<FloatPropertyCompat <in T >, SpringAnimation > ()
2222 val isRunning: Boolean
2323 get() = runningAnimations.isNotEmpty()
2424
2525 private var defaultDampingRatio: Float = SpringForce .DAMPING_RATIO_MEDIUM_BOUNCY
2626 private var defaultStiffness: Float = SpringForce .STIFFNESS_MEDIUM
27- private var animatorListener: AnimatorListener ? = null
27+ private var animatorListener: AnimatorListener < T > ? = null
2828
2929 fun defaultDampingRatio (value : Float ) = apply {
3030 defaultDampingRatio = value
@@ -37,181 +37,181 @@ class ViewPropertySpringAnimator(
3737 fun x (
3838 finalValue : Float ,
3939 config : SpringAnimationConfig .() -> Unit = {}
40- ): ViewPropertySpringAnimator =
40+ ): ViewPropertySpringAnimator < T > =
4141 animateProperty(DynamicAnimation .X , finalValue, config)
4242
4343 fun xBy (
4444 finalValue : Float ,
4545 config : SpringAnimationConfig .() -> Unit = {}
46- ): ViewPropertySpringAnimator =
46+ ): ViewPropertySpringAnimator < T > =
4747 animatePropertyBy(DynamicAnimation .X , finalValue, config)
4848
4949 fun y (
5050 finalValue : Float ,
5151 config : SpringAnimationConfig .() -> Unit = {}
52- ): ViewPropertySpringAnimator =
52+ ): ViewPropertySpringAnimator < T > =
5353 animateProperty(DynamicAnimation .Y , finalValue, config)
5454
5555 fun yBy (
5656 finalValue : Float ,
5757 config : SpringAnimationConfig .() -> Unit = {}
58- ): ViewPropertySpringAnimator =
58+ ): ViewPropertySpringAnimator < T > =
5959 animatePropertyBy(DynamicAnimation .Y , finalValue, config)
6060
6161 fun z (
6262 finalValue : Float ,
6363 config : SpringAnimationConfig .() -> Unit = {}
64- ): ViewPropertySpringAnimator =
64+ ): ViewPropertySpringAnimator < T > =
6565 animateProperty(DynamicAnimation .Z , finalValue, config)
6666
6767 fun zBy (
6868 finalValue : Float ,
6969 config : SpringAnimationConfig .() -> Unit = {}
70- ): ViewPropertySpringAnimator =
70+ ): ViewPropertySpringAnimator < T > =
7171 animatePropertyBy(DynamicAnimation .Z , finalValue, config)
7272
7373 fun rotation (
7474 finalValue : Float ,
7575 config : SpringAnimationConfig .() -> Unit = {}
76- ): ViewPropertySpringAnimator =
76+ ): ViewPropertySpringAnimator < T > =
7777 animateProperty(DynamicAnimation .ROTATION , finalValue, config)
7878
7979 fun rotationBy (
8080 finalValue : Float ,
8181 config : SpringAnimationConfig .() -> Unit = {}
82- ): ViewPropertySpringAnimator =
82+ ): ViewPropertySpringAnimator < T > =
8383 animatePropertyBy(DynamicAnimation .ROTATION , finalValue, config)
8484
8585 fun rotationX (
8686 finalValue : Float ,
8787 config : SpringAnimationConfig .() -> Unit = {}
88- ): ViewPropertySpringAnimator =
88+ ): ViewPropertySpringAnimator < T > =
8989 animateProperty(DynamicAnimation .ROTATION_X , finalValue, config)
9090
9191 fun rotationXBy (
9292 finalValue : Float ,
9393 config : SpringAnimationConfig .() -> Unit = {}
94- ): ViewPropertySpringAnimator =
94+ ): ViewPropertySpringAnimator < T > =
9595 animatePropertyBy(DynamicAnimation .ROTATION_X , finalValue, config)
9696
9797 fun rotationY (
9898 finalValue : Float ,
9999 config : SpringAnimationConfig .() -> Unit = {}
100- ): ViewPropertySpringAnimator =
100+ ): ViewPropertySpringAnimator < T > =
101101 animateProperty(DynamicAnimation .ROTATION_Y , finalValue, config)
102102
103103 fun rotationYBy (
104104 finalValue : Float ,
105105 config : SpringAnimationConfig .() -> Unit = {}
106- ): ViewPropertySpringAnimator =
106+ ): ViewPropertySpringAnimator < T > =
107107 animatePropertyBy(DynamicAnimation .ROTATION_Y , finalValue, config)
108108
109109 fun translationX (
110110 finalValue : Float ,
111111 config : SpringAnimationConfig .() -> Unit = {}
112- ): ViewPropertySpringAnimator =
112+ ): ViewPropertySpringAnimator < T > =
113113 animateProperty(DynamicAnimation .TRANSLATION_X , finalValue, config)
114114
115115 fun translationXBy (
116116 finalValue : Float ,
117117 config : SpringAnimationConfig .() -> Unit = {}
118- ): ViewPropertySpringAnimator =
118+ ): ViewPropertySpringAnimator < T > =
119119 animatePropertyBy(DynamicAnimation .TRANSLATION_X , finalValue, config)
120120
121121 fun translationY (
122122 finalValue : Float ,
123123 config : SpringAnimationConfig .() -> Unit = {}
124- ): ViewPropertySpringAnimator =
124+ ): ViewPropertySpringAnimator < T > =
125125 animateProperty(DynamicAnimation .TRANSLATION_Y , finalValue, config)
126126
127127 fun translationYBy (
128128 finalValue : Float ,
129129 config : SpringAnimationConfig .() -> Unit = {}
130- ): ViewPropertySpringAnimator =
130+ ): ViewPropertySpringAnimator < T > =
131131 animatePropertyBy(DynamicAnimation .TRANSLATION_Y , finalValue, config)
132132
133133 fun translationZ (
134134 finalValue : Float ,
135135 config : SpringAnimationConfig .() -> Unit = {}
136- ): ViewPropertySpringAnimator =
136+ ): ViewPropertySpringAnimator < T > =
137137 animateProperty(DynamicAnimation .TRANSLATION_Z , finalValue, config)
138138
139139 fun translationZBy (
140140 finalValue : Float ,
141141 config : SpringAnimationConfig .() -> Unit = {}
142- ): ViewPropertySpringAnimator =
142+ ): ViewPropertySpringAnimator < T > =
143143 animatePropertyBy(DynamicAnimation .TRANSLATION_Z , finalValue, config)
144144
145145 fun scaleX (
146146 finalValue : Float ,
147147 config : SpringAnimationConfig .() -> Unit = {}
148- ): ViewPropertySpringAnimator =
148+ ): ViewPropertySpringAnimator < T > =
149149 animateProperty(DynamicAnimation .SCALE_X , finalValue, config)
150150
151151 fun scaleXBy (
152152 finalValue : Float ,
153153 config : SpringAnimationConfig .() -> Unit = {}
154- ): ViewPropertySpringAnimator =
154+ ): ViewPropertySpringAnimator < T > =
155155 animatePropertyBy(DynamicAnimation .SCALE_X , finalValue, config)
156156
157157 fun scaleY (
158158 finalValue : Float ,
159159 config : SpringAnimationConfig .() -> Unit = {}
160- ): ViewPropertySpringAnimator =
160+ ): ViewPropertySpringAnimator < T > =
161161 animateProperty(DynamicAnimation .SCALE_Y , finalValue, config)
162162
163163 fun scaleYBy (
164164 finalValue : Float ,
165165 config : SpringAnimationConfig .() -> Unit = {}
166- ): ViewPropertySpringAnimator =
166+ ): ViewPropertySpringAnimator < T > =
167167 animatePropertyBy(DynamicAnimation .SCALE_Y , finalValue, config)
168168
169169 fun alpha (
170170 finalValue : Float ,
171171 config : SpringAnimationConfig .() -> Unit = {}
172- ): ViewPropertySpringAnimator =
172+ ): ViewPropertySpringAnimator < T > =
173173 animateProperty(DynamicAnimation .ALPHA , finalValue, config)
174174
175175 fun alphaBy (
176176 finalValue : Float ,
177177 config : SpringAnimationConfig .() -> Unit = {}
178- ): ViewPropertySpringAnimator =
178+ ): ViewPropertySpringAnimator < T > =
179179 animatePropertyBy(DynamicAnimation .ALPHA , finalValue, config)
180180
181181 fun animateProperty (
182182 finalValue : Float ,
183- setter : View .(Float ) -> Unit ,
184- getter : View .() -> Float ,
183+ setter : T .(Float ) -> Unit ,
184+ getter : T .() -> Float ,
185185 config : SpringAnimationConfig .() -> Unit = {}
186- ): ViewPropertySpringAnimator =
186+ ): ViewPropertySpringAnimator < T > =
187187 animateProperty(createCustomProperty(setter, getter), finalValue, config)
188188
189189 fun animatePropertyBy (
190- setter : View .(Float ) -> Unit ,
191- getter : View .() -> Float ,
190+ setter : T .(Float ) -> Unit ,
191+ getter : T .() -> Float ,
192192 finalValue : Float ,
193193 config : SpringAnimationConfig .() -> Unit = {}
194- ): ViewPropertySpringAnimator =
194+ ): ViewPropertySpringAnimator < T > =
195195 animatePropertyBy(createCustomProperty(setter, getter), finalValue, config)
196196
197197 fun animateProperty (
198- property : FloatPropertyCompat <View >,
198+ property : FloatPropertyCompat <in T >,
199199 finalValue : Float ,
200200 config : SpringAnimationConfig .() -> Unit = {}
201201 ) = apply {
202202 animateProperty(property, finalValue, 0f , config)
203203 }
204204
205205 fun animatePropertyBy (
206- property : FloatPropertyCompat <View >,
206+ property : FloatPropertyCompat <in T >,
207207 finalValue : Float ,
208208 config : SpringAnimationConfig .() -> Unit = {}
209209 ) = apply {
210210 animateProperty(property, finalValue, property.getValue(view), config)
211211 }
212212
213213 private fun animateProperty (
214- property : FloatPropertyCompat <View >,
214+ property : FloatPropertyCompat <in T >,
215215 finalValue : Float ,
216216 finalValueBias : Float ,
217217 configBuilder : SpringAnimationConfig .() -> Unit = {}
@@ -230,7 +230,7 @@ class ViewPropertySpringAnimator(
230230 }
231231
232232 private fun SpringAnimation.createEndListener (
233- property : FloatPropertyCompat <View >
233+ property : FloatPropertyCompat <in T >
234234 ) {
235235 val listener = object : DynamicAnimation .OnAnimationEndListener {
236236 override fun onAnimationEnd (
@@ -251,7 +251,7 @@ class ViewPropertySpringAnimator(
251251 }
252252
253253 @MainThread
254- fun start (): ViewPropertySpringAnimator = apply {
254+ fun start (): ViewPropertySpringAnimator < T > = apply {
255255 val animations = pendingAnimations.toList()
256256 pendingAnimations.clear()
257257 animatorListener?.onAnimationStart(this )
@@ -276,24 +276,24 @@ class ViewPropertySpringAnimator(
276276 }
277277
278278 fun setListener (
279- onStart : (animator: ViewPropertySpringAnimator ) -> Unit = {},
280- onCancel : (animator: ViewPropertySpringAnimator ) -> Unit = {},
281- onEnd : (animator: ViewPropertySpringAnimator ) -> Unit = {}
282- ) = setListener(object : AnimatorListener {
283- override fun onAnimationStart (animator : ViewPropertySpringAnimator ) {
279+ onStart : (animator: ViewPropertySpringAnimator < T > ) -> Unit = {},
280+ onCancel : (animator: ViewPropertySpringAnimator < T > ) -> Unit = {},
281+ onEnd : (animator: ViewPropertySpringAnimator < T > ) -> Unit = {}
282+ ) = setListener(object : AnimatorListener < T > {
283+ override fun onAnimationStart (animator : ViewPropertySpringAnimator < T > ) {
284284 onStart(animator)
285285 }
286286
287- override fun onAnimationCancel (animator : ViewPropertySpringAnimator ) {
287+ override fun onAnimationCancel (animator : ViewPropertySpringAnimator < T > ) {
288288 onCancel(animator)
289289 }
290290
291- override fun onAnimationEnd (animator : ViewPropertySpringAnimator ) {
291+ override fun onAnimationEnd (animator : ViewPropertySpringAnimator < T > ) {
292292 onEnd(animator)
293293 }
294294 })
295295
296- fun setListener (listener : AnimatorListener ? ) = apply {
296+ fun setListener (listener : AnimatorListener < T > ? ) = apply {
297297 this .animatorListener = listener
298298 }
299299
@@ -310,14 +310,14 @@ class ViewPropertySpringAnimator(
310310 }
311311
312312 private fun createCustomProperty (
313- setter : View .(Float ) -> Unit ,
314- getter : View .() -> Float
315- ) = object : FloatPropertyCompat <View >(" CustomProperty" ) {
316- override fun getValue (view : View ): Float {
313+ setter : T .(Float ) -> Unit ,
314+ getter : T .() -> Float
315+ ) = object : FloatPropertyCompat <T >(" CustomProperty" ) {
316+ override fun getValue (view : T ): Float {
317317 return getter.invoke(view)
318318 }
319319
320- override fun setValue (view : View , value : Float ) {
320+ override fun setValue (view : T , value : Float ) {
321321 setter.invoke(view, value)
322322 }
323323 }
0 commit comments