【问题标题】:Android Animation FlickerAndroid 动画闪烁
【发布时间】:2012-03-12 08:42:14
【问题描述】:

我一直在搜索关于这个主题的尽可能多的线程,我可以在 Android 2.2 中处理 AnimationListeners 时出现的闪烁中找到这些线程,但我无法完全解决我的问题。

我得到的是一个 LinearLayout 'popover',用户触摸它可以向下移动大约 100 像素,然后再次触摸它可以向上移动。我终于让它在第一部分工作,没有任何闪烁(感谢在动画视图上调用 clearAnimation() 的建议),但是当做相反的事情(即,将视图向上移动)时,有一个闪烁开始。我真的不能在 onAnimationStart() 方法中调用 clearAnimation() 因为它不会动画!

当然,如果我在没有任何动画侦听器的情况下使用 setFillAfter(),所有动画都可以完美运行,但是视图的触摸区域不会移动(因为视图本身并没有“实际”移动)。

任何帮助将不胜感激。

this.popoverTab.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        popoverTab.setClickable(false);
        popoverTab.setFocusable(false);
        if (popoverHidden) {
            Log.d(TAG, "About to show popover");
            // the popover is currently hidden, show it.
            TranslateAnimation animation = new TranslateAnimation(0, 0, 100, 0);
            animation.setDuration(700);
            animation.setFillBefore(true);
            animation.setAnimationListener(new AnimationListener() {
                public void onAnimationEnd(Animation animation) {

                }

                public void onAnimationRepeat(Animation animation) {

                }

                public void onAnimationStart(Animation animation) {
                    footer.layout(footer.getLeft(), (footer.getTop() - 100), footer.getRight(), footer.getBottom());
                }
            });
            footer.startAnimation(animation);
        } else {
            Log.d(TAG, "About to hide popover");
            // the popover is showing, hide it.
            TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 100);
            animation.setDuration(700);
            animation.setFillAfter(true);
            animation.setAnimationListener(new AnimationListener() {
                public void onAnimationEnd(Animation animation) {
                    footer.clearAnimation();
                    footer.layout(footer.getLeft(), (footer.getTop() + 100), footer.getRight(), footer.getBottom());
                }

                public void onAnimationRepeat(Animation animation) {

                }

                public void onAnimationStart(Animation animation) {

                }
            });
            footer.startAnimation(animation);
        }
        // invert.
        popoverHidden = !popoverHidden;
        popoverTab.setClickable(true);
        popoverTab.setFocusable(true);
    }

});

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    您不应该在onAnimationEnd() 上使用clearAnimation()。

    试试这个:

    1. 在两个动画上使用setFillBefore(true)和setFillAfter(true)
    2. 在开始和结束两个动画时设置正确的布局属性

    【讨论】:

    • 我在向上移动面板时仍然闪烁。我通过在其后面放置一个包含与前面相同内容的虚拟视图来解决此问题。
    • 我也遇到了同样的问题,好像调整view.layout()会导致闪烁。
    【解决方案2】:

    我遇到了同样的问题,几天后我找到了解决方案……谢谢:

    http://www.mail-archive.com/android-developers@googlegroups.com/msg67535.html

    我想出了解决这个问题的办法。线索来自事实 显示视图时,一切正常。 显然,当动画运行时,更新将是 由节目强迫发生在后台,不会导致 闪烁。在后端添加一个简短的动画 onAnimationEnd() 当我们隐藏视图时使闪烁消失 离开。

    这是工作代码中的新 onAndimationEnd()

      public void onAnimationEnd(Animation animation) {
                animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
                animation.setDuration(1);
                mPlayer0Panel.startAnimation(animation);
       } 
    

    【讨论】:

    • 非常感谢您解决了这个问题!
    • 非常感谢。我花了太长时间才找到这个。
    • 我有眨眼问题(我不知道闪烁和眨眼是否是一回事),这解决了这个问题。
    • 搜索了很长时间后,这节省了我的时间。
    • 它适用于我的项目,该项目使用使用 Rebound(Facebook) 和 Backboard(Tumblr) 的浮动操作按钮。
    【解决方案3】:

    Alrite,我遇到了同样的问题,让我搜索它,我最终找到了这篇文章。为我的问题找到了解决方案,想分享我的解决方案。

    我运行了许多动画,但最近开始闪烁,但是当我跟踪问题时发现在 for 循环下绘制动画列表后发生闪烁。 (数组列表)

    我只是简单的加了try catch来避免定位后出现的问题,问题;一些动画被即时删除,但没有足够的时间让线程更新,所以循环仍然尝试并失败但没有显示,而是闪烁,但在数组列表绘图上的 try and catch 下修复了我的问题。

    【讨论】:

      【解决方案4】:

      我搜索了所有关于动画问题(闪烁和迟缓)的 stackoverflow 帖子。 我没有找到任何完美的答案。但是我找到了相同的解决方案,如下所示,

      动画使用的开始,

      view_you_want_to_animate.setDrawingCacheEnabled(true);
      

      onEnd of Animation use,

      view_you_want_to_animate.setDrawingCacheEnabled(false);
      

      现在,当我的视图设置动画时,我的视图不会出现闪烁或迟缓行为。 对我来说效果很好。

      【讨论】:

      • 这也适用于我,无论是独立的 alpha 动画还是平移 + 缩放动画集。
      • setDrawingCacheEnabled 已弃用。
      【解决方案5】:
      @Override
      public void onAnimationEnd(Animation animation)
      {
          footer.clearAnimation();
      }
      

      这对我有用。

      【讨论】:

        【解决方案6】:

        我遇到了同样的错误,但它只出现在一些视图中,尽管所有视图都有相同的实现。事实证明,它的发生有两个原因:

        • 当我的根 xml 元素中有 android:animateLayoutChanges="true" 时
        • 当有问题的视图直接连接到根 xml 元素时

        所以,有两个解决方案,要么删除android:animateLayoutChanges="true" 标签(建议因为你没有使用它),或者在有问题的视图周围使用包装布局!所以而不是:

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:animateLayoutChanges="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        
            <LinearLayout
                android:id="@+id/btnClose"
                android:layout_width="50dp"
                android:layout_height="50dp">
        
                <ImageView
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:layout_gravity="center"
                    android:src="@drawable/ic_arrow_back" />
            </LinearLayout>
        ...
        </RelativeLayout>
        

        ,做

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:animateLayoutChanges="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        
            <LinearLayout
                android:layout_width="50dp"
                android:layout_height="50dp">
        
                <LinearLayout
                    android:id="@+id/btnClose"
                    android:layout_width="50dp"
                    android:layout_height="50dp">
        
                    <ImageView
                        android:layout_width="32dp"
                        android:layout_height="32dp"
                        android:layout_gravity="center"
                        android:src="@drawable/ic_arrow_back" />
                </LinearLayout>
            </LinearLayout>
            ...
        </RelativeLayout>
        

        【讨论】:

        • 此线程中被低估的答案之一。动画后我的视图闪烁的原因是因为android:animateLayoutChanges="true"。我认为它与以编程方式编码的Transition 重叠。删除 android:animateLayoutChanges="true" 解决了我的问题。
        猜你喜欢
        • 2012-09-26
        • 1970-01-01
        • 2011-12-06
        • 1970-01-01
        • 2013-09-26
        • 1970-01-01
        • 2011-12-02
        • 2011-03-30
        • 2011-08-23
        相关资源
        最近更新 更多