Custom circle control RoundImageView and control roundimageview

Source: Internet
Author: User
Tags getcolor

Custom circle control RoundImageView and control roundimageview

1. Custom circle control RoundImageView

Package com. ronye. customView; import android. content. context; import android. content. res. typedArray; import android. graphics. bitmap; import android. graphics. canvas; import android. graphics. paint; import android. graphics. porterDuff; import android. graphics. porterduxfermode; import android. graphics. rect; import android. graphics. drawable. bitmapDrawable; import android. graphics. drawable. drawable; import android. Graphics. drawable. ninePatchDrawable; import android. util. attributeSet; import android. widget. imageView; import com. ronye. r;/*** Created by $ {zyj} on 2015/8/27. * Description: Custom circular image */public class RoundImageView extends ImageView {private int mBorderThickness = 0; private Context mContext; private int defaultColor = 0 xFFFFFFFF; // if only one of them has a value, draw only one circular border private int mBorderOutsideColor = 0; private int mBord ErInsideColor = 0; // control default length and width: private int defaultWidth = 0; private int defaultHeight = 0; public RoundImageView (Context context) {super (context); mContext = context ;} public topology (Context context, AttributeSet attrs) {super (context, attrs); mContext = context; setCustomAttributes (attrs);} public RoundImageView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, DefStyle); mContext = context; setCustomAttributes (attrs);} private void setCustomAttributes (AttributeSet attrs) {TypedArray a = mContext. obtainStyledAttributes (attrs, R. styleable. roundedimageview); mBorderThickness =. getDimensionPixelSize (R. styleable. roundedimageview_border_thickness, 0); mBorderOutsideColor =. getColor (R. styleable. roundedimageview_border_outside_color, defaultColor); mBorder InsideColor =. getColor (R. styleable. response, defaultColor) ;}@ Override protected void onDraw (Canvas canvas) {Drawable drawable = getDrawable (); if (drawable = null) {return;} if (getWidth () = 0 | getHeight () = 0) {return;} this. measure (0, 0); if (drawable. getClass () = NinePatchDrawable. class) return; Bitmap B = (BitmapDrawable) drawable ). getBitmap (); Bitm Ap bitmap = B. copy (Bitmap. config. ARGB_8888, true); if (defaultWidth = 0) {defaultWidth = getWidth ();} if (defaultHeight = 0) {defaultHeight = getHeight ();} int radius = 0; if (mBorderInsideColor! = DefaultColor & mBorderOutsideColor! = DefaultColor) {// define two borders: the outer circle border and the inner circle border radius = (defaultWidth <defaultHeight? DefaultWidth: defaultHeight)/2-2 * mBorderThickness; // draw the inner circle drawCircleBorder (canvas, radius + mBorderThickness/2, mBorderInsideColor); // draw the outer circle drawCircleBorder (canvas, radius + mBorderThickness/2, mBorderOutsideColor);} else if (mBorderInsideColor! = DefaultColor & mBorderOutsideColor = defaultColor) {// define to draw a border radius = (defaultWidth <defaultHeight? DefaultWidth: defaultHeight)/2-mBorderThickness; border (canvas, radius + mBorderThickness/2, mBorderInsideColor);} else if (mBorderInsideColor = defaultColor & mBorderOutsideColor! = DefaultColor) {// define to draw a border radius = (defaultWidth <defaultHeight? DefaultWidth: defaultHeight)/2-mBorderThickness; drawCircleBorder (canvas, radius + mBorderThickness/2, mBorderOutsideColor);} else {// no border radius = (defaultWidth <defaultHeight? DefaultWidth: defaultHeight)/2;} Bitmap roundBitmap = getCroppedRoundBitmap (bitmap, radius); canvas. drawBitmap (roundBitmap, defaultWidth/2-radius, defaultHeight/2-radius, null );} /*** get the cropped circular image * @ param bmp * @ param radius * @ return */public Bitmap getCroppedRoundBitmap (Bitmap bmp, int radius) {Bitmap scaledSrcBmp; int diameter = radius * 2; // to prevent the width and height from being equal, the circular image is deformed. Therefore, the middle position of the rectangle is intercepted. The largest square image int bmp width = bmp. getWidth (); int bmpHeight = bmp. getHeight (); int squareWidth = 0, squareHeight = 0; int x = 0, y = 0; Bitmap squareBitmap; if (bmpHeight> BMP width) {// The height is higher than the width squareWidth = squareHeight = BMP width; x = 0; y = (bmpHeight-BMP width)/2; // capture the square image squareBitmap = Bitmap. createBitmap (bmp, x, y, squareWidth, squareHeight);} else if (bmpHeight <bmp width) {// larger than squareWid Th = squareHeight = bmpHeight; x = (BMP width-bmpHeight)/2; y = 0; squareBitmap = Bitmap. createBitmap (bmp, x, y, squareWidth, squareHeight);} else {squareBitmap = bmp;} if (squareBitmap. getWidth ()! = Diameter | squareBitmap. getHeight ()! = Diameter) {scaledSrcBmp = Bitmap. createScaledBitmap (squareBitmap, diameter, diameter, true);} else {scaledSrcBmp = squareBitmap;} Bitmap output = Bitmap. createBitmap (scaledSrcBmp. getWidth (), scaledSrcBmp. getHeight (), Bitmap. config. ARGB_8888); Canvas canvas = new Canvas (output); Paint paint = new Paint (); Rect rect = new Rect (0, 0, scaledSrcBmp. getWidth (), scaledSrcBmp. getHeight (); paint. setAntiAlias (true); paint. setFilterBitmap (true); paint. setDither (true); canvas. drawARGB (0, 0, 0, 0); canvas. drawCircle (scaledSrcBmp. getWidth ()/2, scaledSrcBmp. getHeight ()/2, scaledSrcBmp. getWidth ()/2, paint); paint. setXfermode (new porterduxfermode (PorterDuff. mode. SRC_IN); canvas. drawBitmap (scaledSrcBmp, rect, rect, paint); bmp = null; squareBitmap = null; scaledSrcBmp = null; return output ;} /*** edges circle */private void drawCircleBorder (Canvas canvas, int radius, int color) {Paint paint = new Paint ();/* de-sawtooth */paint. setAntiAlias (true); paint. setFilterBitmap (true); paint. setDither (true); paint. setColor (color);/* set the paint style to STROKE: hollow */paint. setStyle (Paint. style. STROKE);/* set the width of the paint box */paint. setStrokeWidth (mBorderThickness); canvas. drawCircle (defaultWidth/2, defaultHeight/2, radius, paint );}}

 

2. Define Your Own Property configuration file: attr. xml

<?xml version="1.0" encoding="utf-8"?> <resources>     <declare-styleable name="roundedimageview">         <attr name="border_thickness" format="dimension" />         <attr name="border_inside_color" format="color" />         <attr name="border_outside_color" format="color"></attr>     </declare-styleable> </resources>

 

3. Use the control activity_main.xml in xml configuration.

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" xmlns: imagecontrol = "http://schemas.android.com/apk/res-auto" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "horizontal"> <! -- When the circular ImageView attribute is not specified, no external circle color is selected by default --> <! -- You must set the image resource to src or setImageResource (res) in the program. The background cannot be set to an image, so that the circular effect cannot be achieved. --> <com. dxd. roundimageview. roundImageView android: layout_width = "100dp" android: layout_height = "100dp" android: src = "@ drawable/img"/>
<! -- Border_outside_color the color of the external circle --> <! -- Border_inside_color color of the internal circle --> <! -- Border_thickness: width of the outer circle and inner circle -->
<Com. dxd. roundimageview. roundImageView android: layout_width = "100dp" android: layout_height = "100dp" android: src = "@ drawable/img" imagecontrol: border_inside_color = "# bc0978" imagecontrol: border_outside_color = "# ba3456" imagecontrol: border_thickness = "1dp"/> </LinearLayout>

 

4. Running result

 

Related Article

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.