【问题标题】:Android view animation stops when view out of screen当视图超出屏幕时,Android视图动画停止
【发布时间】:2015-01-14 18:39:51
【问题描述】:

我有一个带有多个视图的 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);

当动画中断时,我找不到任何方法来捕捉事件,因此我可以在动画离开屏幕后重新启动它。

【问题讨论】:

    标签: android xml animation view


    【解决方案1】:
    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 方法也可以解决这个问题,选择你认为更容易的。
    【解决方案2】:

    如果您想将ViewAnimation 用于RecyclerView 项目,则必须在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);
        }
    }
    

    【讨论】:

      【解决方案3】:

      仅当您将视图动画应用于该视图时,RecyclerView 才会在视图离开屏幕时错误地停止视图上的动画。如果您将使用 Property Animation 一切正常。因此,以下解决方案将起作用:

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

      【讨论】:

        【解决方案4】:

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

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

        但是只剩下一个小问题。如果你向下滚动一点,所以视图离开屏幕,但它的视图持有者没有被回收,当你向上滚动时,显然不会再次调用 onBindViewHolder,但动画会停止..所以我仍在尝试修复那个..

        所以,我目前的解决方案:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-01
          • 2014-02-22
          • 1970-01-01
          • 1970-01-01
          • 2023-03-30
          相关资源
          最近更新 更多