android - 如何防止我的程序排队按钮按下

标签 android audio nested-loops thread-sleep vibration

大家好这是我的第一篇文章,所以请温柔。即使我的 android final 已经完成,我仍然觉得很难不继续调整我的程序并添加到它只是为了它的乐趣。它是一个简单的程序,当您触摸屏幕时,屏幕上的角色会以两种方式中的一种笑,并带有匹配的振动。它工作得很好,但是当我的老师(当时心情不好)去测试它时,他几乎把按钮捣碎了,这使它每次按下按钮都排成一列,我们不得不像 15 次笑声一样坐下来,然后我们才能用手机做更多事情。我想要做的只是有一个触摸事件计数,直到第一个事件完成。它是一个简单的触摸事件,带有几个嵌套的 if 语句。

public boolean onTouch(final View v, MotionEvent m) 
    {
        v.setBackgroundResource(R.drawable.laughing);//changes the image to the laughing monkey
            if (m.getAction() == MotionEvent.ACTION_DOWN)
            {
                if (timesTouched != I) //checks to see if the touch amount is not equal to the random number
                {
                    if(laughter != 0) 
                    {       
                        sp.play(laughter, 1, 1, 1, 0, 1);//plays the loaded sound when the screen is pressed
                        //vib.vibrate(900);
                    }
                    Time = 900;
                    timesTouched++;
                    intDelay = 1;
                    if(vibon == 1)
                    {
                        vib.vibrate(Time);
                    }
                }

                else if (timesTouched == I)//checks to see if the touch amount is the same as the random number
                {
                    if(laughter != 0)
                    {
                        sp.play(laughFit, 1, 1, 1, 0, 1);//plays the loaded sound when the screen is pressed
                    }
                    Time = 6000;
                    timesTouched = 0;
                    intDelay = 1;
                    if(vibon == 1)
                    {
                        vib.vibrate(laugh, -1);
                    }
                }

            }

            else if((m.getAction() == MotionEvent.ACTION_UP) && (intDelay == 1))
            {
                try {
                    Thread.sleep(Time);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                v.setBackgroundResource(R.drawable.normal) ;//returns the image to the normal looking monkey
                intDelay = 0;
            }
        return true;
    }
}

sleep 定时器可以防止背景图像在笑声结束之前恢复到默认值。我确实试图让我的老师帮忙,但他只是一个快速的替代品,在今年年初开始教书之前,他甚至从未接触过安卓设备。请您提供的任何帮助将不胜感激,因为到目前为止,我不得不在 Google 搜索的帮助下自学这些东西。

谢谢一堆!

最佳答案

如果这是我,我可能会将声音播放移动到单独的线程,然后使用消息通知该线程何时开始和停止声音。这样,按钮可以立即发送消息,而不会被任何后续操作阻止。副作用应该是每次按下按钮都会停止前一个笑声并开始一个新的笑声(或者只是在前一个声音字节上排队另一个声音字节,有效地组合声音)。相反,您可以在声音结束之前禁用按钮,但我认为这不会是一个好的用户体验 - 他们希望能够捣碎和捣碎他们的心!

查看 http://developer.android.com/resources/articles/painless-threading.html有关如何处理此类事情的更多指导。

关于android - 如何防止我的程序排队按钮按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10465881/

相关文章:

android - checkboxpreference android 或 listpreference 的依赖中的多重依赖

android - Jenkins:Android 项目构建失败(生成数据绑定(bind)/ Dagger 类失败)

android - (Android Repository Pattern) 使用从 web 删除的项目更新电话数据库数据

java - requestaudiofocus 的正确参数是什么?

iphone - 我可以在iPhone上访问短信声音吗?

java - 最大单次卖出利润算法的最优解

android - Gingerbread 不加载 list 文件

apache-flex - 在flex中为麦克风绘制波形,可以吗?

带有循环列表的Python调用函数

Java - 使用嵌套 for 循环打印未填充的菱形