2017-02-23 1 views
0

次のコードを作成して2本の指を検出し、異なる色の円の表現で動きを追跡させようとしています。しかし、私は複数のサークルをどのように動作させるべきかは不明です。2本の円を描くために2本の指をどのように検出しますか?

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.support.v4.view.MotionEventCompat; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.view.View; 

public class MyExtendedView extends View { 

    static int touchDoneCounter = 2; 

    static String DEBUG_TAG = "CUSTOM_VIEW_INFO"; 

    float x=0, y=0; 

    // The constructor is called first 
    public MyExtendedView(Context ctx, AttributeSet attrs) { 

     super(ctx, attrs); 

     // Set the background color to black 
     this.setBackgroundColor(Color.BLACK); 
    } 

    // This method is called before the view is drawn first, on screen rotation and when forceredraw is called 
    protected void onDraw(Canvas canvas) { 

     super.onDraw(canvas); 

     Paint p = new Paint(); 
     p.setColor(Color.YELLOW); 

     Paint g = new Paint(); 
     g.setColor(Color.GREEN); 


     // draw the circle where the touch occurs. At start, x and y are zero so the circle is drawn on top right 
     canvas.drawCircle(x, y, 75f, p); 



    } 



    // This is called when a touch is registered 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 


     int action = MotionEventCompat.getActionMasked(event); 

     // logging the kind of event we got 
     switch (action) { 
      case MotionEvent.ACTION_DOWN: 
      case MotionEvent.ACTION_POINTER_DOWN: { 
       break; 
      } 

      case MotionEvent.ACTION_MOVE: { // a pointer was moved 
       break; 
      } 

      case MotionEvent.ACTION_UP: 
      case MotionEvent.ACTION_POINTER_UP: 
      case MotionEvent.ACTION_CANCEL: { 
       break; 
      } 
     } 
     //1.5 at this point we re-draw the circle where the touch occurred 
     redrawViewWithCircle(event); 

     return true; 
    } 


    public void redrawViewWithCircle(MotionEvent event) { 

     // Get index 
     int index = MotionEventCompat.getActionIndex(event); 

     // Get coordinates for circle center. Set the instance variables. 
     this.x = (int)MotionEventCompat.getX(event, index); 
     this.y = (int)MotionEventCompat.getY(event, index); 

     // Force the view to redraw. 
     this.postInvalidate(); 

    } 

} 

私はインデックスとIDを持っている必要があると思いますが、どこに配置すべきかはわかりません。正しい軌道にいるのですか?

答えて

1

あなたは正しい軌道に乗っています。タッチする各指は、個別のIDとインデックスを持ちます。インデックスは0 ... n(ここでnは現在の指の数です)、IDは上に行くことができ、ギャップがあります(持ち上げた指の場合)。あなたのアプリでは、event.getX(index)とevent.getY(index)を使ってすべてのxとyの位置を追跡し、それらをPointsのリストに追加します。次に、描画するときに、リストの各ポイントに円を描きます。わかりやすくするために、それぞれのタッチのリストをクリアして再構築することができます。なぜなら、最後にどのような効果が必要なのかを100%確信しているわけではないからです。

+0

チップをありがとう。現時点では、2本の指(1本ではなく)を使用して、指の動きの両方を別々の色の円で追跡すると、アプリを認識させようとしています(1本の指は黄色の円、その他)。このxとthis.yでxとyの位置を追跡していませんか? – trungnt

+0

あなたは最初のインデックスです。しかし、複数の指がある場合は、複数のxsとysがあります。 –

+0

また、最初のインデックスの変更点はどれですか。 –

関連する問題