0

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?

Piyush
  • 606
  • 4
  • 16
  • 38

2 Answers2

2

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.

Piyush
  • 606
  • 4
  • 16
  • 38
0

The easiest thing to do is create/inflate a new instance of the Animation type object for each view that needs animation. As you have it now, it's trying to reuse the same object.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • So basically create a new animation object each time the button clickMe is pressed, inside the rand if-else blocks? – Piyush Apr 08 '16 at 21:02
  • Yes, or create them all ahead of time and use them where necessary, making sure they each get used only once. – Doug Stevenson Apr 08 '16 at 21:04
  • I am not sure if this is because of the animation or onClick event. It is because the onClickListener is being invoked only once? – Piyush Apr 08 '16 at 21:30
  • I tried some logging and the problem is that 2nd click onwards my code doesn't reach the AnimationListener block – Piyush Apr 08 '16 at 21:49