In last year's advertising machine project, the UI designed a more gorgeous UI, but for me, I tried to view the overlay and set the margin implementation, although barely achievable, but get click focus is a lot of problems;
As follows:
Finally, there is only another way to do it. I have customized the ViewGroup and customized each button
Source:http://download.csdn.net/detail/hcb1230/6479979
Here's how I implemented it:
1.specailbutton.java
public class Specailbutton extends TextView implements View.onclicklistener {private static final String TAG = "Specai Lbutton "; public static final int text_align_left = 0x00000001; public static final int text_align_right = 0x00000010; public static final int text_align_center_vertical = 0x00000100; public static final int text_align_center_horizontal = 0x00001000; public static final int text_align_top = 0x00010000; public static final int text_align_bottom = 0x00100000; /** Control Brush */private paint paint; /** the orientation of the text */private int textAlign; /** the color of the text */private int textcolor; The width of the/** control, */private int viewwidth; The height of the/** control, */private int viewheight; /** text Axis x coordinate */private float Textcenterx; /** text baseline line y coordinate */private float Textbaseliney; private String text; Private FontMetrics FM; Private Context Mcontext; Private Boolean checked = false; Public SpecAilbutton (Context context) {super (context); Mcontext = context; Init (); } public Specailbutton (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); Mcontext = context; Init (); } public Specailbutton (context context, AttributeSet Attrs) {Super (context, attrs); Mcontext = context; Init (); }/** * Variable initialization */private void init () {Setonclicklistener (this); Text = GetText (). toString (); SetText (""); Paint = new paint (); Paint.settextsize (22); Paint.setantialias (TRUE); Paint.settextalign (Align.center); Text centered by default textAlign = Text_align_center_horizontal | text_align_center_vertical; The default text color is black textcolor = Color.Black; } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {int wMode = MEASURESPEC.G Etmode (WIDTHMEASURESPEC); int wsize = MeasUrespec.getsize (WIDTHMEASURESPEC); int hmode = Measurespec.getmode (Heightmeasurespec); int hsize = measurespec.getsize (Heightmeasurespec); Setmeasureddimension (Wsize, hsize); LOG.I (TAG, "onmeasure ()--wmode=" + WMode + ", wsize=" + wsize + ", hmode=" + hmode+ ", hsize=" + hsize "; Super.onmeasure (Widthmeasurespec, Heightmeasurespec); } @Override protected void OnLayout (Boolean changed, int left, int top, int. right, int bottom) {L OG.I (TAG, "onlayout"); Viewwidth = Right-left; Viewheight = Bottom-top; Super.onlayout (changed, left, top, right, bottom); } @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas); Draw control content Settextlocation (text); Canvas.drawtext (text, Textcenterx, Textbaseliney, paint); /** * Position text Draw location */private void Settextlocation (String text) {//Paint.settextsize (textSize); Paint.setcolor (TextColor); fm = Paint.getfontmetrics (); The width of the text float textWidth = paint.measuretext (text); float Textcenterverticalbaseliney = VIEWHEIGHT/2-fm.descent + (fm.descent-fm.ascent)/2; Switch (textAlign) {Case Text_align_center_horizontal | Text_align_center_vertical:textcenterx = (float) viewwidth/2; Textbaseliney = Textcenterverticalbaseliney; Break Case Text_align_left | Text_align_center_vertical:textcenterx = TEXTWIDTH/2; Textbaseliney = Textcenterverticalbaseliney; Break Case Text_align_right | Text_align_center_vertical:textcenterx = VIEWWIDTH-TEXTWIDTH/2; Textbaseliney = Textcenterverticalbaseliney; Break Case Text_align_bottom | Text_align_center_horizontal:textcenterx = VIEWWIDTH/2; Textbaseliney = Viewheight-fm.bottom; Break Case Text_align_top | Text_align_center_horizoNtal:textcenterx = VIEWWIDTH/2; Textbaseliney =-fm.ascent; Break Case Text_align_top | Text_align_left:textcenterx = TEXTWIDTH/2; Textbaseliney =-fm.ascent; Break Case Text_align_bottom | Text_align_left:textcenterx = TEXTWIDTH/2; Textbaseliney = Viewheight-fm.bottom; Break Case Text_align_top | Text_align_right:textcenterx = VIEWWIDTH-TEXTWIDTH/2; Textbaseliney =-fm.ascent; Break Case Text_align_bottom | Text_align_right:textcenterx = VIEWWIDTH-TEXTWIDTH/2; Textbaseliney = Viewheight-fm.bottom; Break }} public interface Onclicklistener {void OnClick (View V, boolean checked); } private Onclicklistener Mlistener; public void Setonclicklistener (Onclicklistener listener) {Mlistener = listener; } @Override public void onClick (View v) {checked =!checked; Setbackgroundresource (checked 0:r.drawable.logo); if (Mlistener! = null) {Mlistener.onclick (V, checked); }} public String gettextstring () {return text; }}
2. The outside parent control:
public class Specailview extends ViewGroup {private static final String TAG = "Specailview"; private static final int radiu_count = 8; private static final int PADDING = 10; private int Childradius; private int childwidth; private int childheight; private int mchildcount; private int CenterX, centery; Public Specailview (Context context) {super (context); LOG.I (TAG, "Specailview ()"); } public Specailview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); LOG.I (TAG, "Specailview (,,)"); } public Specailview (context context, AttributeSet Attrs) {Super (context, attrs); LOG.I (TAG, "Specailview (,)"); } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {int wMode = MEASURESPEC.G Etmode (WIDTHMEASURESPEC); int wsize = measurespec.getsize (Widthmeasurespec); int hmode = Measurespec.getmode (Heightmeasurespec); INT Hsize = measurespec.getsize (Heightmeasurespec); Setmeasureddimension (Wsize, hsize); CenterX = WSIZE/2; CenterY = HSIZE/2; Childradius = (wsize-padding * 2)/radiu_count; Childwidth = Childradius * 2; Childheight = (int) (Childradius * MATH.SQRT (3)/2) * *; Final int count = Getchildcount (); for (int index = 0, index < count; index++) {View child = Getchildat (index); Measure Child.measure (Childwidth, childheight); } if (Mchildcount! = count) {Mchildcount = count; }//if (Mchildcount > Centers.size ()) {Computerpoint (CenterX, CenterY, childheight);/} LOG.I (TAG, "onmeasure ()--childwidth=" +childwidth+ ", childheight=" +childheight "); LOG.I (TAG, "onmeasure ()--wmode=" + WMode + ", wsize=" + wsize + ", hmode=" + hmode+ ", hsize=" + hsize "; Super.onmeasure (Widthmeasurespec, Heightmeasurespec); } @Override Protected void OnLayout (Boolean changed, int left, int top, int. right, int bottom) {final int count = Getchildcount (); int Childleft, childtop; for (int i = 0; i < count; i++) {View child = Getchildat (i); Childleft = (int) (Centers.get (i). X-childradius); Childtop = (int) (Centers.get (i). Y-CHILDHEIGHT/2); Child.layout (Childleft, childtop, Childleft + childwidth, childtop + childheight); } log.i (TAG, "onlayout ()--changed=" + Changed + ", left=" + Left + ", top=" + Top + ", right=" + Right + ", bottom=" + Bottom + ", count=" + count); } private int Getcircleindex (int i) {int index = 0; while (i > (3*index*index + 3*index)) {index + +; } return index; }/** * Index start from 0 */private int getcirclecount (int index) {if (index = = 0) {R Eturn 1; } return index*6; } private void Computerpoint (doUble A, double b, double h) {Double sqrt3 = math.sqrt (3); Circlecenteter c01 = new Circlecenteter (A, b); Circlecenteter C11 = new Circlecenteter (A, b-h); Circlecenteter C12 = new Circlecenteter (A + SQRT3*H/2, B-H/2); Circlecenteter C13 = new Circlecenteter (A + SQRT3*H/2, B + h/2); Circlecenteter C14 = new Circlecenteter (A, B + h); Circlecenteter C15 = new Circlecenteter (A-SQRT3*H/2, B + h/2); Circlecenteter C16 = new Circlecenteter (A-SQRT3*H/2, B-H/2); Circlecenteter C21 = new Circlecenteter (A, b-2*h); Circlecenteter C22 = new Circlecenteter (A + SQRT3*H/2, B-3*H/2); Circlecenteter c23 = new Circlecenteter (A + sqrt3*h, b-h); Circlecenteter C24 = new Circlecenteter (A + sqrt3*h, b); Circlecenteter C25 = new Circlecenteter (A + sqrt3*h, B + h); Circlecenteter c26 = new Circlecenteter (A + SQRT3*H/2, B + 3*h/2); Circlecenteter c27 = new Circlecenteter (A, B + 2*h); CirclecEnteter C28 = new Circlecenteter (A-SQRT3*H/2, B + 3*h/2); Circlecenteter c29 = new Circlecenteter (a-sqrt3*h, B + h); Circlecenteter c210 = new Circlecenteter (a-sqrt3*h, b); Circlecenteter c211 = new Circlecenteter (a-sqrt3*h, b-h); Circlecenteter c212 = new Circlecenteter (A-SQRT3*H/2, B-3*H/2); Centers.clear (); Centers.add (C01); Centers.add (C11); Centers.add (C12); Centers.add (C13); Centers.add (C14); Centers.add (C15); Centers.add (C16); Centers.add (C21); Centers.add (C22); Centers.add (C23); Centers.add (C24); Centers.add (C25); Centers.add (C26); Centers.add (C27); Centers.add (C28); Centers.add (c29); Centers.add (c210); Centers.add (c211); Centers.add (c212); } public void Setonitemclick (Specailbutton.onclicklistener l) {int count = Getchildcount (); for (int i = 0; i < count; i++) {(Specailbutton) Getchildat (i)). Setonclicklistener (L); }} private Arraylist<circlecenteter> centers = new Arraylist<specailview.circlecenteter> (7); Class Circlecenteter {double x, y; Public Circlecenteter (double x, double y) {this.x = x; This.y = y; } }}
3.activity_main.xml
<relativelayout 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 "> <com. Qiang.testspecialview.SpecailView android:id= "@+id/specail_view" android:layout_width= "Match_parent" android:layout_height= "400dip" android:background= "#78675645" > <com.qiang.testspecialview.spe Cailbutton android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android oid:background= "@drawable/logo" android:gravity= "center" android:text= "wedding 0"/> <com.qian G.testspecialview.specailbutton android:layout_width= "wrap_content" android:layout_height= "Wrap_con Tent "android:background=" @drawable/logo "android:gravity=" center "android:text=" Wedding 1 "/> ; <com.qiang.testspecialvieW.specailbutton android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:background= "@drawable/logo" android:gravity= "center" android:text= "Wedding 2"/> <com.qiang.testspecialview.specailbutton android:layout_width= "Wrap_content" Android:layout_hei ght= "Wrap_content" android:background= "@drawable/logo" android:gravity= "center" android:t ext= "Wedding 3"/> <com.qiang.testspecialview.specailbutton android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:background= "@drawable/logo" android:gravity= "Center" android:text= "Wedding 4"/> <com.qiang.testspecialview.specailbutton Android : layout_width= "wrap_content" android:layout_height= "wrap_content" android:background= "@drawable/logo "Android:gravity= "center" android:text= "wedding 5"/> <com.qiang.testspecialview.specailbutton an Droid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:background= "@drawable /logo "android:gravity=" center "android:text=" wedding 6 "/> <com.qiang.testspecialview . Specailbutton android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a ndroid:background= "@drawable/logo" android:gravity= "center" android:text= "Wedding 7"/> & Lt;com.qiang.testspecialview.specailbutton android:layout_width= "Wrap_content" Android:layout_heigh t= "Wrap_content" android:background= "@drawable/logo" android:gravity= "center" Android:tex t= "Wedding 8"/> <com.qiang.testspecialview.specailbutton android:layout_width= "Wrap_content" Android:layout_height= "Wrap_content" android:background= "@drawable/logo" android:gravity= "center" Andro id:text= "Wedding 9"/> <com.qiang.testspecialview.specailbutton android:layout_width= "Wrap_conte NT "android:layout_height=" Wrap_content "android:background=" @drawable/logo "Android:grav ity= "center" android:text= "wedding Ten"/> <com.qiang.testspecialview.specailbutton an Droid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:background= "@drawable /logo "android:gravity=" center "android:text=" wedding "/> <com.qiang.testspecialvie W.specailbutton android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:background= "@drawable/logo" android:gravity= "center" android:text= "Wedding"/> <com.qiang.testspecIalview. Specailbutton android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a ndroid:background= "@drawable/logo" android:gravity= "center" android:text= "Wedding"/> <com.qiang.testspecialview.specailbutton android:layout_width= "Wrap_content" Android:layout_heig ht= "Wrap_content" android:background= "@drawable/logo" android:gravity= "center" android:te xt= "Wedding"/> <com.qiang.testspecialview.specailbutton android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:background= "@drawable/logo" android:gravity= "Center" android:text= "wedding"/> <com.qiang.testspecialview.specailbutton android:layout _width= "Wrap_content" android:layout_height= "wrap_content" android:background= "@drawable/logo" Android:gravity= "center" android:text= "wedding"/> <com.qiang.testspecialview.specailbutton Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:background= "@drawab Le/logo "android:gravity=" center "android:text=" wedding "/> <com.qiang.testspecialv Iew. Specailbutton android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a ndroid:background= "@drawable/logo" android:gravity= "center" android:text= "wedding"/> </com.qia Ng.testspecialview.specailview></relativelayout>
4.mainactivity.java
public class Mainactivity extends Activity implements Specailbutton.onclicklistener { private Specailview layout; @Override public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Layout = (Specailview) Findviewbyid (R.id.specail_view); Layout.setonitemclick (this); } @Override public void OnClick (View V, Boolean checked) { String text = ((Specailbutton) v). gettextstring (); C12/>toast.maketext (this, text + checked, toast.length_short). Show (); }}
Okay, here's the final performance:
There is a need for engineering, Source:http://download.csdn.net/detail/hcb1230/6479979
Reprinted from: http://www.sjsjw.com/kf_mobile/article/10_3422_10725.asp
Android implements irregular layout like hexagon