Objective:
The first time to write an article, there may be some local text is not clear, forgive me.
The inspiration for doing this comes from a previous company issued a holiday notice on the seal, there is a similar to the idea of a circular seal view. Just as there is no work today, try to achieve it.
Body:
First look at the effect:
The realization of this view, or relatively simple, one is the ring, the second is the text fan arrangement, three is the middle of the circular picture.
The specific code below:
Public classStampviewextendsView {Private Static Final intText_size = 32;//Text Size Private Static Final intOutside_ring_width = 10;//Ring Width Private Static Final floatText_angle = 120f;//the angle of the text sector arrangement Private Static Final intRADIUS = 180;//Seal RADIUS Private Static Final intSPACING = 36;//picture and text spacing Private Static Final floatStamp_rotate = 15f;//Seal Rotation Angle Private Static Final intText_color = 0xff000000;//text color Private Static Final intCircle_color = 0xffff0000;//Ring Color PrivateString MText; PrivateBitmap Source; Private FinalPaint Mtextpaint =NewPaint (); Private FinalPaint Mcirclepaint =NewPaint (); PublicStampview (Context context) {Super(context); Init (); } PublicStampview (Context context, AttributeSet attrs) {Super(context, attrs); Init (); } PublicStampview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); Init (); } Private voidinit () {mtextpaint.setcolor (text_color); Mtextpaint.settextalign (Paint.Align.CENTER); Mcirclepaint.setcolor (Circle_color); Mcirclepaint.setstyle (Paint.Style.STROKE); Mcirclepaint.setantialias (true); SOURCE=Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher); } @Overrideprotected voidOnDraw (canvas canvas) {//Get Wide height intwidth = This. getwidth (); intHeight = This. GetHeight (); Mtextpaint.settextsize (text_size); floattexty = HEIGHT/2-RADIUS + outside_ring_width +text_size; MText= MText! =NULL? MText: ""; //to split the text into a character array Char[] CHS =Mtext.tochararray (); //Draw a circlemcirclepaint.setstrokewidth (outside_ring_width); Canvas.drawcircle (Width/2, HEIGHT/2, RADIUS-OUTSIDE_RING_WIDTH/2, Mcirclepaint); Canvas.save (); Canvas.rotate (stamp_rotate, Width/2, HEIGHT/2); //radius of the middle circle bitmap intRadius = radius-outside_ring_width-text_size-SPACING; Bitmap Image= Createcircleimage (source, RADIUS * 2); Canvas.drawbitmap (image, (width-Image.getwidth ())/2, (Height-image.getheight ())/2, Mcirclepaint); Image.recycle (); Canvas.rotate (-TEXT_ANGLE/2, WIDTH/2, HEIGHT/2); //the angle interval between each text floatSpaceangle = Text_angle/(chs.length-1); for(inti = 0; i < chs.length; i++) {String s=string.valueof (Chs[i]); Canvas.drawtext (s, Width/2, Texty, Mtextpaint); Canvas.rotate (spaceangle, Width/2, HEIGHT/2); } canvas.restore (); } /*** Create a circular bitmap *@paramSource Original picture bitmap *@paramdiameter diameter of circular bitmap *@return */ PrivateBitmap Createcircleimage (Bitmap source,intdiameter) { FinalPaint paint =NewPaint (); Paint.setantialias (true); Paint.setfilterbitmap (true); intwidth =source.getwidth (); intHeight =source.getheight (); Bitmap Clipbitmap; if(Width >height) { intx = (width-height)/2; inty = 0; Clipbitmap=Bitmap.createbitmap (source, x, y, height, height); } Else if(Width <height) { intx = 0; inty = (height-width)/2; Clipbitmap=Bitmap.createbitmap (source, x, y, width, width); } Else{Clipbitmap=source; } Bitmap Scaledbitmap= Bitmap.createscaledbitmap (clipbitmap, diameter, diameter,true); Bitmap Outputbitmap=bitmap.createbitmap (diameter, diameter, config.argb_8888); Canvas Canvas=NewCanvas (OUTPUTBITMAP); Canvas.drawcircle (Diameter/2, DIAMETER/2, DIAMETER/2, paint); Paint.setxfermode (NewPorterduffxfermode (PorterDuff.Mode.SRC_IN)); Canvas.drawbitmap (Scaledbitmap,0, 0, paint);//source.recycle ();clipbitmap.recycle (); Scaledbitmap.recycle (); returnOutputbitmap; } Public voidSetText (String text) {MText=text; } PublicString GetText () {returnMText; } Public voidSetTextColor (intTextColor) {Mtextpaint.setcolor (TextColor); } Public voidSetcirclecolor (intCirclecolor) {Mcirclepaint.setcolor (Circlecolor); } Public voidSetBitmap (intID) {Source=Bitmapfactory.decoderesource (Getresources (), id); }}
Android Custom view-round Seal