search
HomeJavaDetect clicks at specific x and y coordinates in an imageView

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. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use