Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve center point XY coordinates from selection

What I'm aiming to do is to allow the user of my Android application to select points on the face and retrieve the X and Y coordinates from that touch. Please see the below picture.

Coordinates from face

I would like the user to be able to change the size of the selection square.

So far I have the below code, but I honestly have no idea where to go from there. How do I go about drawing a rectangle that the user can manipulate and move (and then return the X and Y centerpoint coordinates from that)? I'm sure there's an Android feature for this.

private void selectImg(){
    //retrieve X and Y values from touch
    surfaceView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent pos) {
            //retrieve position when user finishes touch
            if (pos.getAction() == MotionEvent.ACTION_UP){
                    Log.d("X",String.valueOf(pos.getX()));
                    Log.d("Y",String.valueOf(pos.getY()));
            }
            return true;
        }
    });     
}

Thank you!

Could be useful: Custom Android Image Crop https://github.com/dtitov/pickncrop/blob/master/src/com/github/pickncrop/MainActivity.java

like image 793
LKB Avatar asked Jun 26 '13 23:06

LKB


1 Answers

I'm not sure how much of the app you have done. However, you'll need a way to identify moving the squares versus stretching the squares. You can do this by button or you can do it on design (move from inside square and stretch from bounds).

   @Override
    public boolean onTouch(View view, MotionEvent pos) {
        //retrieve position when user finishes touch
        if (pos.getAction() == MotionEvent.ACTION_UP){
                Log.d("X",String.valueOf(pos.getX()));
                Log.d("Y",String.valueOf(pos.getY()));
        }
        //pseudo code
        //if user is dragging
             //get new dragged position
            //if boundaries are being dragged
                //redraw square to match new dragged position (requires some math to stretch properly)
            //else if inside boundaries being dragged
                //redraw the square to new dragged position (center it)

        return true;
    }

You'll need to look into examples of how to redraw the squares. I'm not sure how you are drawing them in the first place.

EDIT: Here are some useful sources. If you combine the two, you should be easily able to reach your goal:

Moving image with touch events

Android: How to stretch an image to the screen width while maintaining aspect ratio?

like image 71
But I'm Not A Wrapper Class Avatar answered Sep 24 '22 05:09

But I'm Not A Wrapper Class