0

希望我不是在这里重复一个问题;我找不到一个关于 multipleViewPropertyAnimators的。目标是让视图在 8 秒内从 y1 动画到 y2。在第一秒淡出,然后在最后一秒淡出。

这是我在我的活动中尝试过的onCreate()

final View animatingView = findViewById(R.id.animateMe);


    animatingView.post(new Runnable() {
        @Override
        public void run() {
            //Translation
            animatingView.setY(0);
            animatingView.animate().translationY(800).setDuration(8000);

            //Fading view in
            animatingView.setAlpha(0f);
            animatingView.animate().alpha(1f).setDuration(1000);

            //Waiting 6 seconds and then fading the view back out
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    animatingView.animate().alpha(0f).setDuration(1000);
                }
            }, 6000);
        }
    });

但是,结果是从 0 到 800 的转换,以及从 0 到 1 的 alpha 都在一秒钟内完成。然后 6 秒后视图淡出。每次我调用 View.animate() 时,它看起来都会返回相同的 ViewPropertyAnimator。有没有办法让我拥有多个?我正在考虑为视图的 alpha 设置动画,将视图嵌套在相对布局中,然后为相对布局翻译设置动画。如果没有必要,我宁愿不走那条路。有谁知道更好的解决方案?

4

1 回答 1

3

您可以通过ObjectAnimator直接使用实例而不是使用.animate()抽象来解决这个问题。

ObjectAnimator translationY = ObjectAnimator.ofFloat(animatingView, "translationY", 0f, 800f);
translationY.setDuration(8000);

ObjectAnimator alpha1 = ObjectAnimator.ofFloat(animatingView, "alpha", 0f, 1f);
alpha1.setDuration(1000);

ObjectAnimator alpha2 = ObjectAnimator.ofFloat(animatingView, "alpha", 1f, 0f);
alpha2.setDuration(1000);
alpha2.setStartDelay(7000);

AnimatorSet set = new AnimatorSet();
set.playTogether(translationY, alpha1, alpha2);
set.start();
于 2017-09-25T04:32:38.077 回答