0

我正在尝试根据触摸事件画一条线。基本上它在手指移动时画线。覆盖 ontouchevent 和 onsizechanged 时出现错误。它最初是用 JAVA 编写的。我刚把它翻译成C#。这是代码:

protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        currentLevel = Intent.GetIntExtra("gameLevel", 0);
        playerScore = Intent.GetIntExtra("score", 0);
        SetContentView(new SampleView(this));


        // Create your application here
    }

    private class SampleView : View
    {
        private Paint mPaint;
        private static Bitmap m_bitmap;
        private DisplayMetrics m_metrics;
        private Canvas m_canvas;
        private Path m_path;
        private Paint m_bitmapPaint;
        private float m_X, m_Y;
        static bool m_pathDrawn = false;
        private static float TOUCH_TOLERANCE = 4;

        public SampleView(Context context)
            : base(context)
        {
            Focusable = true;

            mPaint = new Paint();
            mPaint.AntiAlias = true;
            mPaint.Dither = true;
            mPaint.SetStyle(Paint.Style.Stroke);
            mPaint.StrokeWidth = 12;
            mPaint.StrokeJoin = Paint.Join.Round;
            mPaint.StrokeCap = Paint.Cap.Round;
            m_metrics = context.Resources.DisplayMetrics;
            m_bitmap = Bitmap.CreateBitmap(m_metrics.WidthPixels, m_metrics.HeightPixels, Bitmap.Config.Argb8888);
            m_canvas = new Canvas(m_bitmap);
            m_bitmapPaint = new Paint();



        }

        public void onerase()
        {
            m_canvas = null;
        }

        protected override void onSizeChanged(int p_w, int p_h, int p_oldw, int p_oldh)
        {
            this.onSizeChanged(p_w, p_h, p_oldw, p_oldh);
        }

        protected override void OnDraw(Canvas canvas)
        {
            canvas.DrawColor(Color.Black);
            canvas.DrawBitmap(m_bitmap, 0, 0, m_bitmapPaint);
            canvas.DrawPath(m_path, mPaint);
        }

        private void touch_start(float p_x, float p_y)
        {
            m_path.Reset();
            m_path.MoveTo(p_x, p_y);
            m_X = p_x;
            m_Y = p_y;
        }

        private void touch_move(float p_x, float p_y)
        {
            float m_dx = Math.Abs(p_x - m_X);
            float m_dy = Math.Abs(p_y - m_Y);
            if (m_dx >= TOUCH_TOLERANCE || m_dy >= TOUCH_TOLERANCE)
            {
                m_path.QuadTo(m_X, m_Y, (p_x + m_X) / 2, (p_y + m_Y) / 2);
                m_X = p_x;
                m_Y = p_y;
                m_pathDrawn = true;
            }
        }

        private void touch_up()
        {
            m_path.LineTo(m_X, m_Y);
            // commit the path to our offscreen
            m_canvas.DrawPath(m_path, mPaint);
            // kill this so we don't double draw
            m_path.Reset();
        }

        public override bool onTouchEvent(MotionEvent p_event)
        {

            float m_x = p_event.GetX();
            float m_y = p_event.GetY();
            switch (p_event.Action)
            {
                case MotionEventActions.Down:
                    touch_start(m_x, m_y);
                    Invalidate();
                    break;
                case MotionEventActions.Move:
                    touch_move(m_x, m_y);
                    Invalidate();
                    break;
                case MotionEventActions.Up:
                    touch_up();
                    Invalidate();
                    break;
            }
            return true;
        }
    }

另一件事。我想将布局中的图像视图作为我的画布,并在触摸事件上画线。我该怎么做?谢谢!

4

1 回答 1

0

方法名称应使用帕斯卡大小写。

Android.Views.View.OnTouchEvent 方法

Android.Views.View.OnSizeChanged 方法

public override bool OnTouchEvent(MotionEvent e)
{
}

protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
{
}

单声道编码指南。

于 2013-02-28T18:34:57.080 回答