【问题标题】:Motionevent.ACTION_UP firing constantly?Motionevent.ACTION_UP 不断触发?
【发布时间】:2011-07-28 19:50:57
【问题描述】:

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

在绘制函数中:

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

在运动事件函数中: 公共布尔 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;
}

【问题讨论】:

    标签: android events motion


    【解决方案1】:

    问题解决了!我了解到您必须在每个案例中放置一个 return 语句,否则它将运行所有案例。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 2011-11-02
    相关资源
    最近更新 更多