I have an image in my desktop and I have opened that image using Paint app and got specific location with x and y pixels for example: 374,207 px but when I add the image to my android app and use for example touched coordinates they are not the same? I get different values when I click on the same location in the imageView the resulting location is much higher number than in my PC.
I have been going on on this issue for about three days now I need your help.
I have tried different solution on the web for example trying to invert the touched position to get pixel values as in this example here: http://android-er.blogspot.com/2012/10/get-touched-pixel-color-of-scaled.html?m=1
But again not the same x and y coordinates I get X:592 Y:496
Here is my code for the android app:
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: seems like it has to do with scaling up the image but now the question is How do I map the scaled image with x and y values I have in my database?
Moving comment to answer.
Assuming you know original dimensions (from database?) and are OK with using wrap_content on view then using (for 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 for height (y).