自定义CircleImageView实现圆角矩形头像
importandroid.content.Context;
importandroid.content.res.TypedArray;
importandroid.graphics.Bitmap;
importandroid.graphics.Canvas;
importandroid.graphics.Matrix;
importandroid.graphics.Paint;
importandroid.graphics.PorterDuff;
importandroid.graphics.PorterDuffXfermode;
importandroid.graphics.RectF;
importandroid.graphics.SweepGradient;
importandroid.graphics.drawable.BitmapDrawable;
importandroid.graphics.drawable.Drawable;
importandroid.support.v7.widget.AppCompatImageView;
importandroid.util.AttributeSet;
importniuniu.com.mylibrary.R;
/**
* Created by LiangMinHua 2019/09/13
*/
publicclassCircleImageViewextendsAppCompatImageView{
privateintmWidth;
privateintmHeight;
privateintmRadius;
privateintmShapeType;
privateintmBorderWidth;
privateintmBorderColor;
privateint[]mColors;//渐变色边框数组
publicvoidsetColors(int[]colors){
//把传入来的色组转变为一组对称色组,解决渐变过渡问题
if(colors!=null){
this.mColors=newint[colors.length*2];
for(inti=0;i<colors.length;i++){
mColors[i]=colors[i];
}
for(inti=colors.length;i<colors.length*2;i++){
mColors[i]=colors[colors.length*2-i-1];
}
invalidate();
}
}
publicintgetBorderWidth(){
returnmBorderWidth;
}
publicvoidsetBorderWidth(intborderWidth){
this.mBorderWidth=borderWidth;
invalidate();
}
publicCircleImageView(Contextcontext){
super(context);
init(context,null);
}
publicCircleImageView(Contextcontext,AttributeSetattrs){
super(context,attrs);
init(context,attrs);
}
publicCircleImageView(Contextcontext,AttributeSetattrs,intdefStyleAttr){
super(context,attrs,defStyleAttr);
init(context,attrs);
}
privatevoidinit(Contextcontext,AttributeSetattrs){
//初始化默认值
mRadius=16;
mShapeType=1;
mBorderWidth=0;
mBorderColor=getResources().getColor(R.color.red);
// 获取控件的属性值
if(attrs!=null){
TypedArrayarray=context.obtainStyledAttributes(attrs,R.styleable.CircleImageView);
mRadius=array.getDimensionPixelSize(R.styleable.CircleImageView_radius,mRadius);
mShapeType=array.getInteger(R.styleable.CircleImageView_shape_type,mShapeType);
mBorderWidth=array.getDimensionPixelOffset(R.styleable.CircleImageView_border_width,mBorderWidth);
mBorderColor=array.getColor(R.styleable.CircleImageView_border_color,mBorderColor);
array.recycle();
}
setClickable(true);
setDrawingCacheEnabled(true);
setWillNotDraw(false);
}
@Override
protectedvoidonDraw(Canvascanvas){
// super.onDraw(canvas);
// 获取当前控件的 drawable
Drawabledrawable=getDrawable();
if(drawable==null){
return;
}
// 这里 get 回来的宽度和高度是当前控件相对应的宽度和高度(在 xml 设置)
if(getWidth()==0||getHeight()==0){
return;
}
// 获取 bitmap,即传入 imageview 的 bitmap
Bitmapbitmap=((BitmapDrawable)drawable).getBitmap();
drawDrawable(canvas,bitmap);
if(mColors!=null){
drawMultiColorsBorder(canvas);
}else{
drawBorder(canvas);
}
}
privatevoiddrawDrawable(Canvascanvas,Bitmapbitmap){
// 画笔
Paintpaint=newPaint();
// 颜色设置
paint.setColor(0xffffffff);
// 抗锯齿
paint.setAntiAlias(true);
//Paint 的 Xfermode,PorterDuff.Mode.SRC_IN 取两层图像的交集部门, 只显示上层图像。
PorterDuffXfermodexfermode=newPorterDuffXfermode(PorterDuff.Mode.SRC_IN);
// 标志
intsaveFlags=Canvas.MATRIX_SAVE_FLAG
|Canvas.CLIP_SAVE_FLAG
|Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
|Canvas.FULL_COLOR_LAYER_SAVE_FLAG
|Canvas.CLIP_TO_LAYER_SAVE_FLAG;
canvas.saveLayer(0,0,mWidth,mHeight,null,saveFlags);
if(mShapeType==0){
// 画遮罩,画出来就是一个和空间大小相匹配的圆
canvas.drawCircle(mWidth/2,mHeight/2,mWidth/2,paint);
}else{
// 当ShapeType = 1 时 图片为圆角矩形
RectFrectf=newRectF(0,0,getWidth(),getHeight());
canvas.drawRoundRect(rectf,mRadius,mRadius,paint);
}
paint.setXfermode(xfermode);
// 空间的大小 / bitmap 的大小 = bitmap 缩放的倍数
floatscaleWidth=((float)getWidth())/bitmap.getWidth();
floatscaleHeight=((float)getHeight())/bitmap.getHeight();
Matrixmatrix=newMatrix();
matrix.postScale(scaleWidth,scaleHeight);
//bitmap 缩放
bitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
//draw 上去
canvas.drawBitmap(bitmap,0,0,paint);
canvas.restore();
}
privatevoiddrawBorder(Canvascanvas){
if(mBorderWidth>0){
Paintpaint=newPaint();
paint.setStrokeWidth(mBorderWidth);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(mBorderColor);
paint.setAntiAlias(true);
if(mShapeType==0){
canvas.drawCircle(mWidth/2,mHeight/2,mWidth/2,paint);
}else{
// 当ShapeType = 1 时 图片为圆角矩形
RectFrectf=newRectF(0,0,getWidth(),getHeight());
canvas.drawRoundRect(rectf,mRadius,mRadius,paint);
}
}
}
privatevoiddrawMultiColorsBorder(Canvascanvas){
if(mBorderWidth>0){
SweepGradientgradient=newSweepGradient(mWidth/2,mHeight/2,mColors,null);
Paintpaint=newPaint(Paint.ANTI_ALIAS_FLAG);
paint.setShader(gradient);
paint.setStrokeWidth(mBorderWidth);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(mBorderColor);
paint.setAntiAlias(true);
if(mShapeType==0){
canvas.drawCircle(mWidth/2,mHeight/2,mWidth/2,paint);
}else{
// 当ShapeType = 1 时 图片为圆角矩形
RectFrectf=newRectF(0,0,getWidth(),getHeight());
canvas.drawRoundRect(rectf,mRadius,mRadius,paint);
}
}
}
@Override
protectedvoidonSizeChanged(intw,inth,intoldw,intoldh){
super.onSizeChanged(w,h,oldw,oldh);
mWidth=w;
mHeight=h;
}
}
在attrs添加
<declare-styleablename="CircleImageView">
<attrname="radius"format="dimension"/>
<attrname="border_width"format="dimension"/>
<attrname="border_color"format="color"/>
<attrname="shape_type"format="enum">
<enumname="round"value="0"/>
<enumname="rectangle"value="1"/>
</attr>
</declare-styleable>