Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform click on circles drawn on a canvas in android?

I am developing a Face Detection app. In this app, I have to draw circles nearby the eyes and mouth of the face and the user can click to drag circles for setting the position of the same according to him on the detected face. So, all circles have been drawn successfully on the face but I can't able to click on the particular circle and move on throughout the face with zoom out option. Please suggest me for the right solution regarding the same.

Thanks in advance.

like image 841
Sanat Pandey Avatar asked Feb 23 '12 11:02

Sanat Pandey


People also ask

Which method used to draw circle on canvas?

To draw arcs or circles, we use the arc() or arcTo() methods.

How to draw shapes in android studio?

In order to draw your shape, you must compile the shader code, add them to a OpenGL ES program object and then link the program. Do this in your drawn object's constructor, so it is only done once.

What is onDraw method in android?

Override onDraw() The parameter to onDraw() is a Canvas object that the view can use to draw itself. The Canvas class defines methods for drawing text, lines, bitmaps, and many other graphics primitives. You can use these methods in onDraw() to create your custom user interface (UI).


2 Answers

Math.sqrt(Math.pow(clickX - centerX, 2) + Math.pow(clickY - centerY, 2));

Calculate the distance between the center of your point and the touch event - if the distance is smaller than the radius of your circle - you pressed on the circle

like image 96
Nam Vu Avatar answered Nov 15 '22 00:11

Nam Vu


Here is an example I used for rectangles. See if you can adjust the code to use circles instead.

@Override
public boolean onTouchEvent( MotionEvent event) {
     super.onTouchEvent(event);

        int x = (int)event.getX();
        int y = (int)event.getY();
        xStored = x; yStored=y;
        if (event.getAction()==MotionEvent.ACTION_UP){

       }else if(event.getAction()==MotionEvent.ACTION_DOWN){
           System.out.println("Touching down!");
               for(Rect rect : rectangles){
                    if(rect.contains(x,y)){
                        System.out.println("Touched Rectangle, start activity."+x+","+y);

                            invalidate();
                 }else{

                 }
               }


           }else if(event.getAction()==MotionEvent.ACTION_MOVE){

           }
          this.postInvalidate();
         return true;
      }
like image 34
industrychanger Avatar answered Nov 14 '22 23:11

industrychanger