各种屏幕尺寸的触摸点

时间:2016-09-07 09:38:39

标签: c# android xamarin xamarin.android

此问题与Get Touch Coordinates Relative To A Custom ImageView

有关

我正在开发一个Android应用程序,我有触摸坐标点的问题。 来自链接线程的代码正在运行,但只有一个屏幕大小。当我在5.5英寸屏幕设备上添加一个点并在9英寸屏幕设备上读取它时,它会出现在相对于我正在绘制的图像的不同位置。两台设备的图像尺寸相同。

感谢任何帮助。谢谢!

编辑:

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

我想要实现的目标: 我正在画一个点触摸屏幕(显示图像时)。点正确绘制,我将此点的坐标发送到Web服务。当我重新打开此活动时,在图像顶部正确绘制旧点。它在相同的分辨率/屏幕尺寸下完美运行,但是当我尝试在8英寸,1200x800p屏幕上加载5.5英寸,1920x1080p屏幕时,点在相对于屏幕上图像的不同位置绘制(位图)

如何在具有不同屏幕尺寸和分辨率的设备中相对于图像绘制相同位置的点?

0 个答案:

没有答案