0

我对 Android 开发人员仍然很陌生并且被卡住了。我有一个 onTouchEvent,它为旋转图形提供动力并增加了一个背景 int [计数器],它触发了一个短的声音效果(哔声)来播放。目前,如果 [counter] > 10 a 播放声音(使用 soundpool)并且只要 onTouchEvent 保持 counter > 10 就会立即循环播放。是否可以在哔声效果的播放之间插入延迟,以便延迟[counter] 的值越大越减小?

示例: if (counter == 10) { delay = 80 // 在 MS } if (counter == 90) { delay = 10 // 在 MS }

我的代码:

sm = new SoundManager();
    sm.initSounds(getBaseContext());
    sm.addSound(1, R.raw.beep);

...

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (buttonClicked) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            counter++;
            startRotating();
            break;
        case MotionEvent.ACTION_UP:
            counter--;
            stopRotating();
            break;
        }
    }

    return super.onTouchEvent(event);
}

public void startRotating() {
    returnRotating = false;

    if (!keepRotating) {
        keepRotating = true;

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (keepRotating) {
                    degrees = (degrees + 10) % 360;
                    make(degrees);
                    counter = counter + 1;

                    if (counter > 10)
                        sm.playSound(1);  // plays beep

                    handler.postDelayed(this, INTERVAL);
                }
            }
        }, INTERVAL);
    }
}

编辑:添加代码

public void stopRotating() {
    keepRotating = false;

    if (!returnRotating) {
        returnRotating = true;

        final Handler handler = new Handler();

        handler.postDelayed(new Runnable() {

            @Override
            public void run() {

                if (returnRotating) {
                    degrees = (degrees - 10) % 360;
                    make(degrees);
                    counter = counter - 1;

                    if (counter > 10) {
                    sm.playSound(1); // plays beep

                    handler.postDelayed(this, INTERVAL);
                }
            }
        }, INTERVAL);
    }
}
4

1 回答 1

0

这样的事情怎么样?

  if (counter > 10) {
      sm.playSound(1);  // plays beep 
      INTERVAL = 100 - counter;
      handler.postDelayed(this, INTERVAL); 
于 2012-05-31T21:42:27.327 回答