Android: how to detect touch location on ImageView if the image view is scaled by matrix?

后端 未结 3 956
我在风中等你
我在风中等你 2020-12-14 03:22

I set OnTouchListener of an ImageView and implement onTouch method, but if the image is scaled using matrix, how do I calculate the location of the

3条回答
  •  既然无缘
    2020-12-14 03:46

    Zorgbargle answer is right but there is another consideration when your loading images from resource folder and that's density of the device.

    Android scale images base on the device density so you if you only have the image in mdpi folder, you must also divide the points by the density to find the real point on the image:

    float[] point = new float[] {event.getX(), event.getY()};
    
    Matrix inverse = new Matrix();
    imageView.getImageMatrix().invert(inverse);
    inverse.mapPoints(point);
    
    float density = getResources().getDisplayMetrics().density;
    point[0] /= density;
    point[1] /= density;
    

提交回复
热议问题