Touch point in various screen size

This question is related to http://stackoverflow.com/questions/38522914/get-touch-coordinates-relative-to-a-custom-imageview/38525259#38525259

Im' developing android app and I have another problem with point x,y when touch.
Code from linked thread is working but only in one screen size. When I mark point on 5,5 inch size screen and then read this in 9' tablet is in other position relative to image on which Im drawing. Image size is the same in phone and tablet.

    public class DrawViewInside : ImageView
{

    public static Bitmap bitmapInside;
    private Paint paint = new Paint();
    private Point point = new Point();

    private Canvas mCanvas;
    private static Bitmap mutableBitmap;
    private Context mContext;
    public static Bitmap b;
    public void SetCarId(int id)
    {
        carId = id;
    }
    public DrawViewInside(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        mContext = context;
        setDefault(drawable);
    }

    public void SetBitmap(int drawableId)
    {
        setDefault(drawableId);
        Invalidate();
    }


    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        base.OnMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = MeasureSpec.GetSize(widthMeasureSpec);
        int height = width;

        var metrics = Resources.DisplayMetrics;
        if (b != null)
        {
            SetMeasuredDimension(b.Width, b.Height);
        }

    }


    public async void setDefault(int drawableId)
    {


            BitmapFactory.Options options = await GetBitmapOptionsOfImage(drawableId);
            paint.Color = Color.Red;
            paint.StrokeWidth = 15;
            paint.SetStyle(Paint.Style.Stroke);
            var metrics = Resources.DisplayMetrics;
            var widthInDp = ConvertPixelsToDp(metrics.WidthPixels);
            var heightInDp = ConvertPixelsToDp(metrics.HeightPixels);

            b = BitmapFactory.DecodeResource(Resources, drawableId);esources, drawableId);
            mutableBitmap = b.Copy(Bitmap.Config.Argb8888, true);
            mCanvas = new Canvas(mutableBitmap);
            mCanvas.Save();

    }


    protected override void OnDraw(Canvas canvas)
    {
        DrawCircle(canvas);


    }
    private void DrawCircle(Canvas canvas)
    {
        if (mCanvas == null)
        {
            setDefault(drawable);
        }
        else
        {
            mCanvas.Restore();
            mCanvas.DrawCircle(point.x, point.y, 10, paint);
            mCanvas.Save();
            canvas.DrawBitmap(mutableBitmap, 0, 0, paint);
            bitmapInside = mutableBitmap;
        }


    }

    public override bool OnTouchEvent(MotionEvent e)
    {
        switch (e.Action)
        {
            case MotionEventActions.Down:

                break;

            case MotionEventActions.Up:
                        Activity act = (Activity)mContext;

                       float viewX = e.RawX;// - this.Left;
                        float viewY = e.RawY;// - this.Top;
                        int[] viewCoords = new int[2];
                        this.GetLocationOnScreen(viewCoords);
                        point.x = viewX - viewCoords[0];// e.RawX;
                        point.y = viewY - viewCoords[1];// e.RawY;

                    break;
                }
        Invalidate();
        return true;

    }

    }
    public class Point
    {
        public float x, y;
    }

What I want achieve:
When I touch screen ( with image in bitmap) I'm drawing little circle using this code. Circle draws ok, and then I send point of this circle to the some webservice. When I open again this activity, old points redrawing where I draw them previous. I works good on the same resolution/screen size but when I try load points from 5,5'' 1920x1080p to for example 8'' 1200x800p, points draw to different places according to image (bitmap).

Please, any help will be appreciate. Thanks!

Sign In or Register to comment.