把方形头像切成圆形的头像工具类

 /**
     * Crops a circle out of the thumbnail photo.
     */
    public Bitmap getCroppedBitmap(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Config.ARGB_8888);
        //设置一个图片大小的矩形
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
      //bm是一个刚好canvas大小的空Bitmap ,画完后应该会自动保存到bm
        Canvas canvas = new Canvas(output);


        final Paint paint = new Paint();
        paint.setAntiAlias(true);


        int halfWidth = bitmap.getWidth()/2;
        int halfHeight = bitmap.getHeight()/2;
        //画圆
        canvas.drawCircle(halfWidth, halfHeight, Math.max(halfWidth, halfHeight), paint);
        //设置为取两层图像交集部门,只显示上层图像
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        //画图像
        canvas.drawBitmap(bitmap, rect, rect, paint);


        return output;
    }

 

你可能感兴趣的:(工具类)