动画只在第一次触发

Animation only triggers the first time

我正在 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

知道为什么代码在第二次点击之后没有到达动画块吗?

最简单的做法是create/inflate为每个需要动画的视图创建一个新的动画类型对象实例。正如您现在拥有的那样,它正在尝试重复使用同一个对象。

好的。我能够解决自己的问题。

我换了

secondimage.setAnimation(fadeout);

secondimage.startAnimation(fadeout);

这样做,代码能够到达 onAnimationEnd 函数。