2016-12-18 10 views
0

私は、useresがタブレット上にアクティブなペンを使って描画して書くことができるAndroidアプリケーションを作成しています。 ユーザは、異なるツールを提供する異なるモード(例えば、ペン、消しゴム、ライン、円形など)の中から選択することができる。
ラインツールとサークルツールを使用すると、ユーザーが長さ/半径と方向を指定して線を描くことができます。これは非常にうまくいっていますが、ユーザーがペンを動かすたびに、別の線/円が描かれ、画面がいっぱいになります。Android:ある点から別の点に線を引かせる

画像: Current result vs. how it should be when drawing from circle center

コード:

@Override 
protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
    super.onSizeChanged(w, h, oldw, oldh); 
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
    canvas = new Canvas(mBitmap); 
} 

@Override 
protected void onDraw(Canvas c){ 
    c.drawBitmap(mBitmap, 0, 0, bitmapPaint); 
} 

private float mX, mY, firstX, firstY; 

private void touch_start(float x, float y) { //called from MotionEvent.ACTION_DOWN 
    path.moveTo(x, y); 
    firstX = x; 
    firstY = y; 
    mX = x; 
    mY = y; 
} 

private void touch_move(float x, float y) { //called from MotionEvent.ACTION_MOVE 
    path.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
    switch(mode){ 
     case "line": 
      canvas.drawLine(firstX, firstY, x, y, paint); 
      break; 
     case "circle": 
      canvas.drawCircle(firstX, firstY, (Math.abs(firstX-x) + Math.abs(firstY-y))/1.5f, paint); // divisor 1.5 just a rough value 
      break; 
     default: 
      canvas.drawPath(path, paint); 
      break; 
    } 
    mX = x; 
    mY = y; 
} 

誰に私はこの問題を解決できるかのアイデアを持って?
ありがとうございます。

+0

これは新しいtouch_moveです。あなたが 'touch_move()'を呼び出す前に 'invalidate()'を持っていなければ、それも必要かもしれません。 – Gary99

答えて

0

自分で解決策を見つけました。
描かれている間に円を表示するために、新しいキャンバスanimationCanvasとそれ自身のanimationBitmapを作成しました。
最後の円はMotionEvent.ACTION_UPの方法で描かれています。たぶん

private void touch_move(float x, float y) { //called from MotionEvent.ACTION_MOVE 
    path.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
    switch(mode){ 
     case "line": 
      animationCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); // clear previously drawn line (for animation) 
      animationCanvas.drawLine(firstX, firstY, x, y, paint); 
      break; 
     case "circle": 
      animationCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); // clear previously drawn circle (for animation) 
      animationCanvas.drawCircle(firstX, firstY, (Math.abs(firstX-x) + Math.abs(firstY-y))/1.5f, paint); // divisor 1.5 just a rough value 
      break; 
     default: 
      canvas.drawPath(path, paint); 
      break; 
    }  
    mX = x; 
    mY = y; 
} 

ない最善の解決策が、それは動作します:)あなたはおそらく、() ``前に各drawXxx() `` canvas.resetが必要

関連する問題