android 把图片生成圆角

来源:互联网 发布:windows高级编程 pdf 编辑:程序博客网 时间:2024/06/06 04:56

 

public static Bitmap GetRoundedCornerBitmap(Bitmap bitmap) {
  try {
   Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
     bitmap.getHeight(), Config.ARGB_8888);
   Canvas canvas = new Canvas(output);
   final Paint paint = new Paint();
   final Rect rect = new Rect(0, 0, bitmap.getWidth(),
     bitmap.getHeight());
   final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(),
     bitmap.getHeight()));
   final float roundPx = 14;
   paint.setAntiAlias(true);
   canvas.drawARGB(0, 0, 0, 0);
   paint.setColor(Color.BLACK);
   canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
   paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
   final Rect src = new Rect(0, 0, bitmap.getWidth(),
     bitmap.getHeight());
   canvas.drawBitmap(bitmap, src, rect, paint);
   return output;
  } catch (Exception e) {
   return bitmap;
  }
 }

 public static Drawable toRoundCorner(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 rect2 = new Rect(bitmap.getWidth() / 50,
    bitmap.getHeight() / 50, bitmap.getWidth() - bitmap.getWidth()
      / 50, bitmap.getHeight() - bitmap.getHeight() / 50);
  final RectF rectF2 = new RectF(rect2);
  final float roundPx2 = pixels;

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

  paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  canvas.drawBitmap(bitmap, rect, rect, paint);
  BitmapDrawable bd = new BitmapDrawable(output);

  return bd;
 }

 

 

 

 Drawable   ======》   (BitmapDrawable)d).getBitmap()

原创粉丝点击