安卓圆形头像理解

2017-10-13  本文已影响80人  拉贝

为什么最近圆形头像变得流行?

https://www.zhihu.com/question/20956411

圆形头像的实现方式

自定义控件(继承的方式):借鉴文章:Android 两种方式实现圆形头像

-继承ImageView

private void init(Context context, AttributeSet attrs) {
    paint = new Paint();
    // 透明度: 00%=FF(不透明) 100%=00(透明)
    paint.setColor(Color.WHITE);
    // paint.setColor(Color.parseColor("ffffffff"));
    paint.setStyle(Paint.Style.STROKE);
    // 解决图片拉伸后出现锯齿的两种办法: 1.画笔上设置抗锯齿 2.画布上设置抗锯齿
    // http://labs.easymobi.cn/?p=3819
    paint.setFlags(Paint.ANTI_ALIAS_FLAG);
    paint.setAntiAlias(true);
    int clearBits = 0;
    int setBits = Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG;
    pfdf = new PaintFlagsDrawFilter(clearBits, setBits);
    //由于imageview有默认底色,如黑色,设置背景为透明是为了第一次setImageBitmap时不显示圆以外方型的默认背景色
    //但是这样在中兴nubia手机上还会首先显示正方形黑色背景,然后才变圆(解决办法,先裁成圆再setImageBitmap)
    setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
}

@Override
protected void onDraw(Canvas canvas) {
    int width = getWidth();
    int height = getHeight();
    // CCW: CounterClockwise(逆时针)
    // CW: Clockwise(顺时针)
    if (path == null) {
        path = new Path();
        path.addCircle(width / 2f, height / 2f, Math.min(width / 2f, height / 2f), Path.Direction.CCW);
        path.close();
    } //      canvas.drawCircle(width / 2f, height / 2f, Math.min(width / 2f, height / 2f), paint);
    // super.onDraw里面也可能有多个canvas.save
    int saveCount = canvas.save();
    canvas.setDrawFilter(pfdf);
    // Region.Op.REPLACE 是显示第二次的 //      canvas.clipPath(path, Region.Op.REPLACE);
    canvas.clipPath(path, Region.Op.INTERSECT);
    super.onDraw(canvas);
    canvas.restoreToCount(saveCount);
} }

-继承View

private void init(Context context, AttributeSet attrs) {
    try {
        if (android.os.Build.VERSION.SDK_INT >= 11) {
            setLayerType(LAYER_TYPE_SOFTWARE, null);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    // 透明度: 00%=FF(不透明) 100%=00(透明)
    // paint.setColor(Color.parseColor("#ffffffff"));
    // 解决图片拉伸后出现锯齿的两种办法: 1.画笔上设置抗锯齿 2.画布上设置抗锯齿
    paint.setFlags(Paint.ANTI_ALIAS_FLAG);
    paint.setAntiAlias(true);
    int clearBits = 0;
    int setBits = Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG;
    pfdf = new PaintFlagsDrawFilter(clearBits, setBits);
    // PorterDuff.Mode.MULTIPLY只显示重叠部分
    xfermode = new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY);
    matrix = new Matrix();
}
public void setImageResource(int resId) {
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), resId);
    setBitmap(bmp);
}

public void setBitmap(Bitmap bmp) {
    this.bitmap = bmp;

}

public void setScaleType(ScaleType scaleType) {
    this.scaleType = scaleType;
}

private void makeDestBmp(int width, int height) {
    destBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    rectf = new RectF(0, 0, width, height);
    Canvas canvas = new Canvas(destBmp);
    canvas.drawOval(rectf, paint);
}

@Override
protected void onDraw(Canvas canvas) {
    if (bitmap == null) {
        return;
    }
    int width = getWidth();
    int height = getHeight();
    int bmpWidth = bitmap.getWidth();
    int bmpHeight = bitmap.getHeight();
    if (destBmp == null) {
        makeDestBmp(width, height);
    }
    // canvas.save();
    // 设置画布抗锯齿
    canvas.setDrawFilter(pfdf);
    canvas.drawBitmap(destBmp, 0, 0, paint);
    paint.setXfermode(xfermode);
    switch(scaleType) {
    case FIT_XY:
         canvas.drawBitmap(bitmap, null, rectf, paint);
        break;
    case CENTER_CROP:
    default:
        matrix.reset();
        float scale = Math.max((float) width / (float) bmpWidth, (float) height / (float) bmpHeight);
        // 默认绕原点进行缩放 matrix.postScale(scale, scale, 0, 0);
        matrix.postScale(scale, scale, 0, 0);
        matrix.postTranslate((width - bmpWidth * scale) / 2f, (height - bmpHeight * scale) / 2f);
        canvas.drawBitmap(bitmap, matrix, paint);
        break;
    }
    paint.setXfermode(null);
    // canvas.restore();
} }

利用shape来制作圆形头像

使用Shader

mBitmap = BitmapFactory.decodeResource(getResources(),
R.mipmap.beauty);
mBitmapShader = new BitmapShader(mBitm
Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
mPaint = new Paint();
mPaint.setShader(mBitmapShader);
canvas.drawCircle(300, 150, 100, mPaint);

借助三方库 自定义圆形头像CircleImageView的使用和源码分析

借鉴文章:

自定义圆形头像CircleImageView的使用和源码分析

http://blog.csdn.net/zhoubin1992/article/details/47258639

Android 两种方式实现圆形头像

http://blog.csdn.net/chy555chy/article/details/54800086

Android中圆形图的几种实现方式

http://blog.csdn.net/cfy137000/article/details/50896273

为什么最近圆形头像变得流行?

https://www.zhihu.com/question/20956411

自定义view之圆形头像(2种实现方式)

http://blog.csdn.net/picasso_l/article/details/49801685

Android-解析自定义view之圆形头像的各类方案

http://www.jianshu.com/p/a12fc72e960e

总结:不管是自定义控件,通过样式,或者是采用三方库,在实现圆形头像的时候都要考虑其拓展性,API的兼容性,使用是否方便,使用过程中可能会带来哪几个问题。项目中用到的技术一定要有选择的理由,还是那句话,不仅要知其然,还要知其所以然,注意一下使用场景。

上一篇下一篇

猜你喜欢

热点阅读