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:
[Java]View Plaincopy
- @Override
- protected void OnDraw (canvas canvas) {
- //Super.ondraw (canvas);
- Setscaletype (Scaletype.center_crop);
- drawable drawable = getdrawable ();
- if (null = = drawable) {
- return;
- }
- //Convert drawable to bitmap==> online search
- 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);
- Drawable.setbounds (0, 0, Drawable.getintrinsicwidth (),
- Drawable.getintrinsicheight ());
- Drawable.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);
- //Drawing
- Bitmapshader shader = new Bitmapshader (Bitmap, Tilemode.clamp,
- Tilemode.clamp);
- Paint paint = new paint ();
- Paint.setshader (shader);
- Paint.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!!
Other great article articles
Android KSOAP2 call. NET Webservicejquery Tutorial (8)- Using the reverse Insert method for DOM tree operation Android Learning Note (34) using Alertdialog to create a simple dialog box Android learning Note (33) Gallery View (Gallery) features and usage Android navidgation drawer How to change the list selection in the navigation drawer ...
More about Android development articles
Android round Avatar DIY