2013-03-27 117 views
-2

我想剪輯在Java中畫布的左上角和右上角。我知道你可以在所有角落都使用addRoundRect,但我不確定要做什麼才能做到頂角。與路徑圓頂角

這是我目前有:

@Override 
protected void onDraw(Canvas canvas) { 
    float radius = 12f; 
    Path clipPath = new Path(); 
    RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight()); 
    //uh... 
    //clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW); 

    canvas.clipPath(clipPath); 
    super.onDraw(canvas); 
}  
+2

可能的重複[如何使用Android畫布繪製矩形只有頂部和頂部的圓角?](http://stackoverflow.com/questions/5896234/how-to-use-android-canvas-to-抽取一個矩形與 - 僅-左上和 - topright-COR) – 2017-05-22 10:54:08

回答

3

你可以破解它。只需設置RectF較大的儘可能多的像素作爲圓角這樣的半徑:

RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight() + 12.0f); // draw a larger rect 

我猜你將不得不設置油漆顏色完全透明(0x00fffffff)。

2

您可以使用另一種方法重載addRoundRect()這樣的:

int width = view.getWidth(); 
int height = view.getHeight();  
float[] radii = {0, 0, 0, 0, 0, 0, 0, 0}; 
if(mRadiusTop) { 
    radii[0] = mRadius; 
    radii[1] = mRadius; 
    radii[2] = mRadius; 
    radii[3] = mRadius; 
} 
if(mRadiusBottom) { 
    radii[4] = mRadius; 
    radii[5] = mRadius; 
    radii[6] = mRadius; 
    radii[7] = mRadius; 
} 

clipPath.addRoundRect(new RectF(0, 0, width, height), radii, Path.Direction.CW); 
canvas.clipPath(clipPath); 

我通過上面的代碼解決了這個問題。