Billabong
Billabong

Reputation: 467

Stop an animation and start again

I want to stop an ObjectAnimation while it's running, when I click on the animated ImageView. Then, I want to play a FrameAnimation on that ImageView. After that, the first Animation starts again.

Here my OnClickListener:

OnClickListener click = new OnClickListener() {
            @Override
                public void onClick(View arg0) {


                try {
                    animator.wait(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                setFrameAnimation(view);


            }
        };

        view.setOnClickListener(click);

Animator is the ObjectAnimator Animation.
View is my ImageView

My setFrameAnimation-method:

AnimationDrawable frameAnimation = (AnimationDrawable)view.Background();
frameAnimation.start();

This code doesn't work. I get IllegalMonitorStateException when I call wait().

Upvotes: 1

Views: 908

Answers (1)

baloo
baloo

Reputation: 217

Use this 


int duration = 150;
        img = (ImageView)findViewById(R.id.imageView1);

        BitmapDrawable frame1 = 
            (BitmapDrawable)getResources().getDrawable(R.drawable.y1);
        BitmapDrawable frame2 = 
            (BitmapDrawable)getResources().getDrawable(R.drawable.y2);
        BitmapDrawable frame3 = 
            (BitmapDrawable)getResources().getDrawable(R.drawable.y3);
        BitmapDrawable frame4 = 
            (BitmapDrawable)getResources().getDrawable(R.drawable.y4);
        BitmapDrawable frame5 = 
            (BitmapDrawable)getResources().getDrawable(R.drawable.y5);


        animation = new AnimationDrawable();

        animation.addFrame(frame1, duration);
        animation.addFrame(frame2, duration);
        animation.addFrame(frame3, duration);
        animation.addFrame(frame4, duration);
        animation.addFrame(frame5, duration);
//        animation.addFrame(frame6, duration);
//        animation.addFrame(frame7, duration);
//        animation.addFrame(frame8, duration);
//        animation.addFrame(frame9, duration);
//        animation.addFrame(frame10, duration);


  img.post(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
             animation.setOneShot(false);
                img.setBackgroundDrawable(animation);
                animation.start();
        }
    });



        btnStart = (Button)findViewById(R.id.btnStart);
        btnStop = (Button)findViewById(R.id.btnStop);

        btnStart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                animation.start();
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                animation.stop();
            }
        });

Upvotes: 1

Related Questions