2011-08-10 167 views
5

我有一個小小的繪畫應用程序,並希望使用「複雜」的形狀作爲畫筆,即明星。 用一個簡單的畫筆繪製已可與下面的代碼:如何使用位圖繪製路徑?

Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.brush_star); 

我的解決方案目前正在使用的點(座標)列表得出:

remotePath.reset(); 
remotePath.moveTo(start_x, start_y); 

float dx = Math.abs(end_x - start_x); 
float dy = Math.abs(end_y - start_y); 
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 
     remotePath.quadTo(start_x, start_y, (end_x + start_x)/2, (end_y + start_y)/2); 
} 

remotePath.lineTo(end_x, end_y); 
// commit the path to our offscreen 
mCanvas.drawPath(remotePath, remotePaint); 
// kill this so we don't double draw 
remotePath.reset(); 
invalidate(); 

我使用這個位圖基本上要相同的功能位圖。該解決方案的問題在於,它僅在給定點處繪製位圖,導致每個繪製的位圖之間存在間隙。我寧願用簡單的筆刷畫出平滑的線條,而沒有間隙。

爲位圖繪製當前代碼:

 protected void onDraw(Canvas canvas) { 

     // Make canvas white 
     canvas.drawColor(Color.WHITE); 

     // Paintable area 
     canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 

     canvas.drawPath(mPath, mPaint); 

     for (Point point : points) { 
      canvas.drawBitmap(complexBrush, point.x, point.y, p); 
     } 
    } 

什麼是這樣做的最佳方法是什麼? 感謝您的幫助!

回答

3

我用這個 點的類:

public class Point implements Serializable { 
float x, y; 
float dx, dy; 
} 

噴漆的對象:在畫布上

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
paint.setStyle(Paint.Style.STROKE); 
paint.setStrokeWidth(5); 
paint.setColor(Color.RED); 
paint.setAntiAlias(true); 

抽獎:

private void drawBitmap(Bitmap bmp, List<Point> pts) { 
    Canvas c = new Canvas(bmp); 
    drawCanvas(c, pts); 
} 
0:

private void drawCanvas(Canvas canvas, List<Point> pts){ 
    if (pts.size() > 1){ 
     Path path = new Path(); 
     final int SMOOTH_VAL = 6; 
     for(int i = pts.size() - 2; i < pts.size(); i++){ 
      if(i >= 0){ 
       Point point = pts.get(i); 

       if(i == 0){ 
        Point next = pts.get(i + 1); 
        point.dx = ((next.x - point.x)/SMOOTH_VAL); 
        point.dy = ((next.y - point.y)/SMOOTH_VAL); 
       } 
       else if(i == pts.size() - 1){ 
        Point prev = pts.get(i - 1); 
        point.dx = ((point.x - prev.x)/SMOOTH_VAL); 
        point.dy = ((point.y - prev.y)/SMOOTH_VAL); 
       } 
       else{ 
        Point next = pts.get(i + 1); 
        Point prev = pts.get(i - 1); 
        point.dx = ((next.x - prev.x)/SMOOTH_VAL); 
        point.dy = ((next.y - prev.y)/SMOOTH_VAL); 
       } 
      } 
     } 

     boolean first = true; 
     for(int i = 0; i < pts.size(); i++){ 
      Point point = pts.get(i); 
      if(first){ 
       first = false; 
       path.moveTo(point.x, point.y); 
      } 
      else{ 
       Point prev = pts.get(i - 1); 
       path.cubicTo(prev.x + prev.dx, prev.y + prev.dy, point.x - point.dx, point.y - point.dy, point.x, point.y); 
      } 
     } 
     canvas.drawPath(path, paint); 
    } else { 
     if (pts.size() == 1) { 
      Point point = pts.get(0); 
      canvas.drawCircle(point.x, point.y, 2, paint); 
     } 
    } 
} 

位圖畫布上繪製

+0

我們如何繪製多個位圖?就像我們改變顏色並繪製多條路徑 –