簡體   English   中英

Motionevent.ACTION_UP不斷觸發?

[英]Motionevent.ACTION_UP firing constantly?

在我的程序中,我讓它在手指向下移動時畫一個矩形,而不是在手指向上移動時畫一個矩形。 這是為了向用戶顯示他/她用作“猜測”以找到根的值的范圍。 但是矩形永遠不會顯示! 但是,如果我刪除了在“ 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;
}

問題解決了! 我了解到,您必須在每種情況下都放入return語句,否則它將僅在所有情況下運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM