如何在Android中使用动画将视图移动到另一个视图? [英] How to move a view to another view using animation in Android?

查看:92
本文介绍了如何在Android中使用动画将视图移动到另一个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在屏幕中心有一个圆圈,里面有一个 ImageView + TextView 。我还有另外两个 ImageView + TextView ,一个在屏幕的顶部,另一个在屏幕的底部。
我的要求是:

I have a circle at the center of the screen inside which there's an ImageView + TextView. I have another two ImageView+TextView, one at the top and another at bottom of the screen. My requirement is :

我想要顶部 ImageView + TextView 和底部 ImageView + TextView 将动画移动到圆的中心,从而更改圆内textView的值。

I want a copy of the top ImageView+TextView and a copy of the bottom ImageView+TextView to move in animation into the center of the circle, thereby changing the value of the textView inside the circle.

例如:

说顶部textView的值为200,底部textview的值为300。我想要一部分这些值(例如100或150)进行动画处理并移动到圆圈中,但是原始值200和300应该保持在同一位置。

Say top textView has value 200 and bottom textview has value 300. I want a portion of those values (say 100 or 150) to animate and move into the circle, but the original values 200 and 300 should remain on the same position.

使用 TranslateAnimation 。但是,我遇到寻找中心圆的x和y坐标的问题。它并不精确地到达圆心。同样,不会保留原始的视图的职位。

I've tried using TranslateAnimation. However I face issues finding the x and y coordinates of the center circle. It is not exactly going to the center of the circle. Also original view's position is not retained.

    TranslateAnimation animation = new
TranslateAnimation(startLayout.getX(),endLayout.getX(),
startLayout.getY(),endLayout.getY);
                    animation.setDuration(1000);
                    animation.setFillAfter(false);
                    startView.startAnimation(animation);

startLayout是ImageView和TextView所在的线性布局。
请帮助!谢谢!

startLayout is the linearlayout in which ImageView and TextView reside. Please help! Thanks!

推荐答案

我遇到了相同的问题,并通过使用下一个代码修复了该问题(对不起,这是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()

这篇关于如何在Android中使用动画将视图移动到另一个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆