MonoTouch/MonoDroid - 将原始触摸坐标转换为屏幕坐标

发布于 2024-10-11 04:42:16 字数 905 浏览 3 评论 0原文

在 MonoTouch 或 MonoDroid 中将原始触摸点转换为屏幕坐标的正确方法是什么?我的代码在模拟器上运行良好,但在设备上这些点不准确。这是我到目前为止所拥有的:

    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);
    }

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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来世叙缘 2024-10-18 04:42:16

对于 MonoDroid,有几个 StackOverflow Android 问题涉及同一主题,例如:

如何获取视图的绝对坐标


建议使用 View.GetLocationOnScreen(int[])View.GetLocationInWindow()

还有

Android 中的事件坐标和视图坐标不匹配?< /a>


它还建议使用 View.GetLocationOnScreen() 以及获取默认显示的屏幕指标。

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.

纵性 2024-10-18 04:42:16

对于 Monotouch,您可以使用 ConvertPointToView,它是所有 UIView 的一部分。它将将该点转换为您传入的视图的正确坐标。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文