Home  >  Article  >  Detect clicks at specific x and y coordinates in an imageView

Detect clicks at specific x and y coordinates in an imageView

WBOY
WBOYforward
2024-02-14 08:06:091019browse

php editor Xigua today will introduce to you how to detect clicks at specific x and y coordinates in imageView. When developing mobile applications, we often need to process user touch events, especially for click events in image display controls (such as imageView), we need to accurately obtain the location information of the user's click. This article will explain in detail how to use the relevant methods in Android Studio to implement this function, helping developers better handle user click operations. Let’s take a look!

Question content

I have an image on my desktop, I have opened it using the Paint application and got a specific position containing x and y pixels, for example: 374,207 px But when I add the image to my android app and use for example the touch coordinates, aren't they the same? When I click on the same position in the imageview I get different values ​​and as a result the number of the position is much higher than the number in my pc.

I've been working on this problem for about three days and now I need your help.

I've tried different solutions around the web, such as trying to invert the touch position to get the pixel value, like in the example below: http://android-er.blogspot.com/2012/10/get-touched-pixel-color-of-scaled.html?m=1

But the x and y coordinates I got are different x:592 y:496

This is my android application code:

View.OnTouchListener imgSourceOnTouchListener
            = new View.OnTouchListener(){
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            float eventX = event.getX();
            float eventY = event.getY();
            float[] eventXY = new float[] {eventX, eventY};

            Matrix invertMatrix = new Matrix();
            ((ImageView)view).getImageMatrix().invert(invertMatrix);

            invertMatrix.mapPoints(eventXY);
            int x = Integer.valueOf((int)eventXY[0]);
            int y = Integer.valueOf((int)eventXY[1]);


            Drawable imgDrawable = ((ImageView)view).getDrawable();
            Bitmap bitmap = ((BitmapDrawable)imgDrawable).getBitmap();


            //Limit x, y range within bitmap
            if(x < 0){
                x = 0;
            }else if(x > bitmap.getWidth()-1){
                x = bitmap.getWidth()-1;
            }

            if(y < 0){
                y = 0;
            }else if(y > bitmap.getHeight()-1){
                y = bitmap.getHeight()-1;
            }
            
            Log.d(TAG,"X:"+x+" Y:"+y);
            return true;
        }};

Edit: It seems like it has something to do with zooming in on the image, but now the question is how to map the zoomed image with the x and y values ​​in the database?

Workaround

Move comment to answer.

Assuming you know the original dimensions (from database?) and can use wrap_content on the view then use (width as example):

public boolean onTouch(View v, MotionEvent event) {

    // get actual width of view on screen (pixels)
    int w = v.getWidth(); 

    // x coordinate in view's coordinate space
    float tw = event.getX(); 

    // ratio between x coordinate and view width
    float ratioVw = (tw / w); 

    // apply same ratio to original image width.
    int origTouchX = (int) (ratioVw * (origW)); //(origW is original width).

    return true;
}

Repeat height (y).

The above is the detailed content of Detect clicks at specific x and y coordinates in an imageView. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete