2013-01-03 30 views
6

拉我用手指油漆畫線字符,而到目前爲止,我想出了下面的代碼:如何識別在畫布上

case MotionEvent.ACTION_MOVE: 
    //return if touch is in this area of canvas 
    if (x<=430 || y<=80 || y>=490) return true; 
    //draw path using x and y co-ordinates 
    mPath.quadTo(previousPoint.x, previousPoint.y, (x+previousPoint.x)/2,(y+previousPoint.y)/2); 
    canvas.drawPath(mPath, paint); 
    previousPoint.x = x; 
    previousPoint.y = y; 
    //invalidate canvas on move 
    imageView.invalidate(); 
    break; 
case MotionEvent.ACTION_UP: 
    Xend=x; 
    Yend=y; 
    //validate that is it true? 
    if((Xstart>=780 && Xstart<=830) && (Xend>=780 && Xend<=830) && (Ystart>=10 && Ystart<=200) && Yend<=800 && Yend>=300){ 
    //show toast if correct 
    Toast.makeText(getBaseContext(), "Correct", Toast.LENGTH_SHORT).show(); 
    }else{ 
    //show toast with XY co-ordinates that your attempt is wrong 
    Toast.makeText(getBaseContext(), "Wrong attempt\n Xstart: "+Xstart+"\n Xend:"+Xend+"\n Ystart: "+Ystart+"\nYend:"+Yend, Toast.LENGTH_SHORT).show(); 
    } 
    imageView.invalidate(); 
    break; 

但不幸的是,上面的代碼不符合我的要求。我想創建按字母順序排列的工作表,用戶通過觸摸進行操作。我想知道他在哪裏開始,他在哪裏移動,他在哪裏結束,以識別他在畫布上繪製的內容,我知道在哪裏獲得接觸點,但問題是如何識別畫布上繪製的內容?想要在Playstore上識別這樣的VisionObjects應用程序。 enter image description here

+0

如果您對android中的某些內容有疑問,請首先嚐試在apis演示中查找示例應用程序...這是一個很好的開始。這裏有一個非常好的例子來處理Android中的觸摸。這是路徑.. \ android-sdk-windows \ samples \ android-10 \ ApiDemos \ src \ com \ example \ android \ apis \ graphics \ FingerPaint.java – karn

+2

上面的代碼是什麼意思? ?),如果它與你的要求無關?我也有理解實際問題的問題:你只是想跟蹤觸摸動作嗎?如用戶觸摸屏幕(開始),將他的手指向左移動5cm(移動),然後我們走(結束)? – AgentKnopf

+0

thanx回覆guyz。 – Hamad

回答

0

要跟蹤,你必須使用三個事件:

  • MotionEvent.ACTION_DOWN跟蹤這些事件開始。
  • MotionEvent.ACTION_MOVE追蹤動作(滑動)。
  • MotionEvent.ACTION_UP來跟蹤事件停止的地方。

請參閱this example。展示如何通過跟蹤動作來繪製路徑。