Android 动画闪烁

发布于 2025-01-08 05:07:34 字数 2393 浏览 2 评论 0原文

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

我得到的是一个 LinearLayout“弹出框”,用户触摸它可以向下移动大约 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);
    }

});

I've been trawling through as many threads on this topic that I can find on the flicker that arises in Android 2.2 when dealing with AnimationListeners, but I can't quite solve my issue.

What I've got is a LinearLayout 'popover' that the user touches to move down about 100 pixels, and touches again to move it back up. I've finally got it working on the first part without any flicker (thanks to the suggestion to call clearAnimation() on the view being animated), but when doing the opposite (ie, moving the view back up), there's a flicker at the start. I can't really call clearAnimation() in the onAnimationStart() method as it won't animate!

Of course, all animation works perfectly if I used setFillAfter() without any animation listener, but then the view's touch area won't move (because the view itself hasn't "actually" moved).

Any help would be greatly appreciated.

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);
    }

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

云淡月浅 2025-01-15 05:07:34

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

http://www.mail-archive.com/[电子邮件受保护]/msg67535.html

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

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

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

I had the same problem and after few days I found the solution ... thanx to:

http://www.mail-archive.com/[email protected]/msg67535.html

I figured out a solution to this problem. The clue came from the fact
that when showing the view, everything worked fine.
Apparently, when the animation is running, the update that would be
forced by the show happens in the background and doesn't cause the
flicker. Adding a short animation to the back end of the
onAnimationEnd() when we are hiding the view makes the flicker go
away.

Here is the new onAndimationEnd() in the working code

  public void onAnimationEnd(Animation animation) {
            animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
            animation.setDuration(1);
            mPlayer0Panel.startAnimation(animation);
   } 
半仙 2025-01-15 05:07:34
@Override
public void onAnimationEnd(Animation animation)
{
    footer.clearAnimation();
}

这对我有用。

@Override
public void onAnimationEnd(Animation animation)
{
    footer.clearAnimation();
}

This worked for me.

耳根太软 2025-01-15 05:07:34

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

  • 当我的根 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>

I faced the same error, but it only appeared in some views, although all had the same implementation. As it turned out, it happened for two reasons:

  • When I had android:animateLayoutChanges="true" in my root xml element
  • When the problematic view was directly connected to the root xml element

So, two solutions, either remove the android:animateLayoutChanges="true" tag (suggested since you're not using it), or use a wrapper Layout around the problematic view! So instead of:

<?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>

, do

<?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>
清晨说晚安 2025-01-15 05:07:34

您不必在 onAnimationEnd() 上使用 clearAnimation()

试试这个:

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

You shouldn't have to use clearAnimation() on onAnimationEnd().

Try this:

  1. Use setFillBefore(true) and setFillAfter(true) on both animations
  2. Set the correct layout properties when starting and when ending both animations
没︽人懂的悲伤 2025-01-15 05:07:34

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

动画使用的onStart,

view_you_want_to_animate.setDrawingCacheEnabled(true);

动画使用的onEnd,

view_you_want_to_animate.setDrawingCacheEnabled(false);

现在当我的视图动画时,我的视图没有闪烁或迟缓的行为。
这对我来说效果很好。

I searched for all stackoverflow posts for animation issue (flicker and sluggish).
I didnt find any perfect answer. But I have found the solution for the same, which is as below,

onStart of Animation use,

view_you_want_to_animate.setDrawingCacheEnabled(true);

onEnd of Animation use,

view_you_want_to_animate.setDrawingCacheEnabled(false);

Now my view does not have flicker or sluggish behavior when my view is animating.
It works well for me.

汹涌人海 2025-01-15 05:07:34

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

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

我只是添加了try catch来避免定位后的问题,问题;一些动画被动态删除,但没有足够的时间来更新线程,因此循环仍然尝试并失败,但没有显示,而是闪烁,但在数组列表绘图上的 try 和 catch 下解决了我的问题。

Alrite, I had the same issue that made me search for it, I ended up in this post. Found a solution for my issue, thought to share my solution.

I had many animations running but flickering started recently, but when I traced the issue found the flickering occured after drawing list of animations under a for loop. (array List)

I simply added try catch to avoid the issue after locating it, the problem; some of the animations were removed on fly but there were not enough time for a thread to update, so the loop still tried and failed but not shown instead it flickered, but under try and catch on the array list drawing fixed my issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文