动画在ANDROID中闪烁

时间:2012-08-23 09:04:15

标签: android animation flicker

我正在尝试在ADNROID中实现相对简单的东西。我有一堆存储在列表中的Drawables。我想创建一个效果,其中drawables使用淡入效果逐个显示在彼此之上。

我使用下面的代码取得了部分成功。它实际上在我的手机(Nexus S)上运行完美,但它在我的平板电脑上显示出闪烁(华硕TF101)。这可能是因为平板电脑具有更快的CPU。

以下是我的设置:我已将所有Drawables存储在drawables列表中。我还在布局中定义了两个图像,一个在另一个上面:imageViewForegroundimageViewBackground

我们的想法是首先设置背景图像,然后开始一个前景图像从alpha-0开始并转到alpha-1的动画。然后用新的前景图像替换背景,选择一个新的前景(即下一个可绘制的)并永远循环。

counter对象是int计数器的简单包装器。

这是我的代码:

final Animation fadeInAnimation = new AlphaAnimation(0f, 1f);
fadeInAnimation.setDuration(2000);
fadeInAnimation.setStartOffset(3000);
fadeInAnimation.setFillBefore(false);
fadeInAnimation.setFillAfter(true);
fadeInAnimation.setRepeatCount(Animation.INFINITE);
fadeInAnimation.setRepeatMode(Animation.RESTART);
fadeInAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
        imageViewForeground.setImageDrawable(drawables.get(counter.value()));
        Log.d(TAG, "onAnimationStart");
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        Log.d(TAG, "onAnimationEnd");
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        imageViewBackground.setImageDrawable(drawables.get(counter.value()));
        counter.increase();
        // the problem appears in this line, 
        // where the foreground becomes visible for a very small period, 
        // causing the flickering
        imageViewForeground.setImageDrawable(drawables.get(counter.value()));
    }
});

imageViewForeground.startAnimation(fadeInAnimation);

我有什么想法可以克服这个闪烁的问题?

0 个答案:

没有答案
相关问题