How to achieve shake animation programmatically?

前端 未结 2 1646
长情又很酷
长情又很酷 2021-02-19 03:15

How can I achieve shake/wobble animation in android programmatically. There is a AndroidViewAnimations library available through which we can get the effect. But I don\'t want t

2条回答
  •  时光说笑
    2021-02-19 04:07

    Just to provide another possible answer to the question. The below animation file need to be put under res/anim folder :

    shake.xml

    
    
    
         
    
         
    
         
    
         
    
         
    
         
    
         
    
         
    
         
    
    
    

    And to use it in code (Here in Kotlin) :

    val animShake = AnimationUtils.loadAnimation(requireContext(), R.anim.shake)
    mView.startAnimation(animShake)
    

    Animation explanation:

    • If you want 1000ms of duration, you need to divide this by the number of step in your anim. Ex. with 5 steps: 1000/5 = 200 So if you put 200 of duration for each step, all will take same time to end

    • The "startOffset" parameter need to specify after what time the animation need to start (Here we wanted to sum all of before operations)

    • The "fromXDelta" parameter is your started point at each step, based on the previous step.

提交回复
热议问题