自定义ImageView实现圆形头像

来源:互联网 发布:csgo优化fps 编辑:程序博客网 时间:2024/05/21 07:04
public class MyImageView extends ImageView {    public MyImageView(Context context, AttributeSet attrs) {        super(context, attrs);    }    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);    private Bitmap mBitmap;    private BitmapShader mBitmapShader;    private Matrix mMatrix = new Matrix();    @Override    protected void onDraw(Canvas canvas) {        Bitmap bitmap = getBitmap(getDrawable());        if (bitmap != null) {            int width = getWidth();            int height = getHeight();            int minSize = Math.min(width, height);            float dstWidth = minSize;            float dstHeight = minSize;            if (mBitmapShader == null || !bitmap.equals(mBitmap)) {                mBitmap = bitmap;                mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);            }            if (mBitmapShader != null) {                mMatrix.setScale(dstWidth / bitmap.getWidth(), dstHeight / bitmap.getHeight());                mBitmapShader.setLocalMatrix(mMatrix);            }            mPaint.setShader(mBitmapShader);            float radius = minSize / 2.0f;            canvas.drawCircle(radius, radius, radius, mPaint);        } else {            super.onDraw(canvas);        }    }    private Bitmap getBitmap(Drawable drawable) {        if (drawable instanceof BitmapDrawable) {            return ((BitmapDrawable) drawable).getBitmap();        } else if (drawable instanceof ColorDrawable) {            Rect rect = drawable.getBounds();            int width = rect.right - rect.left;            int height = rect.bottom - rect.top;            int color = ((ColorDrawable) drawable).getColor();            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);            Canvas canvas = new Canvas(bitmap);            canvas.drawARGB(Color.alpha(color), Color.red(color), Color.green(color), Color.blue(color));            return bitmap;        } else {            return null;        }    }}
1 0