Android圆形头像
创建一个CircleView
继承AppCompatImageView
package com.lich.lichdialect.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import androidx.appcompat.widget.AppCompatImageView;
import com.lich.lichdialect.R;
import com.lich.lichdialect.util.ColorUtil;
/**
* Created by lichhowger on 2020/2/4.
*/
public class CircleView extends AppCompatImageView {
private int mSize;
private Paint mPaint;
private Xfermode mPorterDuffXfermode;
private Paint whitePaint;
public CircleView(Context context) {
this(context, null);
}
public CircleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
//创建Paint
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setAntiAlias(true);
//创建图像合成模式
mPorterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
//创建白色的Paint
whitePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
whitePaint.setStrokeWidth(4);
whitePaint.setStyle(Paint.Style.STROKE);
whitePaint.setColor(ColorUtil.getColor(R.color.white));
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//获取width和height宽高
int width = getMeasuredWidth();
int height = getMeasuredHeight();
//取宽高的最小值
mSize = Math.min(width, height);
//设置CircleImageView为等宽高
setMeasuredDimension(mSize, mSize);
}
@Override
protected void onDraw(Canvas canvas) {
//获取sourceBitmap,即通过xml或者java设置进来的图片
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
//获取Bitmap
Bitmap sourceBitmap = ((BitmapDrawable) getDrawable()).getBitmap();
if (sourceBitmap != null) {
//对图片进行缩放,以适应控件的大小
Bitmap bitmap = resizeBitmap(sourceBitmap, getWidth(), getHeight());
//(1)利用PorterDuffXfermode实现
//drawCircleBitmapByXfermode(canvas, bitmap);
//(2)利用BitmapShader实现
drawCircleBitmapByShader(canvas, bitmap);
}
}
private void drawCircleBitmapByShader(Canvas canvas, Bitmap bitmap) {
//创建BitmapShader
BitmapShader shader = new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
mPaint.setShader(shader);
//画出CircleView
canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2, mPaint);
//画白色的圆圈
canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2 - 2, whitePaint);
}
/**
* 缩放Bitmap
* @param sourceBitmap
* @param dstWidth
* @param dstHeight
* @return
*/
private Bitmap resizeBitmap(Bitmap sourceBitmap, int dstWidth, int dstHeight) {
int width = sourceBitmap.getWidth();
int height = sourceBitmap.getHeight();
float widthScale = ((float) dstWidth) / width;
float heightScale = ((float) dstHeight) / height;
//取最大缩放比
float scale = Math.max(widthScale, heightScale);
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
return Bitmap.createBitmap(sourceBitmap, 0, 0, width, height, matrix, true);
}
private void drawCircleBitmapByXfermode(Canvas canvas, Bitmap bitmap) {
final int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
//绘制dst层
canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2, mPaint);
//设置图层混合模式为SRC_IN
mPaint.setXfermode(mPorterDuffXfermode);
//绘制src层
canvas.drawBitmap(bitmap, 0, 0, mPaint);
//画白色的圆圈
canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2 - 2, whitePaint);
canvas.restoreToCount(sc);
}
}