Two ways to achieve Circleimageview _canvas

Source: Internet
Author: User
Using Paint.setxfermode

Reference link: From the blog, please click here

To get things Circleimageview, split into two parts: Create a circle bitmap use canvas to create a circular bitmap

Private Bitmap Createroundconerimage (Bitmap source)
    final Paint Paint = new Paint ();
    Paint.setantialias (true);
    Create a Bitmap object of the same size
    Bitmap target = Bitmap.createbitmap (Mwidth, Mheight, config.argb_8888);
    Encapsulated as Canvas object
    Canvas Canvas = new Canvas (target);
    RECTF rect = new RECTF (0, 0, source.getwidth (), Source.getheight ());
    Draw the circle through the canvas into a circular canvas
    canvas.drawroundrect (rect, Mradius, Mradius, paint);
    Sets how the brush is stacked
    paint.setxfermode (new Porterduffxfermode (PorterDuff.Mode.SRC_IN));
    Draw the original picture on the canvas
    canvas.drawbitmap (source, 0, 0, paint);
    Remove Bitmap object return
Draw the generated circular bitmap object

Add in OnDraw (Canvas Canvas):

Canvas.drawbitmap (Createroundconerimage (MSRC), 0, 0, NULL);
Using shader

Reference Links: drawable to bitmap bitmap to Bitmapshader via paint Drawcircle the Drawable object to bitmap by Paint objects on the canvas object

Public Bitmap Drawable2bitmap (drawable drawable) {
    if (drawable = null) {
        retrurn null;
    if (drawable instanceof bitampdrawable) {
        retrun (bitampdrawble) drawable). Getbitmap ();
    int w = drawble.getintrisicwidth ();
    int h = drawble.getintrisicheight ();
    Bitmap Newbitmap = Bitmap.createbitmap (w,h,bitmap.config.argb_8888);
    Canvas Canvas = new Canvas (newbitmap);
    Drawable.setbounds (0,0,w,h);
    Drawable.draw (Canvas);
    return newbitamp;
Associating Bitmapshader with the bitmap to be displayed
Mmatrix.setscale (Scale,scale),//set magnification multiples
mbitmapshader.setlocalmatrix (mmatrix);

Bitmapshader Mbitmapshader = new Bitmapshader (newbitmap,tilemode.clamp,tilemode.clamp);
Mbitmappaint.setshader (Mbitmapshader);
To draw a circle again


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: 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.