2017-09-25 7 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에서 1의 모든 1 초 0에서 800 번역 및 알파입니다. 6 초 후보기가 사라집니다. View.animate()를 호출 할 때마다 동일한 ViewPropertyAnimator를 반환합니다. 내가 그 중 여러 개를 가질 수있는 방법이 있습니까? 뷰의 알파에 애니메이션을 적용하고 상대 레이아웃에서 뷰를 중첩 한 다음 상대 레이아웃 번역에 애니메이션을 적용하는 방법에 대해 생각했습니다. 내가 그럴 필요가 없다면 나는 그 길로 가지 않을 것입니다. 누구든지 더 나은 해결책을 알고 있습니까?

답변

1

.animate() 추상화 대신에 ObjectAnimator 인스턴스를 직접 사용하여이 문제를 해결할 수 있습니다.

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();