protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userHead= (ImageView) findViewById(R.id.userhead);
Bitmap b = BitmapFactory.decodeResource(getResources(),R.mipmap.my);
userHead.setImageBitmap(toRoundBitmap(b));
}
/**
* 圆形
*@parambitmap
*@return
*/
publicBitmaptoRoundBitmap(Bitmap bitmap) {
//圆形图片宽高
intwidth = bitmap.getWidth();
intheight = bitmap.getHeight();
//正方形的边长
intr =0;
//取最短边做边长
if(width > height) {
r = height;
}else{
r = width;
}
//构建一个bitmap
Bitmap backgroundBmp = Bitmap.createBitmap(width,
height,Bitmap.Config.ARGB_8888);
//new一个Canvas,在backgroundBmp上画图
Canvas canvas =newCanvas(backgroundBmp);
Paint paint =newPaint();
//设置边缘光滑,去掉锯齿
paint.setAntiAlias(true);
//宽高相等,即正方形
RectF rect =newRectF(0,0,r,r);
//通过制定的rect画一个圆角矩形,当圆角X轴方向的半径等于Y轴方向的半径时,
//且都等于r/2时,画出来的圆角矩形就是圆形
canvas.drawRoundRect(rect,r/2,r/2,paint);
//设置当两个图形相交时的模式,SRC_IN为取SRC图形相交的部分,多余的将被去掉
paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//canvas将bitmap画在backgroundBmp上
canvas.drawBitmap(bitmap, null,rect,paint);
//返回已经绘画好的backgroundBmp
returnbackgroundBmp;}
开源框架 Glide 和picaso等 都可以实现