获取相对于自定义ImageView的触摸坐标 [英] Get Touch Coordinates Relative To A Custom ImageView

查看:98
本文介绍了获取相对于自定义ImageView的触摸坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了自定义ImageView.因为我的图像很大,所以我使用ScrollView和Horizo​​ntalScrollView来滚动图像.我想在onTouch事件中画一个圆圈,但并非在所有情况下都可行.当我滚动图像并触发触摸事件时,x,y坐标适用于图像的当前可见扇区(在右上角,例如0x0,但是当我在某处滚动时,在可见的上角,它也是0x0),但是正在画圆根据图像大小. 另一个问题是该应用程序可以在各种屏幕尺寸上使用. 我知道它是xamarin c#,但在本机Android中应该是相同的.有谁知道如何正确绘制/获取坐标?

I implemented custom ImageView. Because my image is large I'm using ScrollView and HorizontalScrollView to scrolling image. I want to draw circle in onTouch event, but its not work in all cases. When I scroll image and fire touch event the x,y coordinates apply to current visible sector of image ( in top right corner its e.g. 0x0, but when I scroll somwhere and in visible top corner it will be 0x0 too), but circle is drawing according to image size. Another problem is that app could be used on various screen size. I know that it xamarin c# but in native Android it should be the same. Anyone knows how to draw/get coordinates properly?

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

推荐答案

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

I found solution. Just replace

                   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;

这篇关于获取相对于自定义ImageView的触摸坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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