2013-06-20 33 views
6

我用這很常見的類圓角:四捨五入只有一個圖像角 - 不是所有四個

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) { 
     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap 
       .getHeight(), Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 

     final int color = 0xff424242; 
     final Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
     final RectF rectF = new RectF(rect); 
     final float roundPx = pixels; 

     paint.setAntiAlias(true); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setColor(color); 
     canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 

     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
     canvas.drawBitmap(bitmap, rect, rect, paint); 

     return output; 
    } 

我想修改它,只有左上角是圓形的。我無法在代碼中找到這個參數嗎?有人可以協助嗎?

+0

這可能有幫助 - http://developer.android.com/reference/android/graphics/drawable/shapes/RoundRectShape.html – Varun

+0

@Varun我需要更多的信息,然後呢?當你想要某些特定的東西時,開發者文檔並不總是友好的。 – KickingLettuce

回答

5

這可能不是最有效的方法,但可以通過在當前蒙版頂部繪製來填充圓角。您可以從當前代碼開始,然後在適當的區域(角點)上使用canvas.drawRect(在撥打canvas.drawRoundRect之後)。我想象這樣的事情會圓只有左上角:

public static Bitmap getRoundedTopLeftCornerBitmap(Bitmap bitmap, int pixels) { 
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap 
      .getHeight(), Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 

    final int color = 0xff424242; 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
    final RectF rectF = new RectF(rect); 
    final float roundPx = pixels; 
    final Rect topRightRect = new Rect(bitmap.getWidth()/2, 0, bitmap.getWidth(), bitmap.getHeight()/2); 
    final Rect bottomRect = new Rect(0, bitmap.getHeight()/2, bitmap.getWidth(), bitmap.getHeight()); 

    paint.setAntiAlias(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(color); 
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
    // Fill in upper right corner 
    canvas.drawRect(topRightRect, paint); 
    // Fill in bottom corners 
    canvas.drawRect(bottomRect, paint); 

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
    canvas.drawBitmap(bitmap, rect, rect, paint); 

    return output; 
} 

有一點優化,如果你喜歡,你可以在這裏做的,但我認爲它應該工作。總的想法絕對應該。我還沒有嘗試或測試過這段代碼,如果pixels > bitmap.getWidth()/2pixels > bitmap.getHeight()/2看起來不正確。再一次,這也可能是真的。

+0

這樣做了,謝謝!你認爲這會對每個「listView」中的圖像產生性能影響嗎?標記你也是正確的。 – KickingLettuce

+1

我不會想象這會造成嚴重的性能問題,但如果您的意思是它在每個ListView項中,那麼它在滾動時可能會加起來讓事情變得不太流暢。如果發生這種情況,您將不得不按照異步加載的常規方法進行操作。只需嘗試一下,看看它在嘗試過度優化之前的感受。 – kabuko

+0

我該如何去做一個方形圖像,四捨五入,然後提取圖像的右上角1/4? – MCR

1

這更多的是一個概念性的答案,但是您可以繪製一個圓角矩形,然後在您希望圓角的角落頂部劃兩個正常的矩形?

Visual example

+0

這實際上是我在上面的答案中做的,儘管尺寸略有不同。 – kabuko

4
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int topLeftX, int topLeftY, int topRightX, int topRightY, int bottomRightX, int bottomRightY, int bottomLeftX, int bottomLeftY) { 
     final Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 
     // the float array passed to this function defines the x/y values of the corners 
     // it starts top-left and works clockwise 
     // so top-left-x, top-left-y, top-right-x etc 
     RoundRectShape rrs = new RoundRectShape(new float[]{topLeftX, topLeftY, topRightX, topRightY, bottomRightX, bottomRightY, bottomLeftX, bottomLeftY}, null, null); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setAntiAlias(true); 
     paint.setColor(0xFF000000); 
     rrs.resize(bitmap.getWidth(), bitmap.getHeight()); 
     rrs.draw(canvas, paint); 
     paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN)); 
     canvas.drawBitmap(bitmap, rect, rect, paint); 
     return output; 
    } 

或者,你可以看到RoundRects.java源代碼 - 例如顯示如何製作圓角,提供SDK樣本:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.3_r2.1/com/example/android/apis/graphics/RoundRects.java/

+0

我不太瞭解你的代碼。如果我想將圖像四捨五入,然後裁剪到右上角1/4,我可以用這個代碼來做到嗎? – MCR

2

這是選擇的角落:

public static Bitmap getRoundedCornerBitmap(Context context, Bitmap bitmap, float roundDip, boolean roundTL, boolean roundTR, boolean roundBL, boolean roundBR) 
{ 
    try 
    { 

     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 

     final int color = 0xff424242; 
     final Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
     final RectF rectF = new RectF(rect); 
     final float roundPx = convertDipToPixel(roundDip, context); 

     paint.setAntiAlias(true); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setColor(color); 
     canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// draw round 
                   // 4Corner 

     if (!roundTL) 
     { 
      Rect rectTL = new Rect(0, 0, bitmap.getWidth()/2, bitmap.getHeight()/2); 
      canvas.drawRect(rectTL, paint); 
     } 
     if (!roundTR) 
     { 
      Rect rectTR = new Rect(bitmap.getWidth()/2, 0, bitmap.getWidth(), bitmap.getHeight()/2); 
      canvas.drawRect(rectTR, paint); 
     } 
     if (!roundBR) 
     { 
      Rect rectBR = new Rect(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth(), bitmap.getHeight()); 
      canvas.drawRect(rectBR, paint); 
     } 
     if (!roundBL) 
     { 
      Rect rectBL = new Rect(0, bitmap.getHeight()/2, bitmap.getWidth()/2, bitmap.getHeight()); 
      canvas.drawRect(rectBL, paint); 
     } 

     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
     canvas.drawBitmap(bitmap, rect, rect, paint); 

     return output; 
    } catch (Exception e) 
    { 
    } 
    return bitmap; 
} 
+0

沒有爲我工作 – JMRboosties

0

如果您需要繪製(在畫布上)具有不同半徑的圓角,可以使用以下代碼:

private void drawAsymmetricRoundRect(Canvas canvas, RectF rectF, float[] radii, Paint paint) { 
    float topLeftX = rectF.left + radii[0]; 
    float topLeftY = rectF.top + radii[0]; 
    float topRightX = rectF.right - radii[1]; 
    float topRightY = rectF.top + radii[1]; 
    float bottomRightX = rectF.right - radii[2]; 
    float bottomRightY = rectF.bottom - radii[2]; 
    float bottomLeftY = rectF.bottom - radii[3]; 
    float bottomLeftX = rectF.left + radii[3]; 
    RectF topLeftCorner = new RectF(rectF.left, rectF.top, topLeftX + radii[0], topLeftY + radii[0]); 
    RectF topRightCorner = new RectF(topRightX - radii[1], rectF.top, rectF.right, topRightY + radii[1]); 
    RectF bottomRightCorner = new RectF(bottomRightX - radii[2], bottomRightY - radii[2], rectF.right, rectF.bottom); 
    RectF bottomLeftCorner = new RectF(rectF.left, bottomLeftY - radii[3], bottomLeftX + radii[3], rectF.bottom); 

    canvas.drawArc(topLeftCorner, 180, 90, true, paint); 
    canvas.drawArc(topRightCorner, 270, 90, true, paint); 
    canvas.drawArc(bottomRightCorner, 0, 90, true, paint); 
    canvas.drawArc(bottomLeftCorner, 90, 90, true, paint); 
    canvas.drawRect(topLeftX, rectF.top, topRightX, bottomLeftY < bottomRightY ? bottomLeftY : bottomRightY, paint); //top rect 
    canvas.drawRect(topLeftX > bottomLeftX ? topLeftX : bottomLeftX, topRightY, rectF.right, bottomRightY, paint); //right rect 
    canvas.drawRect(bottomLeftX, topLeftY > topRightY ? topLeftY : topRightY, bottomRightX, rectF.bottom, paint); //bottom rect 
    canvas.drawRect(rectF.left, topLeftY, bottomRightX < topRightX ? bottomRightX : topRightX, bottomLeftY, paint); //left rect 
} 

float[] radii是浮點陣列(長度= 4),其存儲你的角部的半徑的尺寸(順時針,從左上角=>{topLeft, topRight, bottomRight, bottomLeft}開始)。

基本上這種方法繪製4弧(角),並填充4角之間的所有角落。

重要說明:我在此方法中放置了角RectFs的初始化以降低發佈代碼的複雜性。由於事實上你很可能會從你的onDraw()方法中調用這個方法,所以你應該提取這部分代碼,並將它放在你初始化其他地方的地方(只要你還沒有在onDraw()中初始化它們:P) 。