开发者

Motionevent.ACTION_UP firing constantly?

开发者 https://www.devze.com 2023-02-21 16:01 出处:网络
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 f

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 t开发者_开发百科he 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消