如何在Android中使用animation将视图移动到另一个视图?
我在屏幕的中心有一个ImageView
+ TextView
的圆圈。 我有另外两个ImageView
+ TextView
,一个在顶部,另一个在屏幕的底部。 我的要求是:
我想要一个顶级的ImageView
+ TextView
的副本和一个底部的ImageView
+ TextView
的副本在animation中移动到圆的中心,从而改变圆内的textView的值。
例如:
说顶部的textView值200和底部的textview值300.我想这些值的一部分(比如100或150)进行animation和移动到圆,但原始值200和300应保持在同一位置。
我试过使用TranslateAnimation
。 但是我面临的问题是find中心圆的x和y坐标。 它不完全走向圆圈的中心。 原始view's
位置也不保留。
TranslateAnimation animation = new TranslateAnimation(startLayout.getX(),endLayout.getX(), startLayout.getY(),endLayout.getY); animation.setDuration(1000); animation.setFillAfter(false); startView.startAnimation(animation);
startLayout是ImageView和TextView所在的线性布局。 请帮忙! 谢谢!
我有同样的问题,我通过使用下一个代码(遗憾的是在Kotlin,但在Java中的工作原理)固定。让我们来看viewFirst想要达到viewTwo位置:
(请勿使用):
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()