D.K.
D.K.

Reputation: 1375

On Android Canvas polygons click

I have a few polygons on Canvas. How I can listening click on any from polygons?

I have a method

public void drawPolygon(ChartPolygon polygon) {

        List<Point> points = polygon.getPoints();
        int size = points.size();
        if (size < 2) {
            return;
        }

        Paint polyPaint = new Paint();
        polyPaint.setColor(polygon.getColor());
        polyPaint.setStyle(Style.FILL);

        Path polyPath = new Path();
        polyPath.moveTo(points.get(0).getX(), points.get(0).getY());
        for (Point point : points) {
            polyPath.lineTo(point.getX(), point.getY());
        }
        canvas.drawPath(polyPath, polyPaint);
    }

Upvotes: 3

Views: 3927

Answers (2)

user2808624
user2808624

Reputation: 2530

I would keep a list of Regions. Whenever you draw a new Polygon, use the same Path which you use for drawing also to define a new Region:

Region r = new Region();
r.setPath(path, clip);
regionList.add(r);

clip could be defined e.g. by

 Region clip = new Region(0, 0, canvas.getWidth(), canvas.getHeight());

Finally when you detect a touch in your onTouchListener loop over the regionList and check, whether the touched point (given by x and y) is inside the region:

for (int i = 0; i < regionList.size(); i++) {
    Region r = regionList.get(i);
    if (r.contains(x,y) {
        selectedRegionIndex = i;
        break;
    }
}

Upvotes: 4

Benjamin RD
Benjamin RD

Reputation: 12034

Read this post, this post can help you

First one, you can get the position in X and Y of the click event in your screen. You can implement the onTouchEvent function in the Activity extends

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:

        if (topTouchArea.contains(event.getX(), event.getY())) {                
            currentTouch = TOUCH_TOP;
        } else if (RightTouchArea.contains(event.getX(),event.getY())) {                
            currentTouch = TOUCH_RIGHT;
        } else if (LeftTouchArea.contains(event.getX(),event.getY())) {            
            currentTouch = TOUCH_LEFT;
        } else {
            return false; //Return false if user touches none of the corners
        }
        return true; 
     }
  }

Now (always in your onTouchEvent) function, you can evaluate the position and regions of your polygon.

case MotionEvent.ACTION_MOVE:

        switch (currentTouch) {
        case TOUCH_TOP:              
             top.x = event.getX();
             top.y = event.getY();                            
             invalidate();
             return true;
        case TOUCH_RIGHT:                
             Right.x = event.getX();
             Right.y = event.getY();                
             invalidate();
             return true;
        case TOUCH_LEFT:                 
             Left.x = event.getX();
             Left.y = event.getY();             
             invalidate();
             return true;       
        }         

    case MotionEvent.ACTION_UP:

        switch (currentTouch) {
        case TOUCH_TOP:
             top.x = event.getX();
             top.y = event.getY();                
             invalidate();
             currentTouch = NONE;
             return true;
        case TOUCH_RIGHT:
             Right.x = event.getX();
             Right.y = event.getY();             
             invalidate();
             currentTouch = NONE;
             return true;
        case TOUCH_LEFT:
            Left.x = event.getX();
             Left.y = event.getY();               
             invalidate();
             currentTouch = NONE;
             return true;      
        }         
        return false;

All the post is in the article link

Upvotes: 1

Related Questions