StacyM
StacyM

Reputation: 1036

Android: trying to add a wait in between two animations, getting Object not locked by thread before wait() error

I'm new to Android and Java programming. I created an app for a Coursera class. It has two animations that occur repeatedly. I want them to animate like a drum beat - 1,2,1,2,1,2,1,2,1,2.

I wrote two recursive methods that run until the user clicks a Reset button. I tried adding a wait in between the method calls so the second would kick off right after the first one.

This is the part of my code that has the wait.

mHandler = new Handler(); // .os package class when importing
        mLeftfoot = findViewById(R.id.leftfoot);
        mRightfoot = findViewById(R.id.rightfoot);  
        mFootAnim = AnimationUtils.loadAnimation(this, R.anim.foot); //this looks to the foot.xml file for the animations
        leftstepRecursive();
        try {
            wait(600);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        rightstepRecursive();

This part of my code shows the recursive methods, which could probably be written better I know. A lot of DRY...

private void leftstepRecursive() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mLeftfoot.startAnimation(mFootAnim);
                if (!pleaseStop)
                    leftstepRecursive();
            }
        }, mIntervalleft);}

    private void rightstepRecursive() {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mRightfoot.startAnimation(mFootAnim);
                    if (!pleaseStop)
                        rightstepRecursive();
                }
            }, mIntervalright);

Any better ideas to implement this left, right, left, right, left, right, left, right, left, right, idea?

Or, what I can do to correct the wait?

I also tried this:

private void rightstepRecursive() {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Object testobj = mRightfoot.startAnimation(mFootAnim);
                    synchronized (testobj) {
                        testobj.wait(600);
                    }
                    if (!pleaseStop)
                        rightstepRecursive();
                }
            }, mIntervalright);

@LuigiMendoza. I tried thread(sleep). No errors, but both animations move simultaneously not sure why...

private void rightstepRecursive() {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mRightfoot.startAnimation(mFootAnim);
                    if (!pleaseStop)
                        rightstepRecursive();
                }
            }, mIntervalright);

            try
            {
                Thread.sleep(500);
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
}

I also get this message repeating in Logcat.

02-11 12:15:32.261: I/Choreographer(30480): Skipped 302 frames!  The application may be doing too much work on its main thread.

Upvotes: 2

Views: 2477

Answers (1)

Submersed
Submersed

Reputation: 8870

You should really utilize the Animations framework and some of the classes it provides such as AnimationListeners and AnimatorSets. The Animations framework provides a lot of functionality that would really simplify what you are trying to do i.e. repeating animations, adding delays, timing animations, etc.

Here's a really bare bones example to give the basic idea:

AnimatorSet animations;

//Play the animation
private void playAnimation(){       
    //If they haven't stopped the animation, loop it.
        ObjectAnimator anim1 = ObjectAnimator.ofFloat(target, propertyName, values);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(target, propertyName, values);
        animations = new AnimatorSet();
        animations.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                playAnimation();
            }
        });
        animations.play(anim1).before(anim2);
            animations.start();

}

//Called when cancel is selected
private void stopAnimation(){
    animations.end();
}

Upvotes: 3

Related Questions