2017-02-21 220 views
13

我的應用程序中有一些球只是通過顯示器飛行。他們畫我想要的。但現在我想要在他們身後畫上蹤跡。安卓繪製球軌跡

所有我可以做只是通過canvas.drawPath畫的東西像下面的圖片:

I have it now

但它不是我想要的。它應該有尖尖的尾巴和漸變色是這樣的:

I want that

我不知道如何做到這一點。嘗試BitmapShader - 無法做出正確的事情。請幫助。

代碼:

首先,有Point類位置上顯示:

class Point { 
    float x, y; 
    ... 
} 

和TRAIL存儲爲Point隊列:

private ConcurrentLinkedQueue<Point> trail; 

一點也沒有不管它如何填充,只知道它有大小限制:

DrawTrail方法
trail.add(position); 
if(trail.size() > TRAIL_MAX_COUNT) { 
    trail.remove(); 
} 

和繪製發生了:

private void DrawTrail(Canvas canvas) { 
    trailPath.reset(); 
    boolean isFirst = true; 
    for(Point p : trail) { 
     if(isFirst) { 
      trailPath.moveTo(p.x, p.y); 
      isFirst = false; 
     } else { 
      trailPath.lineTo(p.x, p.y); 
     } 
    } 
    canvas.drawPath(trailPath, trailPaint); 
} 

順便說一句,trailPaint是真的很肥油漆:)

trailPaint = new Paint(); 
trailPaint.setStyle(Paint.Style.STROKE); 
trailPaint.setColor(color); 
trailPaint.setStrokeWidth(radius * 2); 
trailPaint.setAlpha(150); 
+3

發佈您的代碼至少 – Dimezis

+0

做了@Dimezis – Ircover

+0

@Ircover可以發佈完整的代碼,以便我可以複製該問題。 – Gattsu

回答

0

我找到了解決辦法。但仍然認爲這不是最好的。

首先,我的班級字段用於該任務。

static final int TRAIL_MAX_COUNT = 50; //maximum trail array size 
static final int TRAIL_DRAW_POINT = 30; //number of points to split the trail for draw 

private ConcurrentLinkedQueue<Point> trail; 
private Paint[] trailPaints; 
private float[][] trailPoss, trailTans; 
private Path trailPath; 

此外到trailPath對象我用PathMeasure對象路徑分割到多個相等的部分。

填充路徑數組對象後添加了路徑計算函數的調用。

lastTrailAdd = now; 
trail.add(pos.Copy()); 
if (trail.size() > TRAIL_MAX_COUNT) { 
    trail.remove(); 
} 
FillTrail(); 

然後我FillTrail功能。

private void FillTrail() { 
    trailPath.reset(); 
    boolean isFirst = true; 
    for(Point p : trail) { 
     if(isFirst) { 
      trailPath.moveTo(p.x, p.y); 
      trailPoss[0][0] = p.x; 
      trailPoss[0][1] = p.y; 
      isFirst = false; 
     } else { 
      trailPath.lineTo(p.x, p.y); 
     } 
    } 
    PathMeasure path = new PathMeasure(trailPath, false); 
    float step = path.getLength()/TRAIL_DRAW_POINT; 
    for(int i=0; i<TRAIL_DRAW_POINT; i++) { 
     path.getPosTan(step * i, trailPoss[i], trailTans[i]); 
    } 
} 

它與畫線分離。下一個代碼是繪圖功能。

private void DrawTrail(Canvas canvas) { 
    if(trail.size() > 1) { 
     float prevWidthHalfX = 0f, prevWidthHalfY = 0f, prevX = 0f, prevY = 0f; 
     Path trailStepRect = new Path(); 
     boolean isFirst = true; 
     for (int i = 0; i < TRAIL_DRAW_POINT; i++) { 
      float currWidthHalf = (float) (radius) * i/TRAIL_DRAW_POINT/2f, 
        currWidthHalfX = currWidthHalf * trailTans[i][1], 
        currWidthHalfY = currWidthHalf * trailTans[i][0], 
        currX = trailPoss[i][0], currY = trailPoss[i][1]; 
      if (!isFirst) { 
       trailStepRect.reset(); 
       trailStepRect.moveTo(prevX - prevWidthHalfX, prevY + prevWidthHalfY); 
       trailStepRect.lineTo(prevX + prevWidthHalfX, prevY - prevWidthHalfY); 
       trailStepRect.lineTo(currX + currWidthHalfX, currY - currWidthHalfY); 
       trailStepRect.lineTo(currX - currWidthHalfX, currY + currWidthHalfY); 
       canvas.drawPath(trailStepRect, trailPaints[i]); 
      } else { 
       isFirst = false; 
      } 
      prevX = currX; 
      prevY = currY; 
      prevWidthHalfX = currWidthHalfX; 
      prevWidthHalfY = currWidthHalfY; 
     } 
    } 
} 

這是由不同塗料的零件繪製軌跡。更接近球 - 更寬的軌跡。我想我會優化它,但它已經準備就緒。

如果你想看它看起來只是安裝my app from google play

2

我看到你想看到球漸變路徑,你可以使用類似這樣的東西

int x1 = 0, y1 = 0, x2 = 0, y2 = 40; 
Shader shader = new LinearGradient(0, 0, 0, 40, Color.WHITE, Color.BLACK, TileMode.CLAMP); 
trailPaint = new Paint(); 
trailPaint.setShader(shader); 

這是你應該改變你的trailPaint,看看它是否有效。

here提供。

+0

我不需要漸變球。梯度應該是球路徑。 – Ircover

+0

我編輯了我的評論。 – Destry