Objective:
To tell the truth, this period of time busy to revise the graduation thesis, for a long time without touching the code, it is a sin. Today we come to the end of our round imageview, and if there is a new idea, then again. This article is based on the previous three, detailed stamp Android custom view-build round ImageView (a), Android custom view-to create a round ImageView (ii), Android Custom view-build round ImageView (iii).
:
Body:
Actually looked above, everyone should be at a glance, is a lot of applications often see with the white edge of the gradient head imageview. We use today is the second xfermode to deal with the picture, in fact, the code is similar, as long as the basis for simple modification can be. However, just painted round, no rounded corners ... Excuse me, huh?
What are the preparations for the knowledge before we begin? We've talked about linear gradients before, but today we need to use a ball-shaped gradient radialgradient.
Android.graphics.RadialGradient.RadialGradient (float x, float y, float radius, int[] colors, float[] positions, Tilemode Tile
- First, the second parameter represents the center coordinate of the gradient circle
- The third parameter represents a radius
- Fourth, fifth, sixth, same as linear rendering
Drawing such imageview is divided into three steps:
- Draw a circular edge gradient-that is, with a spherical gradient
- Draw a circular picture
- Draw White edges
All rightStep1: Draw a spherical gradient
Create Brush paint paint = new Paint (paint.anti_alias_flag);p Aint.setdither (true); Radialgradient gradient = new Radialgradient (GetWidth ()/2, GetHeight ()/2, getwidth ()/2, new int[] {0xff5d5d5d, 0xff 5D5D5D, 0X00FFFFFF}, new float[] {0.f, 0.9f, 1.0f}, Shader.TileMode.CLAMP);p aint.setshader (gradient);
When you draw the mask mask layer, that is, when the shape is drawn, the gradient is actually very simple, nothing to explain.Step2: Draw a circular pictureThe code is actually the same as in the previous second article.
@Overrideprotected void OnDraw (canvas canvas) {//Remove picture from cache bitmap Bitmap = Mweakbitmap = = null? Null:mWeakBitmap.get (); /If there is no cache or is recycled, redraw if (bitmap = = NULL | | bitmap.isrecycled ()) {//get background drawabledrawable drawable = getdrawable ();//If there is a background map Then draw if (drawable! = null) {//Get drawable length and width int dwidth = Drawable.getintrinsicwidth (); int dheight = Drawable.getintrinsi Cheight (); bitmap = Bitmap.createbitmap (GetWidth (), GetHeight (), config.argb_8888);//create canvas canvases CANVAS1 = new Canvas ( bitmap);//Set picture scaling ratio float scale = 1.0f;scale = Math.max (getwidth () * 1.0f/dwidth, GetHeight () * 1.0f/dheight);//Zoom Picture Dr Awable.setbounds (0, 0, (int) (scale * dwidth), (int.) (Scale * dheight));//Draw DST picture Drawable.draw (CANVAS1);//Draw src picture if (mMa Skbitmap = = NULL | | Mmaskbitmap.isrecycled ()) {Mmaskbitmap = Drawtype ();} Reset Brush Mpaint.reset ();//Do not use filter mpaint.setfilterbitmap (FALSE); Mpaint.setxfermode (Xfermode); Canvas1.drawbitmap ( Mmaskbitmap, 0, 0, mpaint); Paint paint1 = new Paint (paint.anti_alias_flag);p Aint1.setcolor (0xFFFFFFFF);p aint1.setstrokewidth (mgradientborder);p Aint1.setstyle (Paint.Style.STROKE);p Aint1.setxfermode (new Porterduffxfermode (mode.src_in)) canvas1.drawcircle (GetWidth ()/2, GetHeight ()/2, getwidth ()/2-mgradientgap, Paint 1);//Drawable.draw (canvas);//Draw well-handled graphics mpaint.setxfermode (null); Canvas.drawbitmap (bitmap, 0, 0, mpaint);// Cache picture Mweakbitmap = new weakreference<bitmap> (BITMAP);}} if (bitmap! = null) {Mpaint.setxfermode (null); Canvas.drawbitmap (bitmap, 0.0f, 0.0f, Mpaint);}}The only difference here is that white edge processing needs to be done before Canvas.drawbitmap (Bitmap,0,0,mpaint), which is just one more step, to put it bluntly.
Paint paint1 = new Paint (paint.anti_alias_flag);p Aint1.setcolor (0xFFFFFFFF);p aint1.setstrokewidth (Mgradientborder) ;p Aint1.setstyle (Paint.Style.STROKE);p Aint1.setxfermode (New Porterduffxfermode (mode.src_in)); canvas1.drawcircle (GetWidth ()/2, GetHeight ()/2, getwidth ()/2-mgradientgap, paint1);
This paragraph is to deal with the white edge of the code, in fact, there is nothing to say, as for Xfermode do not understand, look back to my previous three blog bar. All right, put all the code on it.
Package Com.beyole.view;import Java.lang.ref.weakreference;import Android.content.context;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.radialgradient;import Android.graphics.shader;import Android.graphics.xfermode;import Android.graphics.drawable.drawable;import Android.util.attributeset;import Android.widget.imageview;public Class Gradientimageview extends ImageView {//white edge size private int mgradientborder = 7;//set white edge with gradient spacing private int mgradientgap = 5;/ /Brush Private Paint mpaint;//uses the caching mechanism to save the processed Bitmap, facilitating gcprivate weakreference<bitmap> mweakbitmap;// The mode of setting Xfermode is dst_inprivate xfermode xfermode = new Porterduffxfermode (mode.dst_in);//Mask layer private Bitmap mmaskbitmap; Public Gradientimageview (Context context) {This (context, null);} Public Gradientimageview (context context, AttributeSet Attrs) {This(context, attrs, 0);} Public Gradientimageview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle);// Initialize Brush mpaint = new Paint (); Mpaint.setantialias (true);} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, HEIGHTMEASURESPEC);//force wide consistency, with a minimum value of quasi int mwidth = Math.min (Getmeasuredwidth (), Getmeasuredheight ()); Setmeasureddimension (Mwidth, mwidth);} @Overrideprotected void OnDraw (canvas canvas) {//Remove picture from cache bitmap Bitmap = Mweakbitmap = = null? Null:mWeakBitmap.get (); /If there is no cache or is recycled, redraw if (bitmap = = NULL | | bitmap.isrecycled ()) {//get background drawabledrawable drawable = getdrawable ();//If there is a background map Then draw if (drawable! = null) {//Get drawable length and width int dwidth = Drawable.getintrinsicwidth (); int dheight = Drawable.getintrinsi Cheight (); bitmap = Bitmap.createbitmap (GetWidth (), GetHeight (), config.argb_8888);//create canvas canvases CANVAS1 = new Canvas ( bitmap);//Set picture scaling ratio float scale = 1.0f;scale = Math.max (getwidth () * 1.0f/dwidth, GetHeight () * 1.0f/dheight);//Zoom picture drawable.setbounds (0, 0, (int) (scale * dwidth), (int) (scales * dheight));//Draw DST graph Slice Drawable.draw (CANVAS1);//Draw src picture if (Mmaskbitmap = = NULL | | mmaskbitmap.isrecycled ()) {Mmaskbitmap = Drawtype ();} Reset Brush Mpaint.reset ();//Do not use filter mpaint.setfilterbitmap (FALSE); Mpaint.setxfermode (Xfermode); Canvas1.drawbitmap ( Mmaskbitmap, 0, 0, mpaint); Paint paint1 = new Paint (paint.anti_alias_flag);p Aint1.setcolor (0xFFFFFFFF);p aint1.setstrokewidth (Mgradientborder) ;p Aint1.setstyle (Paint.Style.STROKE);p Aint1.setxfermode (New Porterduffxfermode (mode.src_in)); canvas1.drawcircle (GetWidth ()/2, GetHeight ()/2, getwidth ()/2-mgradientgap, paint1);//Drawable.draw (canvas);//Draw well-handled graphics MPAINT.SETXFE Rmode (null); Canvas.drawbitmap (bitmap, 0, 0, mpaint);//cache picture Mweakbitmap = new weakreference<bitmap> (bitmap);}} if (bitmap! = null) {Mpaint.setxfermode (null); Canvas.drawbitmap (bitmap, 0.0f, 0.0f, Mpaint);}} /** * Draw shape as src * * @return */private Bitmap drawtype () {Bitmap Bitmap= Bitmap.createbitmap (GetWidth (), GetHeight (), Bitmap.Config.ARGB_8888); Canvas canvas = new canvas (bitmap);//Create brush paint paint = new Paint (paint.anti_alias_flag);p Aint.setdither (true); Radialgradient gradient = new Radialgradient (GetWidth ()/2, GetHeight ()/2, getwidth ()/2, new int[] {0xff5d5d5d, 0xff 5D5D5D, 0X00FFFFFF}, new float[] {0.f, 0.9f, 1.0f}, Shader.TileMode.CLAMP);p aint.setshader (gradient); Canvas.drawcircle (GetWidth ()/2, getwidth ()/2, getwidth ()/2, paint); return bitmap;} Mask and DST memory reclamation in redraw @overridepublic void invalidate () {Mweakbitmap = Null;if (Mmaskbitmap! = null) { Mmaskbitmap.recycle (); mmaskbitmap = null;} Super.invalidate ();} Externally disclosed white edge width public void setgradientborder (int gradientborder) {this.mgradientborder = Gradientborder;} External announcement white edge with gradient spacing public void setgradientgap (int gradientgap) {this.mgradientgap = Gradientgap;}}The above announced two external methods, if you feel ugly when using, re-set the value will see unexpected effect:
Externally disclosed white edge width public void setgradientborder (int gradientborder) {this.mgradientborder = Gradientborder;} External announcement white edge with gradient spacing public void setgradientgap (int gradientgap) {this.mgradientgap = Gradientgap;}
How to use it is simple:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" > <com.beyole.view.gradientimageview android:id= "@+id/id_img" android:layout_width= "100.0dip" android:layout_height= "100.0dip" android:layout_gravity= "Center_ Horizontal " android:src=" @drawable/demo "/></linearlayout>
post-language:
Well, today's code tutorial ends here. Rushed to change the paper, busy stealing to write this blog, I really do not fit to engage in paper writing.
csdn:http://download.csdn.net/detail/smarticeberg/9493659GitHub Address:Https://github.com/xuejiawei/beyole_roundimageviewWelcome to fork or star
Off Topic:
Android Exchange Group: 279031247 (No ads)
Sina Micro-blog: Smarticeberg
Android Custom view-build round ImageView (iv) End of chapter