Android Learning Note-Draw a circular imageview instance

Source: Internet
Author: User

A lot of apps are now like round avatars, here, simply write a circular imageview~.

Third-party circular ImageView controls:

Roundedimageview

Circleimageview

Implementation code:

Custom ImageView:RoundImageView.java

 PackageCom.jay.demo.imageviewdemo;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.graphics.PaintFlagsDrawFilter;ImportAndroid.graphics.Path;ImportAndroid.graphics.Rect;Importandroid.graphics.Region;ImportAndroid.util.AttributeSet;ImportAndroid.widget.ImageView;/*** Created by Coder-pig on 2015/7/18 0018.*/ Public classRoundimageviewextendsImageView {PrivateBitmap Mbitmap; PrivateRect Mrect =NewRect (); PrivatePaintflagsdrawfilter PDF =NewPaintflagsdrawfilter (0, Paint.anti_alias_flag); PrivatePaint Mpaint =NewPaint (); PrivatePath mpath=NewPath ();  PublicRoundimageview (Context context, AttributeSet attrs) {Super(context, attrs);    Init (); }    //passing in a bitmap object     Public voidSetBitmap (Bitmap Bitmap) { This. Mbitmap =bitmap; }    Private voidinit () {Mpaint.setstyle (Paint.Style.STROKE);        Mpaint.setflags (Paint.anti_alias_flag); Mpaint.setantialias (true);//anti-sawing ruler} @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); if(Mbitmap = =NULL)        {            return; } mrect.set (0,0, GetWidth (), getheight ());        Canvas.save ();        Canvas.setdrawfilter (PDF); Mpath.addcircle (getwidth ()/2, GetWidth ()/2, GetHeight ()/2, Path.Direction.CCW);        Canvas.clippath (MPath, Region.Op.REPLACE); Canvas.drawbitmap (Mbitmap,NULL, Mrect, Mpaint);    Canvas.restore (); }}

Layout Code: Activity_main.xml:

< Com.jay.demo.imageviewdemo.RoundImageView         Android:id = "@+id/img_round"         android:layout_width= "200DP"        android:layout_height= "200DP"         android:layout_margin= "5px"/>

Mainactivity.java:

 PackageCom.jay.demo.imageviewdemo;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity; Public classMainactivityextendsappcompatactivity {PrivateRoundimageview Img_round; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Img_round=(Roundimageview) Findviewbyid (R.id.img_round); Bitmap Bitmap=Bitmapfactory.decoderesource (Getresources (), R.MIPMAP.MEINV);    Img_round.setbitmap (bitmap); }}

Android Learning Note-Draw a circular imageview instance

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.