為什么最近圓形頭像變得流行铁瞒?
https://www.zhihu.com/question/20956411
圓形頭像的實(shí)現(xiàn)方式
自定義控件(繼承的方式):借鑒文章:Android 兩種方式實(shí)現(xiàn)圓形頭像
-繼承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); // 解決圖片拉伸后出現(xiàn)鋸齒的兩種辦法: 1.畫筆上設(shè)置抗鋸齒 2.畫布上設(shè)置抗鋸齒 // 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有默認(rèn)底色,如黑色,設(shè)置背景為透明是為了第一次setImageBitmap時不顯示圓以外方型的默認(rèn)背景色 //但是這樣在中興nubia手機(jī)上還會首先顯示正方形黑色背景,然后才變圓(解決辦法,先裁成圓再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")); // 解決圖片拉伸后出現(xiàn)鋸齒的兩種辦法: 1.畫筆上設(shè)置抗鋸齒 2.畫布上設(shè)置抗鋸齒 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(); // 設(shè)置畫布抗鋸齒 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); // 默認(rèn)繞原點(diǎn)進(jìn)行縮放 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 兩種方式實(shí)現(xiàn)圓形頭像
http://blog.csdn.net/chy555chy/article/details/54800086
Android中圓形圖的幾種實(shí)現(xiàn)方式
http://blog.csdn.net/cfy137000/article/details/50896273
為什么最近圓形頭像變得流行妙色?
https://www.zhihu.com/question/20956411
自定義view之圓形頭像(2種實(shí)現(xiàn)方式)
http://blog.csdn.net/picasso_l/article/details/49801685
Android-解析自定義view之圓形頭像的各類方案
http://www.reibang.com/p/a12fc72e960e
總結(jié):不管是自定義控件,通過樣式慧耍,或者是采用三方庫身辨,在實(shí)現(xiàn)圓形頭像的時候都要考慮其拓展性,API的兼容性芍碧,使用是否方便煌珊,使用過程中可能會帶來哪幾個問題。項(xiàng)目中用到的技術(shù)一定要有選擇的理由泌豆,還是那句話定庵,不僅要知其然,還要知其所以然踪危,注意一下使用場景蔬浙。