本文介绍了通过ValueAnimator动画保证金VS ViewPropertyAnimator translationX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在动画的android的初学者。我有一个RelativeLayout的内部一些看法,我想改变视图位置。什么是我有选择,他们有什么不同?

我曾尝试以下操作:

  view.animate()
.translationX(TOX)
.setDuration(持续时间);

  RelativeLayout.MarginLayoutParams PARAMS =(RelativeLayout.MarginLayoutParams)view.getLayoutParams();
ValueAnimator动画= ValueAnimator.ofInt(params.rightMargin,100);
animator.addUpdateListener(新ValueAnimator.AnimatorUpdateListener(){
    @覆盖
    公共无效onAnimationUpdate(ValueAnimator valueAnimator){
        params5.rightMargin =(整数)valueAnimator.getAnimatedValue();
    }
});

两者改变视图的位置。谁能给我解释一下这两种方法的差异。我还有什么别的选择,哪个是preferred选项。


解决方案

  view.animate()
.translationX(TOX)
.setDuration(持续时间);

我认为这是preferred之一,因为它不会在每次更新调用措施()和布局()作为第二个会。

而在一般情况:

- translationX是为了规范其父
内的孩子的位置
- 执行动画通过改变保证金的参数是不是一个好主意(它的意思是设置一次,要改变很少,如果)

I am a beginner in android animation. I have few views inside a RelativeLayout and i wish to change view position. What are the options i have and how they differ?

I have tried following:

view.animate()
.translationX(toX)
.setDuration(duration);

and

RelativeLayout.MarginLayoutParams params = (RelativeLayout.MarginLayoutParams) view.getLayoutParams();
ValueAnimator animator = ValueAnimator.ofInt(params.rightMargin, 100);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        params5.rightMargin = (Integer) valueAnimator.getAnimatedValue();
    }
});

Both changes the position of the view. Can anyone explain me the difference in these two method. What other options i have and which is the preferred option.

解决方案
view.animate()
.translationX(toX)
.setDuration(duration);

I think it's preferred one, because it doesn't call measure() and layout() on each update as the second one would.

And in general:
- translationX is meant to regulate the position of a child within its parent
- perform animation through changing margin parameter isn't a good idea (it's meant to be set once and to be changed rarely if ever)

这篇关于通过ValueAnimator动画保证金VS ViewPropertyAnimator translationX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:29