Comments (7)
Found another really annoying side-effect of the generic types on AdditiveAnimator
in Kotlin. It makes extension functions difficult as well:
fun <T : AdditiveAnimator<T>> AdditiveAnimator<T>.visibleAlpha(isVisible: Boolean): AdditiveAnimator<T> {
val endAlpha = if (isVisible) 1f else 0f
return alpha(endAlpha)
}
Kotlin errors out when you try to use this function, taking issue with the recursive types.
I've had to work around it by using:
fun <T : AdditiveAnimator<*>> AdditiveAnimator<T>.visibleAlpha(isVisible: Boolean): AdditiveAnimator<T> {
val endAlpha = if (isVisible) 1f else 0f
@Suppress("UNCHECKED_CAST")
return alpha(endAlpha) as AdditiveAnimator<T>
}
from android_additive_animations.
Hi,
I will add the static creation methods you suggested, but I'm having no problem using the AdditiveAnimator.animate()
methods in Kotlin.
I don't really know Kotlin very well (I only just installed AS 3.0 after I read your comment), but I came up with a few solutions to the function extension problem:
First, you can provide a subclass which instantiates the generic type.
Second, you can simply omit the generic specification and use AdditiveAnimator<*>
.
Let me know if these approaches seem usable to you:
// subclass specifies generic type
class AdditiveAnimatorSubclass : AdditiveAnimator<AdditiveAnimatorSubclass> {
constructor(view: View) : super(view)
// can simply return subclass
fun visibleAlpha(visible: Boolean) : AdditiveAnimatorSubclass {
return alpha(if(visible) 1f else 0f)
}
}
// generic extension function
fun AdditiveAnimator<*>.visibleAlpha(isVisible: Boolean): AdditiveAnimator<*> {
val endAlpha = if (isVisible) 1f else 0f
return alpha(endAlpha)
}
// subclass extension function doesn't require generics
fun AdditiveAnimatorSubclass.xy(x: Float, y: Float): AdditiveAnimatorSubclass {
return x(x).y(y)
}
class KotlinTestFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val root = inflater?.inflate(R.layout.fragment_tap_to_move_demo, container, false)!!
val animatedView = root.findViewById(R.id.animated_view)!!
root.setOnTouchListener({ _, motionEvent ->
AdditiveAnimatorSubclass(animatedView).xy(motionEvent.x, motionEvent.y).visibleAlpha(true).start()
// this works fine as well:
// AdditiveAnimator.animate(animatedView).x(motionEvent.x).y(motionEvent.y).visibleAlpha(true).start()
true
})
return root
}
}
I also pushed these changes to the /wirecube/android_additive_animations/tree/kotlin branch, if you'd like to try it out yourself.
from android_additive_animations.
@ScottPierce did you have a chance to try out the code sample?
from android_additive_animations.
from android_additive_animations.
Extending the base animation seems to be working well enough for the time being. Did you want me to keep this open to track the creation of the above mentioned static methods, or should this just be closed?
from android_additive_animations.
I wanted to wait until I merged the animate_objects
branch, which has the improved construction methods, but got sidetracked working on performance improvements.
I'll finally merge this on the weekend, and close this issue once I do.
Thanks for reminding me!
from android_additive_animations.
Hi, I just merged the animate_objects
branch which removes the generics from AdditiveAnimator
. This should remove the need for subclassing and make working with Kotlin easier!
from android_additive_animations.
Related Issues (19)
- Gradle build failed HOT 4
- Leak HOT 9
- Properly blend delayed animations HOT 5
- Provide Kotlin extensions HOT 2
- How to reset, reverse and pause/resume additive animations? HOT 1
- Using setStartDelay after creating a staggered animation removes stagger
- Move to AndroidX HOT 3
- Move away from jcenter HOT 2
- Library sometimes crashes when mRunningAnimationsManager is null internally HOT 6
- Content gone in 1.9.2 but works in 1.9.1 HOT 2
- Remove gifs from this repo
- iOS? HOT 1
- superfluous? HOT 1
- purpose? HOT 1
- effect? HOT 1
- Question HOT 13
- pivotXY for view rotation around center HOT 1
- Cancel repeating animation HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from android_additive_animations.