Android:尝试在两个动画之间添加等待,使对象未在wait()错误之前被线程锁定 [英] Android: trying to add a wait in between two animations, getting Object not locked by thread before wait() error

查看:65
本文介绍了Android:尝试在两个动画之间添加等待,使对象未在wait()错误之前被线程锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android和Java编程的新手.我为Coursera课程创建了一个应用.它具有两个重复出现的动画.我希望他们像鼓拍一样动画-1,2,1,2,1,2,1,2,1,2.

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?

我也尝试过:

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.我试过线程(睡眠).没有错误,但是两个动画同时移动,不确定为什么...

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

我也收到在Logcat中重复的消息.

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.

推荐答案

您应该真正利用动画框架及其提供的一些类,例如 AnimationListener s和 AnimatorSet s.动画框架提供了许多功能,这些功能实际上可以简化您要尝试做的事情,即重复动画,添加延迟,定时动画等.

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

这篇关于Android:尝试在两个动画之间添加等待,使对象未在wait()错误之前被线程锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆