0
我试图创建下面的代码来检测两个手指,并让它跟踪不同彩色圆圈表示的运动。但是,我不确定如何有多个圆圈工作。你如何检测两个手指绘制两个独立的圆?
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,但我不能确定这里应该放置的位置。我在正确的轨道上吗?
感谢您的提示。目前,我试图在每次使用两个手指(而不是一个)时识别应用程序,并使用不同颜色的单独圆圈跟踪两个手指移动(一个手指的黄色圆圈和一个绿色的手指其他)。我没有跟踪这个x和this.y的x和y位置吗? – trungnt
你是第一个索引。但是,如果你有多个手指向下,你有多个x和y –
另外哪个手指是第一个索引的变化 –