I am trying to make an app where you can place points/markers on an image using an imageview. What I am trying to do is get the coordinates of a touch event and then drawing a marker bitmap at those coordinates, however MotionEvent.getX() and MotionEvent.getY() return values that are strangely shifted. When I click the imageView, the markers appear shifted down and left. I have tried using MotionEvent.getRawX() and MotionEvent.getRawY(), however these seem to make the shifting become worse. Here is an example:
Here is the current code:
ImageView mapImage = findViewById(R.id.mapImageView);
Bitmap imageBitmap = BitmapFactory.decodeFile(projectfile2.getPath()).copy(Bitmap.Config.ARGB_8888,true);
mapImage.setImageBitmap(imageBitmap);
Canvas canvas = new Canvas(imageBitmap);
//canvas.setBitmap(imageBitmap);
//canvas.drawBitmap(imageBitmap,0,0,null);
Bitmap pinBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.red_marker);
mapImage.setOnTouchListener((view, motionEvent) -> {
ImageView view2 = (ImageView) view;
//view2.bringToFront();
viewTransformation(view2, motionEvent);
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
float x = motionEvent.getX();
float y = motionEvent.getY();
canvas.drawBitmap(pinBitmap,x,y,null);
Log.d("app123_X_and_Y",x+" | "+y);
}
return true;
});
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IndoorMapView">
<ImageView
android:id="@+id/mapImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rectangle"
tools:srcCompat="@tools:sample/avatars"
android:contentDescription="Map" />
</RelativeLayout>
Any ideas on how to get the correct coordinates? One quick theory I have is that when I set the canvas bitmap, its not setting it to the same size, however I'm not sure if that's how canvas' work. Thank you.