看到QQ有圆角的图片感觉效果还不错,于是就想做一个,开始的时候尝试了用.9这的图片去做为背景图,但是试了几个都没有解决问题,最后只有写代码来完成了,根据之前合并图片的知识就写了下代码,也参考了下网上的资料,下面贴出自己的代码,希望对大家有帮助:
/**
* 转换成圆角图片
* @param bitmap 原始的图片
* @param roundPx 圆角的直径
* @return
*/
public static Bitmap convertRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
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);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
//转换
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}