20 lines of Android code write a circleimageview_android

Source: Internet
Author: User

A reference to get a round head, many people immediately think of using Circleiamgeview, but in fact, it is not difficult to write their own part of the 20 lines of code, mainly used to Poterduffxfermode to set the intersection of two layers of the display mode

First write a control that inherits from ImageView

public class Circleimageview extends ImageView

Then create the construction method

Public Circleimageview (context, AttributeSet attrs) {
  Super (context, attrs);
 }

Then rewrite the OnDraw method

@Override protected void OnDraw (Canvas Canvas) {//Get the width of the picture int width=getwidth ();
  Get the height of the picture int height=getheight ();

  Short One-second as radius int radius=height>width?width/2:height/2;
  Redefining a canvas, this step is critical Paint mpaint = new Paint ();
  Anti-aliasing Mpaint.setantialias (true);
  Bitmap Bitmap = Bitmap.createbitmap (Width,height, Bitmap.Config.ARGB_8888);
  Canvas Bitmapcanvas = new Canvas (bitmap);

  Super.ondraw (Bitmapcanvas);
  Round box Bitmap CB = bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888);
  Canvas Ccanv = new Canvas (CB);

  Draw a ccanv.drawcircle in the middle of the control (WIDTH/2, HEIGHT/2, radius, mpaint);
  Canvas.drawbitmap (Bitmap, 0.0f, 0.0f, Mpaint);
  DST is a mpaint.setxfermode graphic (new Porterduffxfermode (PorterDuff.Mode.DST_IN));

  Be sure to use the previous canvas, or it will appear the corner is black Bitmapcanvas.drawbitmap (CB, 0.0f, 0.0f, Mpaint);
  Add a border to the graphic Paint Paint =new Paint ();
  Paint.setantialias (TRUE);
  Paint.setstyle (Paint.Style.STROKE);
  Paint.setstrokewidth (5); Paint.setcolor (color.bLACK);

 Canvas.drawcircle (WIDTH/2, HEIGHT/2, radius, paint);

 }

A simple Circleimageview is done, you can also make the border into a property and configure the appropriate method, so that users more convenient to use

It's exactly the same as ImageView.

<com.example.jkgeekjk.roadtodevelop3.circleimageview
  android:layout_width= "Match_parent"
  android: src= "@drawable/avastar"
  android:layout_height= "Match_parent"/>

Effect Chart:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.