2014-01-19 93 views
0

我想實現一些RotatingImageView和我有一個動畫看起來像這樣:ViewSwitcher和自定義動畫的ImageView

public void init() { 

    mCurrentRotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.51f); 
    mCurrentRotateAnimation.setDuration(0); 
    mCurrentRotateAnimation.setRepeatCount(Animation.INFINITE); 
    mCurrentRotateAnimation.setRepeatMode(Animation.RESTART); 
    mCurrentRotateAnimation.setInterpolator(new InfoInterpolator()); 
    startAnimation(mCurrentRotateAnimation); 
} 

的困難出現時,這RotatingImageView開始在ViewSwitcher使用(這是一ViewAnimator)和ViewSwitcher改變自己依賴於輸入/輸出動畫有動畫(基本上是通過調用startAnimation()帶着自己的進/出動畫,將覆蓋我的旋轉動畫

// ViewSwitcher/ViewAnimator code changing the animation  
void showOnly(int childIndex, boolean animate) { 
    final int count = getChildCount(); 
    for (int i = 0; i < count; i++) { 
     final View child = getChildAt(i); 
     if (i == childIndex) { 
      if (animate && mInAnimation != null) { 
       child.startAnimation(mInAnimation); 
      } 
      child.setVisibility(View.VISIBLE); 
      mFirstTime = false; 
     } else { 
      if (animate && mOutAnimation != null && child.getVisibility() == View.VISIBLE) { 
       child.startAnimation(mOutAnimation); 
      } else if (child.getAnimation() == mInAnimation) 
       child.clearAnimation(); 
      child.setVisibility(View.GONE); 
     } 
    } 
} 

我試圖通過保留假內插器的輸入來「知道」旋轉動畫停止的位置,並在ViewSwitcher調用startAnimation時將其恢復,但旋轉動畫中仍存在「跳躍」!

我該如何讓ImageView始終在旋轉,並且添加並移除來自ViewSwitcher/ViewAnimator的正確輸入/輸出動畫而不停止旋轉動畫

非常感謝!

+0

您使用的是'ImageView'作爲'ViewSwitcher'的直接子(被放置在res /動畫)? – Luksprog

+0

是的確...... hooo這是一個好主意...... – galex

+0

嘗試在另一個佈局中包裝它,比如'FrameLayout'。 – Luksprog

回答

1

如果您在通過ViewSwitcher更改其可見性時使用舊的動畫系統,則您的動畫視圖將一直閃爍。 解決方案是使用一個ViewPropertyAnimator,它實際上會修改視圖的屬性,這就是爲什麼即使被viewSwitcher隱藏後視圖仍然保持其位置(這裏是它的角度)的原因。

這裏是一個基於你的RotatingImageView的例子,如果你的目標是你應該使用NineOldAndroids

@SuppressLint("NewApi") 
private void init() { 
    ObjectAnimator rotationAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(getContext(), R.animator.rotation); 
    rotationAnimator.setTarget(this); 
    rotationAnimator.start(); 
} 

這裏是rotation.xml

<?xml version="1.0" encoding="utf-8"?> 
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="3000" 
    android:propertyName="rotation" 
    android:repeatCount="infinite" 
    android:repeatMode="restart" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:valueTo="360" 
    android:valueType="floatType" /> 
+0

就是這樣,完美!謝謝! – galex