Round Head DIY
Most apps now use round avatars, and there's a lot of open source online, but have you ever considered a DIY round avatar? The following is a self-realization, first look at the demo show
The first step: explanation of the principle (the picture is very ugly, the principle is very true)
1, draw the frame round, you need to use a custom color to draw a circle, 5dp longer than the radius of the picture
2. After the custom circle picture is finished, we need to convert the picture of the circle head to the top, covering it from the middle to get the piece
3, the center of the image, the radius is set to: R=min (width,height), the center is set to: CX=WIDTH/2,CY=HEIGHT/2, so that you can start to intercept the circular picture
The second step, the code implementation
1, first inherit ImageView
2. Rewrite the OnDraw (canvas canvas) method
3, set the image scaling type Setscaletype (Scaletype.center_crop); Intermediate intercept
The specific code is as follows:
@Overrideprotected void OnDraw (canvas canvas) {//Super.ondraw (canvas); Setscaletype (scaletype.center_crop);D rawable drawable = Getdrawable (); if (null = = drawable) {return;} Convert drawable to bitmap==> online bitmap bitmap = Bitmap.createbitmap (Drawable.getintrinsicwidth (), Drawable.getintrinsicheight (), drawable.getopacity ()! = Pixelformat.opaque? Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565); Canvas Srccanvas = new Canvas (bitmap);d rawable.setbounds (0, 0, drawable.getintrinsicwidth (), Drawable.getintrinsicheight ());d Rawable.draw (Srccanvas), float cx = getwidth ()/2;float cy = getheight ()/2;float radius = Math.min (GetWidth (), getheight ())/2; Paint Borderpaint = new paint (); Borderpaint.setantialias (true); Borderpaint.setcolor (color.green); canvas.drawcircle (CX, CY, RADIUS, borderpaint);//Draw Bitmapshader shader = new Bitmapshader (bitmap, Tilemode.clamp,tilemode.clamp); Paint paint = new paint ();p aint.setshader (shader);p Aint.setantialias (true); Canvas.drawcircle (CX, CY, radius-5, paint) ;}
Code is just a simple demo, of course, can be made into general Circleimageview, need to according to their own needs diy!!
Android round Avatar DIY