2014-01-19 87 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