Android Nine Gongge (16 Gongge) control

Source: Internet
Author: User

public class Ninerectview extends ViewGroup {

Private Context CTX;

private int wsize,hsize,row,column,count,childwidth,childheight,parent_padding_top_bottom;//layout mode by default Wsize=hsize

The spacing is the same

private int childmargin = 4;

private int parent_padding;

private int first_row_num;

private int childcorner=2;

Private String childavatarurlappend;

private int childdefaultimageid = R.drawable.tt_default_user_portrait_corner;

public void Setchildcorner (int corner)

{

This.childcorner = corner;

}

public void Setavatarurlappend (String avatarurlappend)

{

This.childavatarurlappend = Avatarurlappend;

}

public void Setchilddefaultimageid (int id)

{

This.childdefaultimageid = ID;

}

public void Setchildmargin (int margin)

{

This.childmargin = margin;

}

Public Ninerectview (Context context) {

Super (context);

This.ctx = context;

}

Public Ninerectview (context context, AttributeSet Attrs) {

Super (context, attrs);

This.ctx = context;

}

Public Ninerectview (context context, AttributeSet Attrs,int Defstyle) {

Super (context, attrs,defstyle);

This.ctx = context;

}

public void Setavatarurls (list<string> urllist)

{

Count = Urllist.size ();

Double m = math.sqrt (count);

int k = (int) m;

if (m-k==0)

{

column = k;

first_row_num = column;

}

Else

{

column = k+1;

}

if (count%column==0)

{

row = Count/column;

first_row_num = column;

}

Else

{

row = count/column+1;

First_row_num = Count%column;

}

This.removeallviews ();

for (int i=0;i<count;i++)

{

Imbaseimageview imageview=new Imbaseimageview (CTX);

Imageview.setcorner (Childcorner);

Imageview.setdefaultimageres (Childdefaultimageid);

Imageview.setavatarappend (Childavatarurlappend);

Imageview.setimageurl (Urllist.get (i));

This.addview (ImageView);

}

}

@Override

protected void OnLayout (Boolean changed, int left, int top, int. right, int bottom) {

if (Row*column>=count)

{

Parent_padding=getpaddingleft ();

This.wsize = GetWidth ();

This.hsize = GetHeight ();

Computepoint ();

if (count==1)//special treatment for 1

{

int cw= (wsize-parent_padding*2-childmargin)/2;

int ch = (hsize-parent_padding*2-childmargin)/2;

int x = (WSIZE-CW)/2;

int y = (wsize-ch)/2;

View child = Getchildat (0);

Child.layout (x, Y, X+CW, y+ch);

}

Else

{

for (int i = 0; i < count; i++) {

View child = Getchildat (i);

Child.layout (Startcoordinates.get (i). x, Startcoordinates.get (i). Y, Startcoordinates.get (i). X+childwidth, Startcoordinates.get (i). Y+childheight);

}

}

}

}

/**

* Calculate coordinates

*/

private void Computepoint ()

{

Startcoordinates.clear ();

Childwidth = (wsize-parent_padding*2-(column-1) * childmargin)/column;

Childheight = childwidth;//Square

Parent_padding_top_bottom = (Hsize-row*childheight-(row-1) *childmargin)/2;

Calculates the entire row in the bottom-up order

for (int i=0;i<row;i++)

{

for (int j=0;j<column;j++)

{

int x, y;

y = parent_padding_top_bottom+i*childheight+i*childmargin;

if (i==0)

{

if (j<first_row_num)

{

x = (wsize-first_row_num*childwidth-(first_row_num-1) *childmargin)/2+j*childmargin+j*childwidth;

}

Else

{

Break

}

}

Else

{

x = Parent_padding+j*childwidth+j*childmargin;

}

Startcoordinates.add (New Startcoordinate (x, y));

}

}

}

Private arraylist<startcoordinate> startcoordinates = new arraylist<startcoordinate> ();

Class Startcoordinate {

int x, y;

public startcoordinate (int x, int y) {

this.x = x;

This.y = y;

}

}

}

Android Nine Gongge (16 Gongge) control

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.