MonoTouch/MonoDroid - 将原始触摸坐标转换为屏幕坐标
问题描述:
将原始触摸点转换为MonoTouch或MonoDroid屏幕坐标的正确方法是什么?我的代码在仿真器上运行良好,但设备上的点不准确。这是我到目前为止: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);
}
答
对于MonoDroid的,有几个*的Android的问题,其解决了同一主题,如:
建议使用View.GetLocationOnScreen(int[])
或View.GetLocationInWindow()
。
还有
mismatch of event coordinates and view coordinates in Android?
其还建议使用View.GetLocationOnScreen()
与得到屏幕尺度为缺省显示沿。
答
对于Monotouch,你可以使用ConvertPointToView,它是所有UIViews的一部分。它将点转换为正确的坐标为你在通过视图