Michel Melhem
Michel Melhem

Reputation: 681

How i can make my drawed points clickable?

Recently i figure out how to draw point an a view using a canvas but i quickly counter an issue :

How i can make mt drawed points clickable so i can make apear a menu when the user click on them.

Actually this is how i draw my points :

new OnDrawListener() {
            @Override
            public void onLayerDrawn(Canvas canvas, float pageWidth, float pageHeight, int displayedPage) {
                canvas.drawPoint(pageHeight /2, pageHeight /2, paint);
                view.draw(canvas) ;
            }
        }

Does someone has an idea how an i can make my points clickabke or if i can use other clickable points which allows me to place them in my view with some x and y coordinates.

Thank you.

UPDATE :

Does someone know another way to fix my issue because the actual solution do not work.

      public void onLongPress(MotionEvent e) {
                    int clickX = (int)(e.getX());
                    int clickY = (int)(e.getY());

                    for(int i = 0; i < issueList.size(); i++) {
                        Issue issue = issueList.get(i);
                         xPos = view.getWidth() / issue.getxScale();
                         yPos = view.getHeight() / issue.getyScale();
                         double xDistance = Math.pow(clickX - xPos, 2);
                         double yDistance = Math.pow(clickY - yPos, 2);

                        Log.d("xDistance", Double.toString(xDistance));
                        Log.d("yDistance", Double.toString(yDistance));

                        double distance = Math.sqrt(xDistance + yDistance) / view.getZoom();

                        Log.d("Distance", Double.toString(distance));
                        if(distance < 50) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                vibrator.vibrate(VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE));
                            }
                            Log.d("Taped", "true");
                            return;
                        }
                }

Upvotes: 1

Views: 62

Answers (1)

Alex
Alex

Reputation: 972

You need to do 3 things:

First, when you draw a point always store x and y of it's center to an array.

Second, setOnTouchListener on the view that is drawing those points, and get x and y from the touch event like this:

int clickX = (int)event.getX();
int clickY = (int)event.getY();

And finally loop through the array and calculate the distance between the center of your point and the touch event for every point. If that distance is smaller then the radius of your point you have clicked the point.

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

Upvotes: 1

Related Questions