按住按钮限制功能

时间:2014-12-24 05:48:17

标签: android chromecast

    // RIGHT BUTTON
    Button btnRight = (Button) findViewById(R.id.btnRight);
    btnRight.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                API_send_movement("move_right");
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                API_send_movement("stop_right");
            }
            return false; // This will enable players to drag their finger
        }

    });

目的:让玩家将手指拖过一些按钮,向上,向左和向右。这就是它返回false的原因。

这很棒!

问题:我如何'限速'这个?目前它每发送一个信号发送一个信号,需要放慢速度,比如每0.5秒一次。

1 个答案:

答案 0 :(得分:1)

一种简单的方法是设置一个long lastCall;变量,用于存储上次调用API_send_movement("move_right");的时间,如果时间差不够大,则立即返回。类似的东西:

// RIGHT BUTTON 
Button btnRight = (Button) findViewById(R.id.btnRight);
btnRight.setOnTouchListener(new OnTouchListener() { 
    long lastCall;

    @Override 
    public boolean onTouch(View v, MotionEvent event) {
        if(lastCall + 500 > System.currentTimeMillis()) {
            return false;
        }

        lastCall = System.currentTimeMillis();
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            API_send_movement("move_right"); 
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            API_send_movement("stop_right"); 
        } 
        return false; // This will enable players to drag their finger 
    } 

});