如何通过按住图像重复动作?

时间:2014-07-23 22:45:08

标签: android ontouchlistener

我想重复一些指令示例计数器,同时按住并保持在我的SurfaceView中绘制的图像,我尝试android-repeat-action-on-pressing-and-holding-a-button但是当我触摸图像时,计数器即使在释放后也会保持计数。

这是我的代码:

public class Graphics extends Activity implements OnTouchListener{
.
.
.
private Handler mHandler;
private Runnable mAction = new Runnable() {
    @Override public void run() {
        myCounter++;
        mHandler.postDelayed(this, 500);
    }
};
.
.
@Override
public boolean onTouch(View v, MotionEvent event) {
    x = event.getX();
    y = event.getY();
    if(/*testing if x and y in the right positions*/){
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (mHandler != null) return true;
                mHandler = new Handler();
                mHandler.postDelayed(mAction, 500);
                break;
            case MotionEvent.ACTION_UP:
                if (mHandler == null) return true;
                mHandler.removeCallbacks(mAction);
                mHandler = null;
                break;
            }
            return false;
    }
return false;
}

0 个答案:

没有答案