How to animate a View's translation

前端 未结 3 1454
深忆病人
深忆病人 2020-12-14 18:22

At the first click of a button, I want to slide a View along the x-axis (by 200 pixels to the right let\'s say). And on the second press of the button, I want t

3条回答
  •  北海茫月
    2020-12-14 19:02

    Try looking into ObjectAnimator and its super class Value Animator

    you can do something like this ObjectAnimator anim = ObjectAnimator.ofFloat(this, "translationX", 0,200); and then anim.start();

    Use a boolean value and toggle it with 200,0 in the object animator to slide back

    PS: you can use setDuration method to set how long the animation should take to complete

    Edit :

    Try looking at the support library which provides backward compatibility.

    Edit

    As @AdilHussain pointed out there is a library called nineoldandroids which can be used for the same purpose on older androids.

提交回复
热议问题