获取相对于自定义ImageView的触摸坐标

时间:2016-07-22 09:28:28

标签: android xamarin xamarin.android android-custom-view

我实现了自定义ImageView。因为我的图像很大,所以我使用ScrollView和Horizo​​ntalScrollView来滚动图像。我想在onTouch事件中绘制圆圈,但它并不适用于所有情况。当我滚动图像和触发事件时,x,y坐标适用于图像的当前可见扇区(在右上角,例如0x0,但是当我滚动somwhere并且在可见的顶角时它也将是0x0),但是圆圈正在绘制根据图像大小。 另一个问题是app可以用于各种屏幕尺寸。 我知道它是xamarin c#但在原生Android中应该是一样的。任何人都知道如何正确绘制/获取坐标?

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;
                        point.x = viewX;// e.RawX;
                        point.y = viewY;// e.RawY;

                    break;
                }
        Invalidate();
        return true;

    }

}
public class Point
{
    public float x, y;
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。只需替换

                   float viewX = e.RawX - this.Left;
                    float viewY = e.RawY - this.Top;
                    point.x = viewX;// e.RawX;
                    point.y = viewY;// e.RawY;

                       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;