MonoTouch的/ MonoDroid的 - 翻译原始触摸坐标到屏幕坐标 [英] MonoTouch/MonoDroid - Translate Raw touch coordinates to screen coordinates

查看:164
本文介绍了MonoTouch的/ MonoDroid的 - 翻译原始触摸坐标到屏幕坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是屏幕的MonoTouch或MonoDroid的坐标转换原始触摸点的正确方法?我的代码工作很好的仿真器,但这些点的设备上不准确的。这是我到目前为止有:

 私人无效初始化()
{
this.Touch + = TouchView_Touch;
}

无效TouchView_Touch(对象发件人,View.TouchEventArgs E)
{
e.ReturnValue = TRUE;
touchPoints.Add(e.Event.RawX);
touchPoints.Add(e.Event.RawY);
this.Invalidate();
}

保护覆盖无效的OnDraw(Android.Graphics.Canvas帆布)
{
base.OnDraw(画布);
canvas.DrawColor(Color.White);
涂料P =新的油漆();
p.Color = Color.Black;

矩阵M =新的Matrix();
canvas.GetMatrix(米);
浮法[] =目的地touchPoints.ToArray();

矩阵求逆=新的Matrix();
布尔canInvert = m.Invert(反向);
inverse.MapPoints(目标);

canvas.DrawPoints(目的地,P);
}


解决方案

有关MonoDroid的,有几个该解决同一主题的StackOverflow的Android的问题,例如:




如何得到视图的绝对坐标




这表明使用 View.GetLocationOnScreen(INT []) View.GetLocationInWindow()



还有




事件的坐标不匹配和查看坐标的Android?




也建议使用 View.GetLocationOnScreen()随着越来越屏幕规格为缺省显示。


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

解决方案

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.

这篇关于MonoTouch的/ MonoDroid的 - 翻译原始触摸坐标到屏幕坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆