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

筅森魡賤 提交于 2019-12-10 17:10:08

问题


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.


回答1:


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




回答2:


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;
      }


来源:https://stackoverflow.com/questions/9411983/how-to-perform-click-on-circles-drawn-on-a-canvas-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!