Git Product home page Git Product logo

Comments (7)

ScottPierce avatar ScottPierce commented on June 28, 2024

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.

davidganster avatar davidganster commented on June 28, 2024

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.

davidganster avatar davidganster commented on June 28, 2024

@ScottPierce did you have a chance to try out the code sample?

from android_additive_animations.

ScottPierce avatar ScottPierce commented on June 28, 2024

from android_additive_animations.

ScottPierce avatar ScottPierce commented on June 28, 2024

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.

davidganster avatar davidganster commented on June 28, 2024

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.

davidganster avatar davidganster commented on June 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.