MonoTouch/MonoDroid - Translate Raw touch coordinates to screen coordinates

我们两清 提交于 2019-12-21 23:19:16

问题


What's the proper way to translate raw touch points to screen coordinates in MonoTouch or MonoDroid? My code works well on the emulator but the points are inaccurate on a device. Here's what I have so far:

    private void Initialize()
    {
        this.Touch += TouchView_Touch;
    }

    void TouchView_Touch(object sender, View.TouchEventArgs e)
    {
        e.ReturnValue = true;
        touchPoints.Add(e.Event.RawX);
        touchPoints.Add(e.Event.RawY);
        this.Invalidate();
    }

    protected override void OnDraw(Android.Graphics.Canvas canvas)
    {
        base.OnDraw(canvas);
        canvas.DrawColor(Color.White);
        Paint p = new Paint();
        p.Color = Color.Black;

        Matrix m = new Matrix();
        canvas.GetMatrix(m);
        float[] destination = touchPoints.ToArray();

        Matrix inverse = new Matrix();
        bool canInvert = m.Invert(inverse);
        inverse.MapPoints(destination);

        canvas.DrawPoints(destination, p);
    }

回答1:


For MonoDroid, there are several StackOverflow Android questions which address the same topic, e.g.:

How to get the absolute coordinates of a view

which suggests using View.GetLocationOnScreen(int[]) or View.GetLocationInWindow().

There is also

mismatch of event coordinates and view coordinates in Android?

which also suggests using View.GetLocationOnScreen() along with getting the screen metrics for the default display.




回答2:


For Monotouch you can use ConvertPointToView which is part of all UIViews. It will convert the point to the correct coordinate for the view you pass in.



来源:https://stackoverflow.com/questions/4618405/monotouch-monodroid-translate-raw-touch-coordinates-to-screen-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!