private void drawCircleBitmapShader(Canvas canvas){
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
paint.setShader(bitmapShader);
//获取半径
int radius = Math.min(bitmap.getWidth(),bitmap.getHeight());
RectF rect = new RectF(0,0,radius,radius);
//画椭圆型
canvas.drawOval(rect,paint);
}
圆形头像.jpg
private void drawCircleMatrixBitmapShader(Canvas canvas,int radius){
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
paint.setShader(bitmapShader);
Matrix matrix = new Matrix();
//获取缩放比较小的,以保持缩放比一致,防止拉伸图片
float scale = Math.min((float) radius/bitmap.getWidth(),(float) radius/bitmap.getHeight());
matrix.setScale(scale,scale);
bitmapShader.setLocalMatrix(matrix);
RectF rect = new RectF(0,0,radius,radius);
//画椭圆型
canvas.drawOval(rect,paint);
}
缩放后的圆形头像.jpg