代码之家  ›  专栏  ›  技术社区  ›  user8463863

Android淡入淡出动画问题

  •  3
  • user8463863  · 技术社区  · 7 年前

    Alpha Animation

    这个 第一 当我点击按钮时,什么都没发生。当我再次单击按钮时,它会出现,然后立即消失。当我点击按钮a时 第三 时间,它平滑地设置“淡入”运动的动画。然后,当我第四次点击它时,它会平滑地为“淡出”运动设置动画,等等。。。

    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 回复  |  直到 7 年前
        1
  •  4
  •   Juan Cruz Soler    7 年前

    你不需要变量 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);
            }
        }
    });