Android裁剪图片为圆形

public Bitmap getCircleBitmap(){
  //需要裁剪的源图片
  Bitmap resBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
  int targetWidth,targetHeight,radius;
  //新图片的长和宽,为正方形(取两者中小的)
  targetHeight = targetWidth=Math.max(resBitmap.getWidth(), resBitmap.getHeight());
  //图片的半径
  radius = targetHeight/4;
  //create a new Bitmap
  Bitmap outBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Config.ARGB_8888);
  Canvas canvas = new Canvas(outBitmap);
  Paint paint = new Paint();
  final int color = 0xff424242;
  paint.setColor(color);
         //以中心点为圆心,radius为半径画一个圆
  canvas.drawCircle(targetHeight/2, targetHeight/2, radius, paint);
  //设置两张图片交互时的方式为SRC_IN
  paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  canvas.drawBitmap(resBitmap, 0, 0, paint);
  return outBitmap;
 }

你可能感兴趣的:(Android开发)