java - Android:尝试在两个动画之间添加等待,在 wait() 错误之前获取对象未被线程锁定

标签 java android multithreading wait

我是 Android 和 Java 编程新手。我为 Coursera 类(class)创建了一个应用程序。它有两个重复出现的动画。我希望它们像鼓点一样动画 - 1,2,1,2,1,2,1,2,1,2。

我编写了两个递归方法,它们运行直到用户单击重置按钮。我尝试在方法调用之间添加等待,以便第二个方法调用在第一个方法调用之后立即开始。

这是我的代码中需要等待的部分。

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

我的代码的这一部分显示了递归方法,我知道这可能可以写得更好。很多干...

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

有更好的想法来实现这个左,右,左,右,左,右,左,右,左,右,想法吗?

或者,我可以做些什么来纠正等待?

我也尝试过这个:

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。我尝试过线程( sleep )。没有错误,但两个动画同时移动,不知道为什么......

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 中重复收到此消息。

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

最佳答案

您确实应该利用 Animations框架及其提供的一些类,例如 AnimationListenerAnimatorSet s。动画框架提供了很多功能,可以真正简化您想要做的事情,即重复动画、添加延迟、定时动画等。

这是一个非常简单的例子来给出基本的想法:

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

关于java - Android:尝试在两个动画之间添加等待,在 wait() 错误之前获取对象未被线程锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21711435/

相关文章:

java - 非阻塞 I/O 与使用线程(上下文切换有多糟糕?)

java - 从接口(interface)转换为特征

java - 当我将 IntentService 与 ResultReceiver 一起使用时,当我的 Activity 被销毁时会发生什么

android - 如何删除线性布局的 View

c# - 循环内的多线程c#

multithreading - 基本的Perl线程: threads->list() does not decrease

java - 如何实现具有条目自动过期功能的CacheMap?

java - 生成 *.WAR(64 位 x 32 位)

java - 应用程序警报导致奇怪的错误,直到我按下后退按钮

android - 如何在 Android 中创建自定义 ListView?