Arn*_*rny 9 xml animation android view
我有一个带有几个视图的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>
Run Code Online (Sandbox Code Playgroud)
应用动画:
animation = AnimationUtils.loadAnimation(this.getContext(),
R.anim.rotate_around_center_point);
loadingRotatingCircleIV.startAnimation(animation);
Run Code Online (Sandbox Code Playgroud)
当动画中断时我找不到任何捕捉事件的方法,所以我可以在动画离开屏幕后重新开始动画.
只有当您将视图动画应用于该视图时,RecyclerView 才会错误地停止视图上的动画。如果您将使用属性动画,则一切正常。因此,以下解决方案将起作用:
ObjectAnimator animator = ObjectAnimator.ofFloat(loadingRotatingCircleIV, "rotation", 0.0f, 360.0f);
animator.setDuration(1000L);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.start();
Run Code Online (Sandbox Code Playgroud)
我现在正在做完全相同的事情,并且遇到完全相同的问题。所以问题是视图返回屏幕后如何重新启动动画。
我通过调用“loadingRotatingCircleIV.startAnimation(animation);”就解决了 99% 的问题 每次调用我的动画视图的位置时,都会在 onBindViewHolder 内部。
但还剩下一个小问题。如果你向下滚动一点,那么视图就会离开屏幕,但它的视图持有者不会被回收,当你向上滚动时,onBindViewHolder显然不会再次被调用,但动画会停止..所以我仍在尝试修复那个..
所以,我目前的解决方案:
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
...
if (position=="animated_view_position") view.startAnimation(animation);
...
}
Run Code Online (Sandbox Code Playgroud)
如果有人有更好的解决方案,请帮忙。