android 多点触摸绘画demo

public class GestureView extends View {

    public GestureView(Context context) {
        super(context);
    }

    TwofingerGestureDetector detector;

    Paint paint;

    public GestureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        detector = new TwofingerGestureDetector();
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(30);

    }
    SparseArray mActivePointers = new SparseArray<>();

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        //当前DOWN或者UP的是手指的index
        int curPointerIndex = event.getActionIndex();
        //通过index获得当前手指的id
        int curPointerId = event.getPointerId(curPointerIndex);

        int actionMasked = event.getActionMasked();

        switch (actionMasked) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN:
                PointF pointF = new PointF();
                pointF.x = event.getX(curPointerIndex);
                pointF.y = event.getY(curPointerIndex);
                Path p = new Path();
                p.moveTo(pointF.x, pointF.y);
                mActivePointers.append(curPointerId, p);
                break;

            case MotionEvent.ACTION_MOVE:
                for (int size = event.getPointerCount(), i = 0; i < size; i++) {
                    Path path = mActivePointers.get(event.getPointerId(i));
                    if (path != null) {
                        path.lineTo(event.getX(i), event.getY(i));
                    }
                }

                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                mActivePointers.remove(curPointerId);
                break;
        }
        invalidate();
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        for (int size = mActivePointers.size(), i = 0; i < size; i++) {
            Path path = mActivePointers.valueAt(i);
            canvas.drawPath(path, paint);
        }
    }
}

你可能感兴趣的:(android)