Android circular Imageview

Source: Internet
Author: User

Android circular Imageview

The circular imageview custom control found on the Internet :'

Package com. wangll. widget; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmap. config; import android. graphics. canvas; import android. graphics. paint; import android. graphics. paintFlagsDrawFilter; import android. graphics. porterDuff. mode; import android. graphics. porterduxfermode; import android. graphics. rect; import android. graphics. rectF; import android. graphics. dra Wable. bitmapDrawable; import android. graphics. drawable. drawable; import android. util. attributeSet; import android. widget. imageView;/*** circular Imageview ** @ author wangll **/public class CircularImage extends ImageView {public CircularImage (Context context) {super (context );} public CircularImage (Context context, AttributeSet attrs) {super (context, attrs);} public CircularImage (Context context, TributeSet attrs, int defStyle) {super (context, attrs, defStyle) ;}@ Override protected void onDraw (Canvas canvas) {Drawable drawable = getDrawable (); if (null! = Drawable) {Bitmap bitmap = (BitmapDrawable) drawable). getBitmap (); if (null! = Bitmap) {Bitmap B = toRoundCorner (bitmap); Paint paint = new Paint (); paint. setAntiAlias (true); paint. setFilterBitmap (true); canvas. drawBitmap (B, new Rect (0, 0, B. getWidth (), B. getHeight (), new Rect (0, 0, this. getWidth (), this. getHeight (), paint) ;}} else {super. onDraw (canvas) ;}} private Bitmap toRoundCorner (Bitmap bitmap) {Bitmap outBitmap = Bitmap. createBitmap (bitmap. getWidth (), bitmap. GetHeight (), Config. ARGB_8888); final Paint paint = new Paint (); paint. setAntiAlias (true); paint. setFilterBitmap (true); paint. setColor (0 xffffffff); final Rect rect = new Rect (0, 0, bitmap. getWidth (), bitmap. getHeight (); final RectF rectF = new RectF (rect); final float roundPX = bitmap. getWidth ()/2 <bitmap. getHeight ()/2? Bitmap. getWidth (): bitmap. getHeight (); Canvas canvas = new Canvas (outBitmap); canvas. drawARGB (0, 0, 0, 0); canvas. setDrawFilter (new PaintFlagsDrawFilter (0, Paint. ANTI_ALIAS_FLAG | Paint. FILTER_BITMAP_FLAG); canvas. drawRoundRect (rectF, roundPX, roundPX, paint); paint. setXfermode (new porterduduxfermode (Mode. SRC_IN); canvas. drawBitmap (bitmap, rect, rect, paint); return outBitmap ;}}

Directly use

 
  

Image effects:

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.