标签:
public class NineRectView extends ViewGroup {
private Context ctx;
private int wSize,hSize,row,column,count,childWidth,childHeight,parent_padding_top_bottom;//布局方式按照默认wSize=Hsize
//间距都相同
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)//为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);
}
}
}
}
/**
* 计算坐标
*/
private void computePoint()
{
startCoordinates.clear();
childWidth = (wSize- parent_padding*2-(column-1)* childMargin)/column;
childHeight = childWidth;//正方形
parent_padding_top_bottom = (hSize - row*childHeight - (row-1)*childMargin)/2;
//计算整排 按照从下向上的顺序计算
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;
}
}
}
标签:
原文地址:http://www.cnblogs.com/qiyongqiang/p/4251878.html