Android: How to check if a path contains touched point?

后端 未结 4 853
花落未央
花落未央 2020-12-02 10:45

I would try to develop an application in which I can draw a planimetry. So, each room has got its own ID or name and, if I touch a room, I want to show a T

4条回答
  •  伪装坚强ぢ
    2020-12-02 11:16

    Ok i solved my problem. I post the example code:

    Path p;
    Region r;
    
    @Override
    public void onDraw(Canvas canvas) {
    
        p = new Path();
    
        p.moveTo(50, 50);
        p.lineTo(100, 50);
        p.lineTo(100, 100);
        p.lineTo(80, 100);
        p.close();      
    
        canvas.drawPath(p, paint);
    
        RectF rectF = new RectF();
        p.computeBounds(rectF, true);
        r = new Region();
        r.setPath(p, new Region((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));
    
    }   
    
    public boolean onTouch(View view, MotionEvent event) {
    
        Point point = new Point();
        point.x = event.getX();
        point.y = event.getY();
        points.add(point);
        invalidate();
        Log.d(TAG, "point: " + point);
    
        if(r.contains((int)point.x,(int) point.y))
            Log.d(TAG, "Touch IN");
        else
            Log.d(TAG, "Touch OUT");
    
        return true;
    }
    

提交回复
热议问题