在我的重写方法mono for android中找不到合适的方法

时间:2013-02-28 15:30:46

标签: events view xamarin.android draw

我正在尝试根据触摸事件绘制一条线。基本上它随着手指移动而绘制线条。我在覆盖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;
        }
    }

另一件事。我想从布局中将我的图像视图作为我的画布,然后在那里绘制线条。我该怎么做?谢谢!

1 个答案:

答案 0 :(得分:0)

方法名称应使用pascal大小写。

Android.Views.View.OnTouchEvent Method

Android.Views.View.OnSizeChanged Method

public override bool OnTouchEvent(MotionEvent e)
{
}

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

Mono coding guidelines.