Android圆形图片的实现以及设置边缘颜色、宽度
public static Bitmap getCircleBitmap(Bitmap bitmap, int width, int height, int color, int border) {
if (bitmap == null) {
return null;
}
int radius;
int mWidth = bitmap.getWidth();
int mHeight = bitmap.getHeight();
float widthScale = width * 1f / mWidth;
float heightScale = height * 1f / mHeight;
Matrix matrix = new Matrix();
matrix.setScale(widthScale, heightScale);
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
if (height > width) {
radius = width / 2;
} else {
radius = height / 2;
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
bitmapShader.setLocalMatrix(matrix);
paint.setShader(bitmapShader);
canvas.drawCircle(width / 2, height / 2, radius - border, paint);
if (border > 0) {
Paint borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
borderPaint.setColor(color);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(border);
canvas.drawCircle(width / 2, height / 2, radius - border, borderPaint);
}
return result;
}