2017-09-25 32 views
0

希望我不是在这里重复一个问题;我找不到多个ViewPropertyAnimators。目标是在8秒钟内从y1到y2生成一个视图。在第一秒淡出,然后在最后一秒淡出。多个ViewPropertyAnimators

以下是我在已经尽了活动的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的所有在一秒钟。然后6秒后视图淡出。它看起来每次我调用View.animate()它返回相同的ViewPropertyAnimator。有没有办法让我可以有多个?我正在考虑将视图的alpha设置为动画,将视图嵌套在相对布局中,然后对相关的布局转换进行动画处理。如果我不需要,我宁愿不走那条路。有谁知道更好的解决方案?

回答

1

您可以直接使用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();