Android淡入,淡出动画问题

时间:2017-10-03 22:40:14

标签: android animation button fadein fadeout

我正在尝试创建一个简单的程序,它上面有一个按钮和一个隐藏按钮。当用户单击第一个按钮时,第二个按钮将使用平滑动画显示。当用户再次单击第一个按钮时,第二个按钮会平滑消失,等等。我正在使用Alpha Animation为显示和消失的操作设置动画,但我遇到了以下问题:

  

第一次时间我点击按钮,没有任何反应。当我再次单击该按钮时,它会出现,然后立即消失。当我点击按钮第三次时,它会平滑地动画"淡入"运动。然后,当我第四次点击它时,它可以平滑地激活"淡出"动作等...

enter image description here enter image description here

以下是这个简单程序中的代码:

public class MainActivity extends AppCompatActivity {
public Button toggleButton;
public Button helloButton;
public boolean isVisible = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    helloButton = (Button)findViewById(R.id.helloButton);

    toggleButton = (Button)findViewById(R.id.toggleButton);
    toggleButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (!isVisible) {
                //helloButton.setVisibility(View.VISIBLE);
                AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1); // start alpha, end alpha
                fadeInAnimation.setDuration(250); // time for animation in milliseconds
                fadeInAnimation.setFillAfter(true); // make the transformation persist
                fadeInAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        helloButton.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) { }

                    @Override
                    public void onAnimationStart(Animation animation) {
                        helloButton.setVisibility(View.GONE);
                    }
                });
                helloButton.startAnimation(fadeInAnimation);
                isVisible = true;
                System.out.println("Button is invisible... Time to Fade IN");
            }
            else {
                //helloButton.setVisibility(View.GONE);
                AlphaAnimation fadeOutAnimation = new AlphaAnimation(1, 0); // start alpha, end alpha
                fadeOutAnimation.setDuration(250); // time for animation in milliseconds
                fadeOutAnimation.setFillAfter(true); // make the transformation persist
                fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        helloButton.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) { }

                    @Override
                    public void onAnimationStart(Animation animation) {
                        helloButton.setVisibility(View.VISIBLE);
                    }
                });
                helloButton.startAnimation(fadeOutAnimation);
                isVisible = false;
                System.out.println("Button is visible... Time to Fade OUT");
            }
        }
    });

  }
}

我觉得这很简单;然而,这是我第一次在Android开发中遇到过动画。任何建议和意见将不胜感激!

1 个答案:

答案 0 :(得分:2)

您不需要变量isVisible,只需检查按钮是否可见。
要首先查看fadeInAnimation,您需要使视图可见。

我也更改了两个动画的持续时间,请检查此代码:

helloButton = findViewById(R.id.helloButton);
toggleButton = findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (helloButton.getVisibility() == View.GONE) {
            AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
            fadeInAnimation.setDuration(2500); 
            fadeInAnimation.setFillAfter(true);
            fadeInAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationEnd(Animation animation) {
                }

                @Override
                public void onAnimationRepeat(Animation animation) { }

                @Override
                public void onAnimationStart(Animation animation) {
                    helloButton.setVisibility(View.VISIBLE);
                }
            });
            helloButton.startAnimation(fadeInAnimation);
        } else {
            AlphaAnimation fadeOutAnimation = new AlphaAnimation(1, 0); 
            fadeOutAnimation.setDuration(2500); 
            fadeOutAnimation.setFillAfter(true); 
            fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationEnd(Animation animation) {
                    helloButton.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) { }

                @Override
                public void onAnimationStart(Animation animation) {
                }
            });
            helloButton.startAnimation(fadeOutAnimation);
        }
    }
});