2012-01-18 176 views
0

我有圖像列表。我已經使用此功能四捨五入我的形象的角落:Android圖像模糊圓角

public Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) { 
     Bitmap output = null; 
     try { 
      if(bitmap != null) { 
       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); 
      } 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return output; 
    } 

它成功地輪圖像的角落,但角模糊,粗糙和像素化。角落不光滑。示例輸出:

enter image description here

我該如何解決這個問題?

回答

0

我看不出有任何問題,在你的代碼,但我用這個代碼:

public static Bitmap roundBitmap(Bitmap bitmap, int radius) { 
    Paint paintForRound = new Paint(); 
    paintForRound.setAntiAlias(true); 
    paintForRound.setColor(0xff424242); 
    paintForRound.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 

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

    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
    final RectF rectF = new RectF(rect); 

    canvas.drawARGB(0, 0, 0, 0); 
    paintForRound.setXfermode(null); 

    canvas.drawRoundRect(rectF, radius, radius, paintForRound); 

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

    return output; 
} 

該代碼給出了像素完美的結果:

0

使用使用圓角半徑此公式

float roundPx = bitmap.getWidth() > bitmap.getHeight() ? ((float) bitmap.getHeight())/6 : ((float) bitmap.getWidth())/6; 
     roundPx = roundPx < 10 ? 10 : roundPx;