ObjectAnimator VS TranslateAnimationObjectAnimator、VS、TranslateAnimation

2023-09-05 02:33:14 作者:网恋选我我超甜

我只是做一个简单的项目,我试图显示/隐藏与TranslateAnimation一个LinearLayout中顶部的布局。有一个忽悠,因为当我打电话onAnimationEnd(),动画还没有结束的0.1秒。

I just do a simple project where I try to show/hide a layout on the top of a LinearLayout with TranslateAnimation. There was a flicker because when I call onAnimationEnd(), the animation wasn't finished for 0.1sec.

例如:

            @Override
            public void onAnimationEnd(Animation animation) {
                retractableLayout.setVisibility(View.GONE);
            }

当我搜索计算器,我发现还有另一种方式来做到这一点。随着ObjectAnimator。使用它之后,我的动画被罚款没有 View.GONE

When I search on stackoverflow, I found there's another way to do it. With ObjectAnimator. After using it, my animation was fine without a View.GONE

TranslateAnimation和ObjectAnimator之间的区别是什么?就是其中之一就是去precated和他们做同样的事情还是有一个时间,当一个或另一个比较好。

What is the difference between TranslateAnimation and ObjectAnimator? Is one of them is deprecated and they do the same thing or there's a time when one or the other is better.

下面是一个GitHub库与2个版本( https://github.com/charlesvigneault/AAA_Test1 )

Here's a github repo with the 2 versions (https://github.com/charlesvigneault/AAA_Test1)

感谢

推荐答案

的不同之处主要是,如果你使用一个TranslateAnimation,你是动画并没有真正在屏幕上留下原来的位置来看,它只是使它看起来就像是移动的。因此,认为基本上不改变它的坐标。

The difference is mainly that if you use a TranslateAnimation, the view which you are animating does not really leave its original position on the screen, it just makes it look like it is moving. So the view basically doesnt change its coordinates.

查看查看动画视频: https://www.youtube.com/watch?v=_UWXqFBF86U

Check this video about View Animations : https://www.youtube.com/watch?v=_UWXqFBF86U

如果您使用的ObjectAnimator认为真正改变其实际位置。

If you use an ObjectAnimator the view really changes its actual position.

TranslateAnimation不是去precated,你仍然可以找到它的棒棒糖,但在大多数情况下,我可以推荐一个类的 ViewPropertyAnimator ,其中许多人仍然不似乎知道,这可能是最简单,最直接的方式,以动画的图,也可以为您节省大量的code。下面有一个例子:

TranslateAnimation is not deprecated, you can still find it on Lollipop, but for most cases I can recommend a class called ViewPropertyAnimator , which many people still dont seem to know about, it is probably the easiest and most straight forward way to animate a view, and can also save you a lot of code. Heres an example :

retractableLayout.animate()
                .translationX(toX)
                .translationY(toY)
                .setDuration(duration)
                .setInterpolator(interpolator)
                .setStartDelay(startDelay);

您还可以设置一个监听器等,一定要检查可用的方法。

You can also set a listener etc., be sure to check the available methods.

和看看这个真正有用的视频:

And check out this really helpful video :

https://www.youtube.com/watch?v=3UbJhmkeSig