public class RoundImageView extends AppCompatImageView {
private Paint imagePaint = new Paint(); // 图片画笔
private Paint borderPaint = new Paint(); // 边框画笔
Matrix shaderMatrix = new Matrix();
private Bitmap mBitmap;
private BitmapShader bitmapShader;
private float radius; // 圆形图片半径
private int borderWidth; // 边框宽度
private int borderColor; // 边框颜色
private int defaultborderColor = Color.WHITE; // 默认边框颜色
private int defaultborderWidth = 10; // 默认边框宽度
public RoundImageView(Context context) {
super(context);
}
public RoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
initDate(context, attrs);
}
private void initDate(Context context, AttributeSet attrs) {
TypedArray array = null;
try{
array = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
borderColor = array.getColor(R.styleable.RoundImageView_border_color, defaultborderColor);
borderWidth = array.getDimensionPixelOffset(R.styleable.RoundImageView_border_width, defaultborderWidth);
} catch (Exception e){
e.printStackTrace();
} finally {
if (null != array) {
array.recycle();
}
}
}
@Override
public void setImageResource(int resId) {
// TODO Auto-generated method stub
super.setImageResource(resId);
invalidate();
}
@Override
public void setImageBitmap(Bitmap bm) {
// TODO Auto-generated method stub
super.setImageBitmap(bm);
invalidate();
}
/**
* 设置边框宽度
* @param borderWidth
*/
public void setBorderWidth(int borderWidth) {
this.borderWidth = borderWidth;
invalidate();
}
/**
* 设置边框颜色
* @param borderColor
*/
public void setBorderColor(int borderColor) {
this.borderColor = borderColor;
invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
// 设置圆形的半径
radius = size / 2;
setMeasuredDimension(size, size);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
update();
canvas.drawCircle(radius, radius, radius, imagePaint);
if (borderWidth > 0) {
canvas.drawCircle(radius, radius, radius - (borderWidth / 2), borderPaint);
}
}
/**
* 初始化画笔
*/
private void update() {
if (null == getDrawable()) {
return;
}
BitmapDrawable bitmapDrawable = (BitmapDrawable) getDrawable();
mBitmap = bitmapDrawable.getBitmap();
int bitmapwidth = mBitmap.getWidth();
int bitmapheight = mBitmap.getHeight();
shaderMatrix.set(null);
// 获取图片和view的缩放比
float scale = (radius * 2.0f) / Math.min(bitmapwidth, bitmapheight);
// 设置缩放比
shaderMatrix.setScale(scale, scale);
float dx = 0;
float dy = 0;
// 若果图片的宽大于高,获取要绘画图片的开始的X轴坐标,
if (bitmapwidth > bitmapheight) {
dx = (radius * 2 - (bitmapwidth * scale)) * 0.5f;
} else { // 若果图片的高大于宽,获取要绘画图片的开始的Y轴坐标,
dy = (radius * 2 - (bitmapheight * scale)) * 0.5f;
}
shaderMatrix.postTranslate(dx, dy);
bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
bitmapShader.setLocalMatrix(shaderMatrix);
imagePaint.setAntiAlias(true);
imagePaint.setDither(true);
imagePaint.setShader(bitmapShader);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setAntiAlias(true);
borderPaint.setDither(true);
borderPaint.setColor(borderColor);
borderPaint.setStrokeWidth(borderWidth);
}
}
values文件夹下创建attrs.xml文件,添加自定义属性。
<declare-styleable name="RoundImageView">
<attr name="border_color" format="color" />
<attr name="border_width" format="dimension" />
</declare-styleable>
XML布局
<com.zhang.mytestapplication.RoundImageView
android:id="@+id/round_imageview"
android:layout_width="180dp"
android:layout_height="180dp"
app:border_width="6dp"
app:border_color="@color/colorAccent"
android:src="@mipmap/image"/>