我正在 Android 上制作一个应用程序,它会在单击按钮时生成两个可用图像之一,并且在 1 秒的持续时间后,该图像应该会消失,让用户可以选择再次单击该按钮。
问题是动画在第一次按下按钮时运行顺畅(即生成图像然后消失),但是在第二次按下按钮时,图像只是坐在那里,没有任何反应。我不知道为什么。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView firstimage = (ImageView)findViewById(R.id.imageView2);
final ImageView secondimage = (ImageView)findViewById(R.id.imageView1);
final Button clickMe = (Button)findViewById(R.id.button);
final TextView image_description = (TextView)findViewById(R.id.textView);
image_description.setText("");
final Animation fadeout = new AlphaAnimation(1,0);
fadeout.setStartOffset(1000);
fadeout.setDuration(1000);
secondimage.setAnimation(fadeout);
firstimage.setAnimation(fadeout);
image_description.setAnimation(fadeout);
secondimage.setVisibility(View.GONE);
firstimage.setVisibility(View.GONE);
image_description.setVisibility(View.GONE);
clickMe.setVisibility(View.VISIBLE);
fadeout.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation) {
System.out.println("Animation block");
secondimage.setVisibility(View.GONE);
firstimage.setVisibility(View.GONE);
image_description.setVisibility(View.GONE);
clickMe.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) { }
});
clickMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("Click block");
Random r = new Random();
int i = r.nextInt(2);
clickMe.setVisibility(View.GONE);
if(i == 0) {
secondimage.setVisibility(View.VISIBLE);
image_description.setText("LOOK it's a CAT");
image_description.setVisibility(View.VISIBLE);
secondimage.setAnimation(fadeout);
image_description.setAnimation(fadeout);
} else {
firstimage.setVisibility(View.VISIBLE);
image_description.setText("LOOK it's a DOG");
image_description.setVisibility(View.VISIBLE);
firstimage.setAnimation(fadeout);
image_description.setAnimation(fadeout);
}
}
});
}
日志看起来像这样:
Click Block
Animation Block
Click Block
Click Block
Click Block
Click Block
知道为什么代码没有在第二次点击时到达动画块吗?