1

アクティビティで 2 つのサークル ondraw を作成してみました。しかし、私が本当に達成したいのは、タッチムーブで線を引くことです。ユーザーは、指の動きに応じて、指を使って線を引くことができます。これが私の絵にドットを付ける方法です。

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

Canvas に指で描画する方法を示すプロジェクトがあります。

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 に答える