Motionevent.ACTION_UP不断开火?

时间:2011-04-03 22:33:03

标签: android events motion

在我的程序中,我在手指向下和移动时绘制一个矩形而不是在手指向上后擦除它。这是为了向用户显示他/她用作“猜测”以找到根的值的范围。但矩形永远不会出现!但是,如果我删除调用以关闭“action_up”部分中的矩形,则用户可以绘制矩形。 这是代码:

在绘图功能中

if(dataline>1)//if greater than 1, draw rectangle
{
    myPaint.setColor(Color.CYAN);
    canvas.drawRect(tX1,0, tX2,canvas.getHeight(),myPaint);
}
运动事件功能中的

:     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;
}

1 个答案:

答案 0 :(得分:1)

问题解决了!我已经知道你必须在每种情况下都给出一个return语句,否则它只会运行所有的情况。