Question

I have implemented the following onTouchEvent() function inside a class extended from View:

public boolean onTouchEvent(MotionEvent event){
    // Log.d("MYLOG","TOUCH EVENT OCCURED");
    int i, pointToTrack = 0;
    boolean trackAPoint = false;
    float temp = 0.0f;
    float x = event.getX();
    float y = event.getY();
    // Log.d("MYLOG","TOUCH AT ("+Float.toString(x)+","+Float.toString(y)+")");
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // Toast.makeText(getApplicationContext(), "ACT_DOWN", Toast.LENGTH_SHORT).show();
        // Log.d("MYLOG", "ACT_DOWN");
        for(i=0; i<NUM_OF_GRAPH_POINTS; i++){
            // if(graphPoints[i][0] == y){
            if(Math.abs(graphPoints[i][0] - y)<10.0f ){
                pointToTrack = i;
                trackAPoint = true;
                Log.d("MYLOG", "PRESSED PNT = "+Float.toString(y));
            }
        }
        break;
    case MotionEvent.ACTION_MOVE:
        // Toast.makeText(getApplicationContext(), "ACT_MOVE", Toast.LENGTH_SHORT).show();
        // Log.d("MYLOG", "ACT_MOVE");
        /*
        if(trackAPoint == true){
            graphPoints[pointToTrack][1] = y; 
        }
        */
        temp = temp + 10.0f;
        Log.d("MYLOG", "temp="+Float.toString(temp));
        receiveGainValues(temp);

        break;
    case MotionEvent.ACTION_UP:
        // Toast.makeText(getApplicationContext(), "ACT_UP", Toast.LENGTH_SHORT).show();
        // Log.d("MYLOG", "ACT_UP");
        trackAPoint = false;
        break;
    }       
    // invalidate();
    postInvalidate();
    return true;
}

Please look at this section in particular:

case MotionEvent.ACTION_MOVE:
    // Toast.makeText(getApplicationContext(), "ACT_MOVE", Toast.LENGTH_SHORT).show();
    // Log.d("MYLOG", "ACT_MOVE");
    /*
    if(trackAPoint == true){
        graphPoints[pointToTrack][1] = y; 
    }
    */
    temp = temp + 10.0f;
    Log.d("MYLOG", "temp="+Float.toString(temp));
    receiveGainValues(temp);

    break;

the logcat shows temp = 10 multiple times while I move my finger over the tablet surface. However, since it is detecting the motion, shouldn't temp continue to increase as long as the motion is detected? Why dies it increase once, then stops, and yet the Log.d() below it keeps on getting called?

The tablet is nexus 7 with Android 4.3 and API level 18 if it is relevant

Was it helpful?

Solution

However, since it is detecting the motion, shouldn't temp continue to increase as long as the motion is detected?

Because you reset temp in begin of onTouchEvent:

public boolean onTouchEvent(MotionEvent event){
 ...
float temp = 0.0f;

Set it global but not in onTouchEvent of method

 public class YourClass{

  private float temp = 0.0f;
  ...
 }

OTHER TIPS

Well, since you declare the temp variable inside the onTouchEvent() method, it is re-set to 0 for every new MOVE event. Try declaring temp outside of onTouchEvent() and observe the change.

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