How to detect single touch as well as multi-touch in onTouchEvent()

后端 未结 7 1960
眼角桃花
眼角桃花 2020-12-29 11:19

I used the following code to detect the single finger touch and double finger touch. The code detects the double finger touch (when count==2).

I need t

7条回答
  •  清酒与你
    2020-12-29 11:57

    Here is the code for detecting points and drawing each points in different color get compete example here

    public class custom_view extends View {
    
    
    
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    
    public int cu = 0;
    
    
    
    final int MAX_NUMBER_OF_POINT = 10;
    
    float[] x = new float[MAX_NUMBER_OF_POINT];
    
    float[] y = new float[MAX_NUMBER_OF_POINT];
    
    boolean[] touching = new boolean[MAX_NUMBER_OF_POINT];
    
    
    
    public custom_view(Context context, AttributeSet attrs, int defStyle) {
    
        super(context, attrs, defStyle);
    
        init();
    
    }
    
    public custom_view(Context context, AttributeSet attrs) {
    
        super(context, attrs);
    
        init();
    
    }
    
    
    
    public custom_view(Context context) {
    
        super(context);
    
        init();
    
    }
    
    void init() {
    
        paint.setStyle(Paint.Style.STROKE);
    
        paint.setStrokeWidth(40);
    
    }
    
    @Override
    
    protected void onDraw(Canvas canvas) {
    
        for (int i = 0; i < MAX_NUMBER_OF_POINT; i++) {
    
            if (touching[i]) {
    
                switch (i) {
    
                case 1:
    
                    paint.setColor(Color.BLUE);
    
                    break;
    
                case 2:
    
                    paint.setColor(Color.RED);
    
                    break;
    
                case 3:
    
                    paint.setColor(Color.CYAN);
    
                    break;
    
                case 4:
    
                    paint.setColor(Color.GREEN);
    
                    break;
    
                case 5:
    
                    paint.setColor(Color.YELLOW);
    
                    break;
    
                case 6:
    
                    paint.setColor(Color.MAGENTA);
    
                    break;
    
                case 7:
    
                    paint.setColor(Color.DKGRAY);
    
                    break;
    
                case 8:
    
                    paint.setColor(Color.LTGRAY);
    
                    break;
    
                case 9:
    
                    paint.setColor(Color.GREEN);
    
                    break;
    
                case 10:
    
                    paint.setColor(Color.BLACK);
    
    
    
                    break;
    
                    }
    
                canvas.drawCircle(x[i], y[i], 70f, paint);
    
            }
    
        }
    
    }
    
    @Override
    
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
    
                MeasureSpec.getSize(heightMeasureSpec));
    
    }
    
    @Override
    
    public boolean onTouchEvent(MotionEvent event) {
    
        int action = (event.getAction() & MotionEvent.ACTION_MASK);
    
        int pointCount = event.getPointerCount();
    
        for (int i = 0; i < pointCount; i++) {
    
            int id = event.getPointerId(i);
    
    
    
            if (id < MAX_NUMBER_OF_POINT) {
    
                x[id] = (int) event.getX(i);
    
                y[id] = (int) event.getY(i);
    
    
    
                if ((action == MotionEvent.ACTION_DOWN)
    
                        || (action == MotionEvent.ACTION_POINTER_DOWN)
    
                        || (action == MotionEvent.ACTION_MOVE)) {
    
                    touching[id] = true;
    
                } else {
    
                    touching[id] = false;
    
                    }
    
            }
    
        }
    
        invalidate();
    
        return true;
    
    }
    

    }

提交回复
热议问题