Question

This is most likely a trivial stuff but I can't find a solution for it. Things are simple, the user presses the screen and while he is holding the finger on the screen I want a piece of code to execute.

Here is a code sample:

@Override
    public boolean onTouchEvent(MotionEvent event) {
        synchronized (getHolder()) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        myobject.setX(myobject.getX() - 10);
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {

            } else if (event.getAction() == MotionEvent.ACTION_UP) {
            }
            return true;
        }
    }

The effect of this code is that myobject.setX() only happens once, I want it to keep repeating while the user keeps the finger on the screen, so if it holds it for 3 seconds, myobject.setX() would decrease with -10 a few times.

Was it helpful?

Solution 2

You could do something like this (example):

int howLongUntilEdge = 1000;
CountDownTimer t = new CountDownTimer(howLongUntilEdge, 1000) {

    @Override
    public void onTick(long millisUntilFinished) {
        myobject.setX(myobject.getX() - 10);
    }

    @Override
    public void onFinish() {}
};

then on your touch event:

@Override
public boolean onTouchEvent(MotionEvent event) {
    synchronized (getHolder()) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            t.start();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            t.stop();
        }

        return true;
    }
}

It would move it left by 10 every second. You would need to do something like decide how many seconds until it would reach the edge and set the howLongUntilEdge var to reflect the correct value

OTHER TIPS

You can use a boolean variable (assigned to true on ACTION_DOWN) to indicate that you still pressing the screen, and when you raise your finger (ACTION_UP), you set it to false, so during this you can start a Thread to execute your code much time you want, like this:

private boolean mIsStillPressing;

@Override
public boolean onTouchEvent(MotionEvent event) {
    synchronized (getHolder()) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mIsStillPressing = true;
            new Thread() {
                @Override
                public void run() {
                    // Still moving object until an ACTION_UP is done.
                    while (mIsStillPressing) {
                        myobject.setX(myobject.getX() - 10);

                        // Some sleep time
                        try {
                            Thread.sleep(100L);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            mIsStillPressing = false;
        }
        return true;
    }
}`

The MotionEvent provided in public boolean onTouchEvent(MotionEvent event) will only match MotionEvent.ACTION_DOWN for the initial touch. You'll need to continue your updates in your else if statement: (event.getAction() == MotionEvent.ACTION_MOVE).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top