Android round Avatar DIY

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.