java - AlphaAnimation : Fade out works, 但不淡入

标签 java android alpha

我有以下代码:

public boolean stopped = false;

public void fadeOut(final ImageView obj, final int time, int delay)
    {
        if(stopped)
        {
            stopped = false;
            anim = new AlphaAnimation(1.00f, 0.00f);
            anim.setDuration(time);
            if(delay > 0)
            {
                anim.setStartOffset(delay);
            }
            anim.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub
                    Log.v("FADER", "Fading out. Duration: " + time + "ms.");

                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    // TODO Auto-generated method stub
                    Log.v("FADER", "Fading out finished.");
                    //obj.setAlpha(255);
                    stopped = true;
                }
            });
            obj.startAnimation(anim);
        }
    }

这段代码工作正常。我的对象(一个 ImageView)漂亮地淡出。但是当我运行这个时:

public void fadeIn(final ImageView obj, final int time, int delay)
    {
        if(stopped)
        {
            stopped = false;
            anim = new AlphaAnimation(0, 1);
            anim.setDuration(time);
            if(delay > 0)
            {
                anim.setStartOffset(delay);
            }
            anim.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub
                    Log.v("FADER", "Fading in. Duration: " + time + "ms.");

                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    // TODO Auto-generated method stub
                    Log.v("FADER", "Fading in finished.");
                    //obj.setAlpha(255);
                    stopped = true;
                }
            });
            obj.startAnimation(anim);
        }
    }

它不起作用 - 没有淡入。ImageView 保持完全透明。

这是为什么,有什么想法吗?

对于那些想知道的人,是的,我已经声明并设置了 boolean 值 stopped,所以这不是问题,因为当我查看 LogCat 时,它在运行 fadeIn() 函数。

最佳答案

解决了!

事实证明,如果您使用 [ImageView Object].setAlpha,并将其设置为 0,那么当您运行 AlphaAnimation 时,它在 0 的边界和图像的当前 alpha 之间工作。

所以,如果你想在淡出后保持图像不可见,解决方案是使用[ImageView Object].setVisibility(View.INVISIBLE),并将其设置回View .VISIBLE 在运行动画之前。

任务完成。

关于java - AlphaAnimation : Fade out works, 但不淡入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10772601/

相关文章:

ios - RGBA 的 CGColorSpace 在哪里?

java - 需要属性 'entityManagerFactory'

用于字节操作的 Java RandomAccessFile 与 DataInputStream

android - java.lang.NoSuchMethodError : android. 应用程序通知$Builder.setColor

java - 找不到 DriveId。您有权查看此文件吗? Android 谷歌驱动集成

android - AlphaAnimation 不起作用

java - 整数不像真正的对象?

javascript - HTML 表单未提交

android - 当 listview 滚动时 edittext 设置默认值

java - 如何在java中清除BufferedImage的像素?