/** * 得到圆形的图片 * @param bitmap * @return */ public Bitmap getCircleBitmap(Bitmap bitmap){ try { Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(circleBitmap); 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())); float roundPx = 0.0f; //以较短的为准 if(bitmap.getWidth() > bitmap.getHeight()){ roundPx = bitmap.getHeight() / 2.0f; }else{ roundPx = bitmap.getWidth() / 2.0f; } paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(Color.WHITE); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); //图片和圆形相交的部分,取图片上相交的 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); final Rect src = new Rect(0,0,bitmap.getWidth(),bitmap.getHeight()); canvas.drawBitmap(bitmap,src,rect,paint); return circleBitmap; }catch (Exception e){ return bitmap; } }