skip to Main Content

Here’s my code:

private fun roomChange(animation: Int)
{
    val rocketImage = findViewById<ImageView>(R.id.imageView2).apply {
        setBackgroundResource(R.drawable.animation)
        foxanim = background as AnimationDrawable
        foxanim.start()
    }
}

I have anim.xml file and would like to pass it to a function but "animation" is an unresolved reference. How would I go about passing it correctly? Thanks in advance!

3

Answers


  1. R.drawable.whatever is just an Int. That’s all you need to pass along to setBackgroundResource().

    If you want the IDE to help prevent you from accidentally passing the wrong kind of argument, you can also add @AnimRes. Then in certain cases when it can detect that you are passing an Int that is not an animation resource ID, it will show an error.

    private fun roomChange(@AnimRes animation: Int)
    {
        val rocketImage = findViewById<ImageView>(R.id.imageView2).apply {
            setBackgroundResource(animation)
            foxanim = background as AnimationDrawable
            foxanim.start()
        }
    }`
    
    Login or Signup to reply.
  2. Modify your code like this :

    private fun roomChange(animation: Drawable) {
        val rocketImage = findViewById<ImageView>(R.id.imageView2).apply {
            background = animation
            foxanim = background as AnimationDrawable
            foxanim.start()
        }
    }
    
    Login or Signup to reply.
  3. Here’s the enum thing I mentioned in the comments:

    // Basic enum with an animation resource property - add one for each anim
    // The @AnimRes annotation is optional, just gives you a warning if you use something else
    enum class StateAnimation(@AnimRes val animationId: Int) {
        ANIM(R.drawable.animation),
        RUN(R.drawable.run)
    }
    
    ...
    
    // set your function to take one of your animation definitions instead
    private fun roomChange(stateAnim: StateAnimation) {
        // pull out the resource ID from the passed animation
        setBackgroundResource(stateAnim.animationId)
    }
    
    // call your function using the animation you want
    roomChange(StateAnimation.RUN)
    

    The idea here is you can create an enum class that defines all your animations, so you can name each one however you like, and they all have a resource ID for the animation they represent. Then you pass one of those to your function instead, and the function can refer to its resource ID

    This way you get code completion and type checking (you have to pass one of the StateAnimations, not any other animation ID, resource ID, or random int). Your code can be cleaner because StateAnimation.RUN is pretty clear (you might want to rename that but you get the idea), and it’s safer than passing a string like "run" and converting that into a resource lookup which may or may not exist

    And if you want, you can just import everything in StateAnimation (put your cursor on RUN and do Alt+Enter or click the lightbulb and it’ll offer to do it for you) and then you can just call roomChange(RUN) and it’s nice and concise!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search