Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Kotlin rotateAnimation is not working. Nothing is happening when the spinBtn is clicked

***I am trying to create af spinningWheel animation with my imageView spinning once the button is pressed. I found a youtube video with some java code i converted into kotlin. With the code below nothing happens when the button is pushed. I have used the rotateAnimation class and some calculations in the GameViewModel thus far, and call the spin() in the GameFragment class’ onCreateView().

The code below are the two classes i use for the rotateAnimation but it doesn’t seem to work. I am not sure which part is the problem. Does anyone know?***

class GameViewModel : ViewModel() {

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private val fields = arrayOf("1","2","3","4","5","6","7")
private val fieldDegrees = IntArray(fields.size)
private var degree = 0
var isSpinning = false


fun getDegreesForSectors(){
    val oneFieldsDegrees = 360/fields.size
    for (i in fields.indices){
        fieldDegrees[i] = (i+1) * oneFieldsDegrees
    }
}


fun spin(imageView: ImageView){
    isSpinning = true
    degree = (0..fields.size).random()

    val rotateAnimation = RotateAnimation(0.toFloat(),
        (360 * fields.size + fieldDegrees[degree]).toFloat(),
        RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f)

    rotateAnimation.setDuration(3600)
    rotateAnimation.setFillAfter(true)
    rotateAnimation.setInterpolator(DecelerateInterpolator())
    rotateAnimation.setAnimationListener(object : Animation.AnimationListener{

        override fun onAnimationStart(animation: Animation) {}

        override fun onAnimationEnd(animation: Animation) {
            isSpinning = false
        }
        override fun onAnimationRepeat(animation: Animation) {}

    })
    imageView.startAnimation(rotateAnimation)
}

}

class GameFragment : Fragment() {

private lateinit var gameViewModel: GameViewModel
private var _binding: GameFragmentBinding? = null

// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!


override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {

    gameViewModel =
        ViewModelProvider(this).get(GameViewModel::class.java)


    val spinBtn: Button? = view?.findViewById(R.id.spinBtn)
    val wheelImage: ImageView? = view?.findViewById(R.id.Spinning_Wheel)
    gameViewModel.getDegreesForSectors()
    spinBtn?.setOnClickListener() {
        if (!gameViewModel.isSpinning) {
            gameViewModel.spin(wheelImage!!)
        }
    }


  _binding = GameFragmentBinding.inflate(inflater, container, false)
  val root: View = binding.root
  return root



}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}

}

>Solution :

You are accessing fragment view inside onCreateView(). This will return null since nothing has been inflated yet.
Use the binding variable to access the views.

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {

    gameViewModel = ViewModelProvider(this).get(GameViewModel::class.java)

    _binding = GameFragmentBinding.inflate(inflater, container, false)

    gameViewModel.getDegreesForSectors()
    binding.spinBtn.setOnClickListener() {
        if (!gameViewModel.isSpinning) {
            gameViewModel.spin(binding.spinningWheel)
        }
    }

  return binding.root
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading