Update Attributes during Rotation Animation

十年热恋 提交于 2019-12-25 01:14:39

问题


I'm working on a rotation animation in Kotlin and I after each Animation I want to set a net rotationStart and rotationEnd where the rotationStart value initially should be set = 0 and afterwards assumes the value of the rotationEnd value after each rotation

    val rand = Random()
    var rotStart : Float = 0f
    var rotEnd : Float = rand.nextFloat(100f)

Setting up the rotation like this:

    var rotateAnimation = RotateAnimation(
            rotStart, rotEnd,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f

    )
    rotateAnimation.duration = 4000
    rotateAnimation.repeatCount = 4

with the listener:

    rotateAnimation.setAnimationListener(object : Animation.AnimationListener {

        override fun onAnimationStart(animation: Animation?) {
        }

        override fun onAnimationRepeat(animation: Animation?) {
            rotStart =  rotEnd
            rotEnd = rand.nextFloat(359f)


        }

        override fun onAnimationEnd(animation: Animation?) {


        }
    })

    img_spinner.startAnimation(rotateAnimation)
}

The problem here is, that the value rotStart and rotEnd doesn't get updated after each cycle. Is there a onUpdateListener I forgot to implement?


回答1:


Well, the only way I found until now is this here (just in case nobody will answer to this thread, here is my solution)

    val angles = arrayOf(9,6,9,4)

    var rotStart = 0f
    var imageSpinner = findViewById<ImageView>(R.id.img_spinner)

        val ra1 = RotateAnimation(rotStart,-angles[0].toFloat() * 39, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f)
    ra1.duration = 3200

    val ra2 = RotateAnimation(rotStart, -angles[1].toFloat() * 39, Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f)
    ra2.duration = 1900
       val ra3 = RotateAnimation(rotStart, -angles[2].toFloat() * 39, Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f)
    ra3.duration = 5400
    val ra4 = RotateAnimation(rotStart, -angles[3].toFloat() * 39, Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f)
    ra4.duration = 5400

after this you have to set on each onAnimationEnd: imageview.startAnimation(ra#NUMBER#). It's a workaround and not a clean solution :-)



来源:https://stackoverflow.com/questions/59253778/update-attributes-during-rotation-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!