简体   繁体   中英

Motionevent.ACTION_UP firing constantly?

In my program I am having it draw a rectangle while the finger is down and moving than erase it after the finger is up. this is to show the user the range of values he/she is using as a "guess" to find the root. however the rectangle never shows up! But, if I remove the call to close the rectangle in the "action_up" part the user can draw the rectangle. Here's the code:

in the on draw function:

if(dataline>1)//if greater than 1, draw rectangle
{
    myPaint.setColor(Color.CYAN);
    canvas.drawRect(tX1,0, tX2,canvas.getHeight(),myPaint);
}

in the motion event function: public boolean onTouchEvent(MotionEvent ev) { final int action = ev.getAction();

         switch (action) {
        case MotionEvent.ACTION_DOWN: {

        final float x = ev.getX();
        final float y = ev.getY();

        // Remember where we started
        mLastTouchX = x;
        mLastTouchY = y;
   tX1=(int)ev.getX();
   tX2=tX1;

       x_1 = ev.getX();
    x_1=(x_1-X1)/(zoom_x);
    clicks= 1;
    tX1=(int) ev.getX();//set first x coord
    tX2=tX1;// make second x coord equal to the first


        }

    case MotionEvent.ACTION_MOVE: {
        final float x = ev.getX();
        final float y = ev.getY();

        // Calculate the distance moved
        final float dx = x - mLastTouchX;
        final float dy = y - mLastTouchY;

        mLastTouchX = x;
        mLastTouchY = y;  



        dataline=2;//let onDraw() draw the rectangle while dragging finger
        tX2+= (int)dx;// find new second coordinate







    }
    case MotionEvent.ACTION_UP: {
        dataline=0;//if commented out, rectangle is drawn otherwise, it is never seen.
    }

         }

    return true;
}

Problem solved! I have learned that you have to put a return statement in each case, or else it will just run all the cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM