Android round picture controls such as the following:
The code is as follows:
Roundimageview.java
Package Com.dxd.roundimageview;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.paint;import Android.graphics.porterduff.mode;import Android.graphics.porterduffxfermode;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.util.Log; Import Android.widget.imageview;import com.alan.myimageview.r;/** * Round ImageView, can be set up to two different width and different color of the circular border. * Set the color in the XML layout file by yourself define the attribute configuration parameters specified */public class Roundimageview extends ImageView {private int mborderthickness = 0;private Con Text mcontext;private int defaultcolor = 0xffffffff;//Assuming only one of the values is there, just draw a circular border private int mborderoutsidecolor = 0;private int mborderinsidecolor = 0;//control default long, wide private int defaultwidth = 0;private int defaultheight = 0;public RoundimagevIew (Context context) {super (context); mcontext = context;} Public Roundimageview (context context, AttributeSet Attrs) {Super (context, attrs); mcontext = context; Setcustomattributes (attrs);} Public Roundimageview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); mcontext = cont Ext;setcustomattributes (attrs);} private void Setcustomattributes (AttributeSet attrs) {TypedArray a = Mcontext.obtainstyledattributes (Attrs, R.styleable.roundedimageview); mborderthickness = A.getdimensionpixelsize (r.styleable.roundedimageview_border_ thickness, 0); Mborderoutsidecolor = A.getcolor (R.styleable.roundedimageview_border_outside_color,defaultcolor); Mborderinsidecolor = A.getcolor (R.styleable.roundedimageview_border_inside_color, defaultColor);} @Overrideprotected 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 (); Bitmap 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 draw two borders. Outer border and inner circle border radius = (DefaultWidth < defaultheight? defaultwidth:defaultheight)/2-2 * mborderthickness;//draw inner circle DRA Wcircleborder (canvas, radius + mborderthickness/2,mborderinsidecolor);//Draw Outer circle Drawcircleborder (canvas, Radius + Mborderthickness + MBORDERTHICKNESS/2, mborderoutsidecolor);} else if (mborderinsidecolor! = DefaultColor && Mborderoutsidecolor = = DefaultColor) {//define draw a border radius = (defaultwi DTH < DefaultHeight? Defaultwidth:defaultheight)/2-mborderthickness;drawcircleborder (canvas, Radius + MBORDERTHICKNESS/2, mBorderInside Color);} else if (Mborderinsidecolor = = DefaultColor && mborderoutsidecolor! = DefaultColor) {//Define draw a border radius = (DefaultWidth < defaultheight? Defaultwidth:defaultheight)/2-mborderthickness;drawcircleborder (canvas, Radius + MBORDERTHICKNESS/2, MBorderOutsid EColor);} 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 cropped round picture * @param radius radius */public Bitmap getcroppedroundbitmap (Bitmap bmp, int radius) {Bitmap Scaledsrcbmp;int diameter = radius * 2;//In order to prevent the width of the height of unequal, resulting in a circular picture deformation, so that the rectangle in the middle of the largest square image of the picture int bmpwidth = Bmp.getwidth (); int bmpheight = Bmp.gethe Ight (); int squarewidth = 0, squareheight = 0;int x = 0, y = 0; Bitmap squarebitmap;if (Bmpheight > Bmpwidth) {//tall to wide squarewidth = Squareheight = Bmpwidth;x = 0;y = (BMPHEIGHT-BMPW Idth)/2;//intercept square picture Squarebitmap = Bitmap.createbitmap (bmp, X, Y, Squarewidth, squareheight);} else if (Bmpheight < bmpwidth) {//large to high squarewidth = Squareheight = Bmpheight;x = (bmpwidth-bmpheight)/2;y = 0;squa Rebitmap = 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 (), config.argb_8888); Canvas canvas = new canvas (output); Paint paint = new paint (); Rect rect = new Rect (0, 0, scaledsrcbmp.getwidth (), Scaledsrcbmp.getheight ());p Aint.setantialias (true); Paint.setfilterbitmap (True);p Aint.setdither (true); Canvas.drawargb (0, 0, 0, 0); Canvas.drawcircle ( Scaledsrcbmp.getwidth ()/2,scaledsrcbmp.getheight ()/2, Scaledsrcbmp.getwidth ()/2,paint);p Aint.setxfermode (new Porterduffxfermode (mode.src_in)); Canvas.drawbitmap (scaledsrcbmp, rect, rect, paint); bmp = Null;squarebitmap = null; Scaledsrcbmp = Null;return output;} /** * Edge Draw circle */private void Drawcircleborder (canvas canvas, int radius, int color) {Paint paint = new paint ();/* antialiasing */paint . Setantialias (True);p Aint.setfilterbitmap (True);p Aint.setdither (True);p aint.setcolor (color);/* Set Paint style For stroke: Hollow */Paint.setstyle (Paint.Style.STROKE);/* Sets the outer frame width of the Paint */paint.setstrokewidth (mborderthickness); Canvas.drawcircle ( DEFAULTWIDTH/2, DEFAULTHEIGHT/2, radius, paint);}}
1. Define your own property profile: 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>
2. Using controls in XML configuration: Activity_main.xml
<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 "> <!--not specified round ImageView property, the default is no outer circle color-<!--need to customize the picture resource to SRC. or in the program Setimageresource (RES) cannot be set background as a picture, so it does not achieve a round effect--<com.dxd.roundimageview.roundimageview Android : layout_width= "100DP" android:layout_height= "100DP" android:src= "@drawable/img"/> <!--bord Er_outside_color the outer circle color-<!--border_inside_color inner circle Color-<!--border_thickness outer and inner circle width-- <com.dxd.roundimageview.roundimageview android:layout_width= "100DP" android:layout_height= "100DP" android:src= "@drawable/img" imagecontrol:border_inside_color= "#bc0978" Imagecontrol:border_outside_colo R= "#ba3456 "imagecontrol:border_thickness=" 1DP "/></linearlayout>
3, there is no relevant code in the main activity. Loading the layout file directly is possible.
Android Round Pictures--Define your own controls