Java 如何检测两个手指来画两个独立的圆?
我试图创建以下代码来检测两个手指,并让它用不同颜色的圆圈表示来跟踪运动。但是,我不确定如何让多个圆圈工作Java 如何检测两个手指来画两个独立的圆?,java,android,Java,Android,我试图创建以下代码来检测两个手指,并让它用不同颜色的圆圈表示来跟踪运动。但是,我不确定如何让多个圆圈工作 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.A
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,但我不确定应该放在哪里。我走对了吗?你走对了。每个接触的手指都有一个单独的id和食指。索引变为0…n(其中n是现在向下的手指数),ID可以变高并有间隙(在手指抬起的情况下)。对于您的应用程序,通过event.getX(索引)和event.getY(索引)跟踪所有x和y位置,并将它们添加到点列表中。然后在绘制时,在列表中的每个点上绘制一个圆。为了简单起见,您现在只需清除并重新生成每次触摸的列表,因为我无法100%确定您最终想要什么效果。谢谢您的提示。目前,我正试图让应用程序在我使用两个手指(而不是一个)时识别,并用不同颜色的单独圆圈(一个手指为黄色圆圈,另一个手指为绿色圆圈)跟踪两个手指的运动。我不是用这个.x和这个.y来跟踪x和y的位置吗?你是第一个索引。但是如果你有多个手指向下,你有多个xs和ysAlso哪个手指是第一个索引更改SSO而不是只有x和y,我应该创建另外两个x和y变量?然后我应该用它们来追踪第二个圆圈,对吗?此外,由于索引发生变化,我希望圆圈保持在屏幕上,我应该使用getPointerId吗?我会制作一个列表,而不是制作多个其他变量。这样可以减少变量的数量,将x和y数据保持在一起,并可扩展到3个、4个或更多手指。