Android 淡入淡出动画问题
Android Fade In, Fade Out animation issue
我正在尝试创建一个简单的程序,它有一个按钮和一个位于其正下方的隐藏按钮。当用户单击第一个按钮时,第二个按钮会使用流畅的动画出现。当用户再次点击第一个按钮时,第二个按钮顺利消失,等等。我正在使用 Alpha Animation 来设置出现和消失动作的动画,但我 运行 遇到以下问题:
The first time I click the button, nothing happens. When I click the button again, it appears and then disappears instantly. When I click the button a third time, it smoothly animates the "fade-in" motion. And then when I click it a fourth time, it smoothly animates the "fade-out" motion, etc...
下面是这个简单程序中的代码:
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 开发中遇到动画。任何建议和意见将不胜感激!
您不需要变量 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);
}
}
});
我正在尝试创建一个简单的程序,它有一个按钮和一个位于其正下方的隐藏按钮。当用户单击第一个按钮时,第二个按钮会使用流畅的动画出现。当用户再次点击第一个按钮时,第二个按钮顺利消失,等等。我正在使用 Alpha Animation 来设置出现和消失动作的动画,但我 运行 遇到以下问题:
The first time I click the button, nothing happens. When I click the button again, it appears and then disappears instantly. When I click the button a third time, it smoothly animates the "fade-in" motion. And then when I click it a fourth time, it smoothly animates the "fade-out" motion, etc...
下面是这个简单程序中的代码:
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 开发中遇到动画。任何建议和意见将不胜感激!
您不需要变量 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);
}
}
});