1

我尝试在活动中创建两个圆圈。但我真正想要完成的是在触摸移动上画一条线。用户将能够根据手指的移动使用他/她的手指绘制一条线。这就是我如何为我的画点画的。

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;

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

            mPaint = new Paint();
            mPaint.AntiAlias = true;
        }

        protected override void OnDraw(Canvas canvas)
        {
            canvas.DrawColor(Color.White);

            canvas.Translate(10, 10);

            canvas.SaveLayerAlpha(0, 0, 200, 200, 0x88, SaveFlags.All);

            mPaint.Color = Color.Red;
            canvas.DrawCircle(75, 75, 75, mPaint);
            mPaint.Color = Color.Blue;
            canvas.DrawCircle(125, 125, 75, mPaint);

            canvas.Restore();
        }
    }
4

2 回答 2

1

我有一个项目,它展示了如何用手指在画布上绘图:

https://github.com/Cheesebaron/MonoDroid.CaptureSignature

您可能对CaptureSignatureView包含在画布上绘图的所有逻辑的东西感兴趣。

于 2013-03-01T01:59:35.907 回答
-1

试试下面的代码:

public class FingerDrawView extends View
   {
private Paint m_paint;
private static Bitmap m_bitmap;
private Canvas m_canvas;
private Path m_path;
private Paint m_bitmapPaint;
private DisplayMetrics m_metrics;
private int m_color = 0xFFFF0000;
static boolean m_pathDrawn = false;
private float m_X, m_Y;
private static final float TOUCH_TOLERANCE = 4;

public FingerDrawView(Context p_c)
{
    super(p_c);
    m_paint = new Paint();
    m_paint.setAntiAlias(true);
    m_paint.setDither(true);
    m_paint.setColor(m_color);
    new ColorPickerDialog(p_c, new OnColorChangedListener()
    {
        public void colorChanged(int p_color)
        {
            m_paint.setColor(p_color);
            m_color = p_color;
        }
    }, 0xFFFF0000).show();
    m_paint.setStyle(Paint.Style.STROKE);
    m_paint.setStrokeJoin(Paint.Join.ROUND);
    m_paint.setStrokeCap(Paint.Cap.ROUND);
    m_paint.setStrokeWidth(12);
    m_metrics = p_c.getResources().getDisplayMetrics();
    m_bitmap =
            Bitmap.createBitmap(m_metrics.widthPixels, m_metrics.heightPixels,
                    Bitmap.Config.ARGB_8888);
    m_canvas = new Canvas(m_bitmap);
    m_path = new Path();
    m_bitmapPaint = new Paint(Paint.DITHER_FLAG);
}

public void onerase()
{
    m_canvas = null;
}

@Override
protected void onSizeChanged(int p_w, int p_h, int p_oldw, int p_oldh)
{
    super.onSizeChanged(p_w, p_h, p_oldw, p_oldh);
}

@Override
protected void onDraw(Canvas p_canvas)
{
    p_canvas.drawColor(0x00000000);
    p_canvas.drawBitmap(m_bitmap, 0, 0, m_bitmapPaint);
    p_canvas.drawPath(m_path, m_paint);
}

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, m_paint);
    // kill this so we don't double draw
    m_path.reset();
}
@Override
public boolean onTouchEvent(MotionEvent p_event)
{
    float m_x = p_event.getX();
    float m_y = p_event.getY();
    switch (p_event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                touch_start(m_x, m_y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(m_x, m_y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
        }
    return true;
}
  }

使用上面的类如下:

    FingerDrawView   m_cv = new FingerDrawView(MainActivity.this);
  setContentView(m_cv);

我希望它会帮助你。

谢谢

于 2013-02-28T05:19:41.963 回答