1

I have a simple scale animation:

val x = PropertyValuesHolder.ofFloat(View.SCALE_X, 0f, 2f)
val y = PropertyValuesHolder.ofFloat(View.SCALE_Y, 0f, 2f)
val grow = ObjectAnimator.ofPropertyValuesHolder(imageViews[j].starViews[3], x, y).apply {
    duration = 5000
}

imageView[].visibility = View.VISIBLE
AnimatorSet().play(grow)

It works well when I add it to my onTouchListener. But when I add it to the onLongClickListener, it doesn't work. It only gets visible, and animation doesn't happen

imageViews[j].starViews[2].setOnLongClickListener {
    if (!imageViews[j].done && imageViews[j].intermediate) {
        //The onTouchListener also has this if condition
        val x = PropertyValuesHolder.ofFloat(View.SCALE_X, 0f, 2f)
        val y = PropertyValuesHolder.ofFloat(View.SCALE_Y, 0f, 2f)
        val grow = ObjectAnimator.ofPropertyValuesHolder(imageView, x, y).apply {
        duration = 5000
        }

   imageView.visibility = View.VISIBLE
    AnimatorSet().apply {
        Log.d("animCall", "called")
        play(grow)
    }
    return@setOnLongClickListener true
   }
   return@setOnLongClickListener false
  }
Lucas
  • 87
  • 1
  • 8

2 Answers2

0

You'll want to create a minimal, reproducible example. This is an example of that:

val x = PropertyValuesHolder.ofFloat(View.SCALE_X, 0f, 2f)
val y = PropertyValuesHolder.ofFloat(View.SCALE_Y, 0f, 2f)
val grow = ObjectAnimator.ofPropertyValuesHolder(imageview, x, y).apply {
    duration = 5000
}
imageview.setOnClickListener{
    grow.start()
}


imageview.setOnLongClickListener{
    grow.start()
    return@setOnLongClickListener true
}

Generally AnimatorSet is for having multiple animations trigger at once, for your single scale animation just calling .start() on scaleMid2 should be enough.

Both OnClick and OnLongClick produced the same animation, which leads me to believe your problem is either

  • the if statement
  • the .isFocusableInTouchMode = true line
  • something else in the code not represented in your question

Since I have the bare minimum reproducible code I can change the code to this

imageview.setOnLongClickListener{
    imageview.isFocusableInTouchMode = true
    grow.start()
    return@setOnLongClickListener true
}

And find that the OnLongClickListener is still working fine (demonstrating, in my case at least, that isFocusableInTouchMode is unnecessary).

I can then only assume that the issue is in your if statement or in the other code, you can follow the previous steps to figure it out on your own.

  • Thanks for the example's tips, I changed my question a little bit and I think it's more clear to undderstand now. The if statement is also in the onTouchListener, both gestures will need the same condition, but the animation will be different, based on the gesture (a normal touch/a long touch). The isFocusableInTouchMode was just to try to fix it, I already deleted – Lucas Apr 15 '20 at 00:53
  • Also, the ``Log.d("animCall", "called")`` is called, and It's inside ``Animatorset().apply``, so the animation is called. – Lucas Apr 15 '20 at 01:05
0

I just found out I forgot the start() inside the AnimatorSet()

Lucas
  • 87
  • 1
  • 8