2017-09-14 41 views
5

我有一個漫畫家,infinite_rotation,定義爲:ObjectAnimator具有無限旋轉口吃

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <objectAnimator 
     android:propertyName="rotation" 
     android:repeatCount="infinite" 
     android:valueFrom="0" 
     android:valueTo="360" 
     android:duration="2000" /> 
</set> 

當時間(時間不定)來,我不再需要這個,我叫infinite_animator.cancel()。然後發揮其佈局容器上的fade_out動畫:

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
     <objectAnimator 
      android:propertyName="alpha" 
      android:repeatCount="0" 
      android:valueTo="0.0" 
      android:duration="1000" /> 
</set> 

生成的動畫是,旋轉停止如預期,但口吃而淡出。我錯過了什麼?

下面是它的外觀:

enter image description here

更新: 我是老三星Tab鍵4奇巧OS測試上述問題。我剛剛在一款相當新款的帶有棉花糖操作系統的三星Tab 4上進行測試。動畫效果很好。所以我想更好的問題是如何修復舊設備/操作系統上的sl animation動畫?

這是動畫電話:

private void animateRefreshButton() { 
    ImageView iv_refresh = (ImageView) findViewById(R.id.iv_refresh); 

    if(infinite_animator == null) { 
     infinite_animator = AnimatorInflater.loadAnimator(this, R.animator.refresh_rotate); 
    } 
    infinite_animator.setTarget(iv_refresh); 
    infinite_animator.start(); 
} 

hideRefreshButton()當應用程序確定刷新完成啓動。這是取消並淡出動畫來電:

private void hideRefreshButton() { 
    if(infinite_animator != null) { 
     infinite_animator.cancel(); 
    } 

    Animator anim = AnimatorInflater.loadAnimator(this, R.animator.refresh_fadeout); 
    anim.addListener(new Animator.AnimatorListener() { 
     @Override 
     public void onAnimationStart(Animator animation) {} 

     @Override 
     public void onAnimationEnd(Animator animation) { 
      framelayout_container_of_iv_refresh.setVisibility(View.GONE); 
     } 

     @Override 
     public void onAnimationCancel(Animator animation) {} 

     @Override 
     public void onAnimationRepeat(Animator animation) {} 
    }); 
    anim.setTarget(framelayout_container_of_iv_refresh); 
    anim.start(); 
} 
+0

您是否嘗試爲您的旋轉動畫設置fillAfter爲true? – algrid

+0

沒有。我可以嘗試一下。謝謝。 – user1506104

+0

你能發表該行爲的gif嗎? – azizbekian

回答

6

我個人不使用Animation API因爲"problems" it has的。相反,在這種情況下,我會選擇Animators API

有這個片段:

 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     View view = findViewById(R.id.imageView); 

     ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, View.ROTATION, 0.0f, 360.0f); 

     objectAnimator.setDuration(2000); 
     objectAnimator.setRepeatCount(Animation.INFINITE); 
     objectAnimator.setInterpolator(new LinearInterpolator()); 

     objectAnimator.addListener(new AnimatorListenerAdapter() { 
      @Override 
      public void onAnimationCancel(Animator animation) { 
       view.animate() 
         .alpha(0.0f) 
         .setDuration(1000); 
      } 
     }); 

     objectAnimator.start(); 

     view.setOnClickListener((v) -> objectAnimator.cancel()); 

    } 
 

那麼這將是輸出:

enter image description here


我在模擬器測試(API 19):按預期工作。

+0

我也在使用ObjectAnimator。就我而言,我通過一個xml資源創建了動畫設計器。 – user1506104

+0

@ user1506104,現在我明白了,對於混淆抱歉。 – azizbekian

+0

@ user1506104,你能展示你如何開始和取消你的動畫,以便我們理解你的代碼爲什麼這樣工作? – azizbekian