Search code examples
androidandroid-animationimage-generation

Animation only triggers the first time


I am making an application on Android which generates one of the two available images on clicking a button and after a duration of 1 second, that image is supposed to fade away, giving the user a choice to click the button again.

The problem is that the animation works smoothly on the first button press (i.e. generates an image and then fades away), however on the second button press, the image just sits there and nothing happens. I can't figure out why.

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);
                }    
            }
        });    
    }

The Logs look something like this:

Click Block
Animation Block
Click Block
Click Block
Click Block
Click Block

Any idea why the code is not reaching the Animation block on 2nd click onwards?


Solution

  • Alright. I was able to solve my own query.

    I replaced

    secondimage.setAnimation(fadeout);
    

    with

    secondimage.startAnimation(fadeout);
    

    On doing so, the code was able to reach the onAnimationEnd function.