android 给图片添加圆角

 public static final Bitmap round(Bitmap src, float corner, int bgColor) {
  Bitmap bitmap = src;

  Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
    bitmap.getHeight(),Config.ARGB_8888);
  Canvas canvas = new Canvas(output);
  Paint paint = new Paint();
  final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
  final RectF rectF = new RectF(rect);
  final float roundPx = corner;
  paint.setAntiAlias(true);
  canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
  paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  canvas.drawBitmap(bitmap, rect, rect, paint);
  if (bgColor != -1) {
   bitmap = Bitmap.createBitmap(output.getWidth(), output.getHeight(),
     Config.ARGB_8888);
   canvas = new Canvas(bitmap);
   final int color = bgColor;
   paint = new Paint();
   paint.setAntiAlias(true);
   paint.setColor(color);
   canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
   canvas.drawBitmap(output, rect, rect, paint);
   output = bitmap;
  }
  return output;
 }

你可能感兴趣的:(android 给图片添加圆角)