使用画布在ImageView上绘制OnTouch行时的坐标错误

时间:2018-04-05 14:05:21

标签: imageview drawing ontouch

感谢您的建议,为您提供帮助!

我正在尝试制作一个简单的应用程序,使用OnTouch点击检测和路径和位图绘制在ImageView上绘制线条

请告诉我,我在哪里用错了OnTT的坐标 可能有一些通知方法如何重新计算所取得的X Y坐标 可能我应该更好地使用某种不同的布局或OnTouch监听器实现

图像尺寸 - 1024 x 768 Android Screen 1280 x 800

图像在ImageView中心加载 如果我做了以下 - 我得到正确的边框正确的正方形

        void Button3OnClick(object sender, EventArgs eventArgs)
    {            
        OnImageClick(50, 50, 0, 0);
        OnImageClick(950, 50, 0, 0);
        OnImageClick(950, 710, 0, 0);
        OnImageClick(50, 710, 0, 0);
        OnImageClick(40, 40, 0, 0);
        DrawPolygon();
    }

为什么OnTouch绘画会产生错误的结果?

Android Screenshot

红线 - 我手动绘制的路径 绿线 - 应用最终绘制的线

我的XAML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/MyButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Задать Изображение" />
    <Button
        android:id="@+id/MyButton1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Сброс" />
    <Button
        android:id="@+id/MyButton2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Расчёт" />
  <Button
      android:id="@+id/MyButton3"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="ВЫХОД" />
  <ImageView
        android:src="@android:drawable/ic_menu_gallery"                
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/imageView1" />
</LinearLayout>

这是我的代码

        protected override void OnCreate(Bundle bundle)
    {

        base.OnCreate(bundle);

        metrics = new DisplayMetrics();
        WindowManager.DefaultDisplay.GetMetrics(metrics);
        widthInDp = metrics.WidthPixels;
        heightInDp = metrics.HeightPixels;

        SetContentView(Resource.Layout.Main);

        pointList = new List<Point>();            
        _imageView = FindViewById<ImageView>(Resource.Id.imageView1);
        _imageView.SetOnTouchListener(this);

        button = FindViewById<Button>(Resource.Id.MyButton);
        button1 = FindViewById<Button>(Resource.Id.MyButton1);
        button2 = FindViewById<Button>(Resource.Id.MyButton2);
        button3 = FindViewById<Button>(Resource.Id.MyButton3);

        button.Click += ButtonOnClick;
        button1.Click += Button1OnClick;
        button2.Click += Button2OnClick;
        button3.Click += Button3OnClick;
    }

    public bool OnTouch(View v, MotionEvent e)
    {   

        switch (e.Action)
        {
            case MotionEventActions.Up:                    
                TimeSpan span = lastTouchDown - System.DateTime.Now;                    
                float UpTouchX = e.GetX(); 
                float UpTouchY = e.GetY();
                float differenceX = Math.Abs(initialTouchX - UpTouchX);
                float differenceY = Math.Abs(initialTouchY - UpTouchY);                    
                if ((span.TotalMilliseconds < 300) && (differenceY < 10) && (differenceX < 10))
                    OnImageClick(UpTouchX, UpTouchY, v.MeasuredHeight, v.MeasuredWidth);

                break;
            case MotionEventActions.Down:
                lastTouchDown = System.DateTime.Now;                    
                initialTouchX = e.GetX();
                initialTouchY = e.GetY();
                break;
        }
        return true;
    }

    void OnImageClick(float x, float y, int MeasuredHeight, int MeasuredWidth)
    {
        int measuredHeight = MeasuredHeight;
        int measuredWidth = MeasuredWidth;

        Point point = new Point();

        point.x = x;
        point.y = y;

        pointList.Add(point);

        button2.Text = string.Format("Рассчёт Вершин {0}", pointList.Count);

        DrawPolygon();
    }
    void DrawPolygon()
    {
        var paint = new Paint();
        paint.SetARGB(255, 200, 255, 0);
        paint.SetStyle(Paint.Style.Stroke );
        paint.StrokeWidth = 5;

        Bitmap tempBitmap = Bitmap.CreateBitmap(baseBitmap.Width, baseBitmap.Height, Bitmap.Config.Rgb565);
        Canvas tempCanvas = new Canvas(tempBitmap);

        tempCanvas.DrawBitmap(baseBitmap, 0, 0, null);            

        var path = new Path();            
        foreach (Point pnt in pointList)
        {
            if (path.IsEmpty)
                path.MoveTo(pnt.x, pnt.y);
            else
                path.LineTo(pnt.x, pnt.y);

            tempCanvas.DrawCircle(pnt.x, pnt.y, 7, paint);  
        }

        if (!path.IsEmpty)
            tempCanvas.DrawPath(path, paint);

        _imageView.SetImageDrawable (new BitmapDrawable(tempBitmap));            
    }

0 个答案:

没有答案