且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android中使用动画将视图移动到另一个视图?

更新时间:2022-03-06 21:51:08

我遇到了相同的问题,并通过使用下一个代码修复了该问题(对不起,这是Kotlin,但工作原理相同假设viewFirst要到达viewTwo位置:

I had the same issue and I fixed by using the next code (sorry is in Kotlin, but works the same in Java).Let's say viewFirst wants to reach viewTwo position:

(不要使用):

               viewFirst.animate()
                        .translationX(viewSecond.x)
                        .translationY(viewSecond.y)
                        .setDuration(1000)
                        .withEndAction {
                        //to make sure that it arrives, 
                        //but not needed actually these two lines
                            viewFirst.x = viewSecond.x
                            viewFirst.y = viewSecond.y
                        }
                        .start()

(使用此解决方案):

               viewFirst.animate()
                        .x(viewSecond.x)
                        .y(viewSecond.y)
                        .setDuration(1000)
                        .withEndAction {
                        //to make sure that it arrives, 
                        //but not needed actually these two lines
                            viewFirst.x = viewSecond.x
                            viewFirst.y = viewSecond.y
                        }
                        .start()