Android视图动画停止播放当视图超出屏幕

9
我正在使用一个带有多个视图的recyclerView,其中之一是动画视图,该视图应用了一个动画。一旦视图离开屏幕,即使动画仍然存在,动画也不再起作用。
数据:
rotate_around_center_point.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <rotate
        android:duration="2500"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:toDegrees="360" />

</set>

应用动画:
                animation = AnimationUtils.loadAnimation(this.getContext(),
                        R.anim.rotate_around_center_point);
                loadingRotatingCircleIV.startAnimation(animation);

我找不到任何方法来捕获动画被中断的事件,以便在动画离开屏幕后能够重新启动它。

4个回答

7

当视图从屏幕上消失时,RecyclerView 在应用视图动画时错误地停止了该视图的动画。但是,如果您使用属性动画,一切都将正常工作。因此,以下解决方案将起作用:

ObjectAnimator animator = ObjectAnimator.ofFloat(loadingRotatingCircleIV, "rotation", 0.0f, 360.0f);
animator.setDuration(1000L);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.start(); 

4

如果你想为RecyclerView项使用ViewAnimation,你需要在RecyclerView的重写方法onViewAttachedToWindow(ItemViewHolder holder)中恢复动画效果,例如:

@Override
public void onViewAttachedToWindow(ItemViewHolder holder) {
    super.onViewAttachedToWindow(holder);

    if (holder.animated) 
    {
        Animation anim = new ScaleAnimation(0.8f, 1.2f, 
            0.8f, 1.2f,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setFillAfter(true);
        anim.setInterpolator(new BounceInterpolator());
        anim.setDuration(1100);
        anim.setRepeatCount(Animation.INFINITE);
        anim.setRepeatMode(Animation.REVERSE);
        holder.animatedView.startAnimation(anim);
    }
}

0

我现在正在做完全相同的事情,并且遇到了完全相同的问题。所以问题是如何在视图重新出现在屏幕上时重新启动动画。

我通过在每次调用我的动画视图位置的onBindViewHolder内调用“loadingRotatingCircleIV.startAnimation(animation);”来解决99%的问题。

但是还有一个小小的问题。如果您只向下滚动一点,使视图离开屏幕,但其视图持有者未被回收,则向上滚动时显然不会再次调用onBindViewHolder,但动画会停止。 所以我仍在努力解决这个问题。

因此,我的当前解决方案:

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
...
        if (position=="animated_view_position") view.startAnimation(animation);
...
}

如果有更好的解决方案,请帮忙。


0
create **HashMap<Integer, Boolean>** for saving each items animation loaded status

construct (){
for(int c = 0; c < adapterDataList.size(); c++){
//Here is create default status of animation loaded status used Boolean to saving
}
}

//Call Start Animation with onViewAttachedToWindow
@Override
public void onViewAttachedToWindow(ItemViewHolder holder) {
    super.onViewAttachedToWindow(holder);

// get HashMap of each items Animation status like this
if(!HashMap.get(position)){ //FALSE means animation not loaded yet
//Here should call start Animation method;
} else {
//Here call no animation method, like setImageDrawable etc...
}

   //create an AnimationListener for each item, when an animation is End, Listener should call a method to change HashMap above which you just created, like this **HashMap.put(position, Boolean.valueOf(true))** //TRUE means animation loaded
}

//要获取位置值作为整数,请使用holder.getLayoutPosition()


使用holder.setTag方法也可以解决这个问题,只需选择您认为更容易的方法即可。 - chundk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接