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を持っている必要があると思いますが、どこに配置すべきかはわかりません。正しい軌道にいるのですか?
チップをありがとう。現時点では、2本の指(1本ではなく)を使用して、指の動きの両方を別々の色の円で追跡すると、アプリを認識させようとしています(1本の指は黄色の円、その他)。このxとthis.yでxとyの位置を追跡していませんか? – trungnt
あなたは最初のインデックスです。しかし、複数の指がある場合は、複数のxsとysがあります。 –
また、最初のインデックスの変更点はどれですか。 –