Android custom rounded corner ImageView

Source: Internet
Author: User

Android custom rounded corner ImageView

We often see some apps that can display rounded corner images, such as qq contact icons. One way to achieve rounded corner images is to directly use the rounded corner image resources. Of course, if there is no rounded corner image resources, we can also implement it through a program. The following describes how to customize the ImageView of the rounded corner:

Package com. yulongfei. imageview; import android. content. context; import android. content. res. typedArray; import android. graphics. bitmap; import android. graphics. bitmap. config; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. path; import android. graphics. porterDuff; import android. graphics. porterduxfermode; import android. graphics. rectF; imp Ort android. util. attributeSet; import android. widget. imageView; public class RoundAngleImageView extends ImageView {private int roundWidth = 13; private int roundHeight = 13; public RoundAngleImageView (Context context) {super (context); init (context, null );} public RoundAngleImageView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); init (context, attrs);} public RoundAng LeImageView (Context context, AttributeSet attrs) {super (context, attrs); init (context, attrs);} private void init (Context context, AttributeSet attrs) {if (attrs! = Null) {TypedArray a = context. obtainStyledAttributes (attrs, R. styleable. roundAngleImageView); roundWidth =. getDimensionPixelSize (R. styleable. roundAngleImageView_roundWidth, roundWidth); roundHeight =. getDimensionPixelSize (R. styleable. roundAngleImageView_roundHeight, roundHeight);. recycle ();} else {float density = context. getResources (). getDisplayMetrics (). density; roundWidth = (int) (roundWidth * density); roundHeight = (int) (roundHeight * density);}/** override draw () * // @ Overridepublic void draw (Canvas canvas) {// instantiate a bitmapBitmap of the same size as ImageView. bitmap = Bitmap. createBitmap (getWidth (), getHeight (), Config. ARGB_8888); // instantiate a canvas. The memory of this canvas is bitmapCanvas canvas2 = new Canvas (bitmap); if (bitmap. isRecycled () {bitmap = Bitmap. createBitmap (getWidth (), getHeight (), Config. ARGB_8888); canvas2 = new Canvas (bitmap);} // draw the imageView to canvas2 by yourself. This causes the bitmap to store imageViewsuper. draw (canvas2); // draw a rounded rectangle using canvas, which modifies the bitmap data drawRoundAngle (canvas2); // draws the cropped bitmap to the current canvas of the system, in this way, the cropped imageview can be displayed on the screen. Paint = new paint (); Paint. setXfermode (null); canvas. drawBitmap (bitmap, 0, 0, paint); bitmap. recycle ();} public void setRoundWidth (int roundWidth, int roundHeight) {this. roundWidth = roundWidth; this. roundHeight = roundHeight;} private void drawRoundAngle (Canvas canvas) {Paint maskPaint = new Paint (); maskPaint. setAntiAlias (true); maskPaint. setXfermode (new porterduxfermode (PorterDuff. mode. CLEAR); Path maskPath = new Path (); maskPath. addRoundRect (new RectF (0.0F, 0.0F, getWidth (), getHeight (), roundWidth, roundHeight, Path. direction. CW); // This is the filling mode, which is critical to maskPath. setFillType (Path. fillType. INVERSE_WINDING); canvas. drawPath (maskPath, maskPaint );}}

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.