2013-05-27 3 views
0

내가 이루고자하는 것은 간단합니다. 매우 기본 및/또는 원시 드로잉 응용 프로그램을 만듭니다. 브러시 크기를 변경하거나 색상을 변경할 필요가 없으며 단순한 그리기 응용 프로그램을 사용하여 간단한 종이 유형에 그릴 수 있습니다.페인팅/드로잉에서 안기고있는 Android 개발

나는 캔버스와 페인트에 대해 알고 있지만 내가 언급 한 자유로운 그림 개념을 구현하는 방법을 모르겠다. 어떤 도움이 크게 apprechiated 것입니다.

@Override 
    protected void onDraw(Canvas canvas) { 
    Paint p = new Paint(); 

    p.setColor(Color.BLUE); 

    int width = getWidth(); 
    int height = getHeight(); 

    canvas.drawLine(0, 0, width, height, p); 
} 
+0

당신은 무료로 손으로 그릴 필요? 그것은 당신이 찾고있는 것입니까? – Raghunandan

+1

그래 무료 손으로 그리기 –

+0

아래의 답변을 확인하면 화면에 손가락을 움직여서 자유롭게 그려 낼 수 있습니다. – Raghunandan

답변

1

클래스에서보기를 확장해야합니다. onDraw (캔버스)를 재정 의하여 패스를 그립니다. onTouch (이벤트)를 재정 의하여 무효화를 호출하여 무승부를 새로 고칩니다. 그리는 경로를 사용합니다.

public class MainActivity extends Activity { 

DrawingView dv ; 
RelativeLayout rl; 
private Paint  mPaint; 
private DrawingManager mDrawingManager=null; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    dv = new DrawingView(this); // custom view 
    setContentView(dv); 
    mPaint = new Paint(); 
    mPaint.setAntiAlias(true); 
    mPaint.setDither(true); 
    mPaint.setColor(Color.GREEN); 
    mPaint.setStyle(Paint.Style.STROKE); 
    mPaint.setStrokeJoin(Paint.Join.ROUND); 
    mPaint.setStrokeCap(Paint.Cap.ROUND); 
    mPaint.setStrokeWidth(12); // paint thickness 
} 

public class DrawingView extends View { 

     public int width; 
     public int height; 
     private Bitmap mBitmap; 
     private Canvas mCanvas; 
     private Path mPath; 
     private Paint mBitmapPaint; 
     Context context; 
     private Paint circlePaint; 
     private Path circlePath; 

     public DrawingView(Context c) { 
     super(c); 
     context=c; 
     mPath = new Path(); // path used to draw 
     mBitmapPaint = new Paint(Paint.DITHER_FLAG); 

     circlePaint = new Paint(); 

     circlePath = new Path(); 
     circlePaint.setAntiAlias(true); 
     circlePaint.setColor(Color.BLUE); 
     circlePaint.setStyle(Paint.Style.STROKE); 
     circlePaint.setStrokeJoin(Paint.Join.MITER); 
     circlePaint.setStrokeWidth(4f); 
        // draw a circle when user touch and move. 
     } 

     @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); 
     mCanvas = new Canvas(mBitmap); 

     } 
     @Override 
     protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);  
     canvas.drawPath(mPath, mPaint); // draw the path 
     canvas.drawPath(circlePath, circlePaint); //draw the circle 
     } 

     private float mX, mY; 
     private static final float TOUCH_TOLERANCE = 4; 

     private void touch_start(float x, float y) { 
     mPath.reset(); 
     mPath.moveTo(x, y); 
     mX = x; 
     mY = y; 
     } 
     private void touch_move(float x, float y) { 
     float dx = Math.abs(x - mX); 
     float dy = Math.abs(y - mY); 
     if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 
      mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
      mX = x; 
      mY = y; 

      circlePath.reset(); 
      circlePath.addCircle(mX, mY, 30, Path.Direction.CW); 
     } 
     } 
     private void touch_up() { 
     mPath.lineTo(mX, mY); 
     circlePath.reset(); 
     // commit the path to our offscreen 
     mCanvas.drawPath(mPath, mPaint); 
     // kill this so we don't double draw 
     mPath.reset(); 
     } 

     @Override 
     public boolean onTouchEvent(MotionEvent event) { // touch events 
     float x = event.getX(); 
     float y = event.getY(); 

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       touch_start(x, y); 
       invalidate(); // invalidate to refresh 
       break; 
      case MotionEvent.ACTION_MOVE: 
       touch_move(x, y); 
       invalidate(); 
       break; 
      case MotionEvent.ACTION_UP: 
       touch_up(); 
       invalidate(); 
       break; 
     } 
     return true; 
     } 
     } 

}

enter image description here

+0

매우 잘 설명되어 있습니다. –