使用QuiltView lib时,控制Gridlayout中的补丁大小

时间:2013-04-03 04:59:11

标签: android android-custom-view grid-layout android-gridlayout quilt

我大约一年前开始进行Android开发,自从我开始开发以来,我想创建一个包含所有不同大小的盒子的布局,这些盒子可以很贴合w。任何差距。我唯一的另一个要求就是我能够在runntime中以编程方式确定盒子的大小.....我认为这是一个问题,只要我很酷,制作视图在可滚动内部的框架。我四处询问,并且我在这里或任何地方问过任何想法怎么做,所以id把项目放在一边几个月,拿起我离开的地方,研究并再次询问,什么都没找到....冲洗并重复。

直到最后我才遇到这个华丽的图书馆似乎是我的问题的答案:https://github.com/jacobmoncur/QuiltViewLibrary

唯一的问题是它完全随意,我发现项目的一个分支试图使被子“非随机”,似乎作者试图为我的问题找到一个解决方案,但是很难过并且走出了简单的道路而是第二个“非随机版本”(只有3个短类),下面是补丁类:https://github.com/jacobmoncur/QuiltViewLibrary/blob/non-randomized-quilt/src/com/jake/quiltview/QuiltViewBase.java

QuiltViewPatch.java:

public class QuiltViewPatch implements Comparable{
public int width_ratio;
public int height_ratio;

public QuiltViewPatch(int width_ratio, int height_ratio){
this.width_ratio = width_ratio;
this.height_ratio = height_ratio;
}

private static QuiltViewPatch create(Size size){
switch(size){
case Big:
return new QuiltViewPatch(2,2);
case Small:
return new QuiltViewPatch(1,1);
case Tall:
return new QuiltViewPatch(1,2);
}

return new QuiltViewPatch(1,1);
}

public int getHeightRatio(){return this.height_ratio;}
public int getWidthRatio(){return this.width_ratio;}

public static QuiltViewPatch create(int view_count){

if(view_count == 0)
return new QuiltViewPatch(2,2);
else if((view_count % 11) == 0)
return new QuiltViewPatch(2,2);
else if((view_count % 4) == 0)
return new QuiltViewPatch(1,2);
else
return new QuiltViewPatch(1,1);

}

private enum Size{
Big,
Small,
Tall
}

public static QuiltViewPatch init(int position, int column){
switch(column){
case 2:
return init2(position);
case 3:
return init3(position);
case 4:
return init4(position);
case 5:
return init5(position);
}

return init3(position);
}

private static QuiltViewPatch init2(int position){
switch(position % 15){
case 0:
return create(Size.Big);
case 1:
case 2:
case 3:
return create(Size.Small);
case 4:
return create(Size.Tall);
case 5:
case 6:
case 7:
return create(Size.Small);
case 8:
return create(Size.Tall);
case 9:
return create(Size.Tall);
case 10:
return create(Size.Small);
case 11:
return create(Size.Big);
case 12:
return create(Size.Tall);
case 13:
return create(Size.Tall);
case 14:
return create(Size.Small);
}

return create(Size.Small);
}

private static QuiltViewPatch init3(int position){
switch(position % 32){
case 0:
return create(Size.Big);
case 1:
case 2:
case 3:
return create(Size.Small);
case 4:
return create(Size.Tall);
case 5:
case 6:
case 7:
return create(Size.Small);
case 8:
return create(Size.Tall);
case 9:
case 10:
return create(Size.Small);
case 11:
return create(Size.Big);
case 12:
return create(Size.Tall);
case 13:
case 14:
return create(Size.Small);
case 15:
return create(Size.Small);
case 16:
return create(Size.Tall);
case 17:
case 18:
case 19:
return create(Size.Small);
case 20:
return create(Size.Tall);
case 21:
case 22:
return create(Size.Small);
case 23:
return create(Size.Big);
case 24:
return create(Size.Small);
case 25:
return create(Size.Tall);
case 26:
case 27:
case 28:
return create(Size.Small);
case 29:
return create(Size.Tall);
case 30:
case 31:
return create(Size.Small);
}

return create(Size.Small);
}

private static QuiltViewPatch init4(int position){
switch(position % 36){
case 0:
return create(Size.Big);
case 1:
case 2:
case 3:
return create(Size.Small);
case 4:
return create(Size.Tall);
case 5:
case 6:
case 7:
return create(Size.Small);
case 8:
return create(Size.Tall);
case 9:
case 10:
case 11:
return create(Size.Small);
case 12:
return create(Size.Big);
case 13:
return create(Size.Tall);
case 14:
case 15:
case 16:
return create(Size.Small);
case 17:
return create(Size.Tall);
case 18:
case 19:
case 20:
return create(Size.Small);
case 21:
return create(Size.Tall);
case 22:
case 23:
return create(Size.Small);
case 24:
return create(Size.Small);
case 25:
return create(Size.Big);
case 26:
return create(Size.Small);
case 27:
return create(Size.Tall);
case 28:
case 29:
case 30:
return create(Size.Small);
case 31:
return create(Size.Tall);
case 32:
case 33:
case 34:
case 35:
return create(Size.Small);
}

return create(Size.Small);
}

private static QuiltViewPatch init5(int position){
switch(position % 35){
case 0:
return create(Size.Big);
case 1:
case 2:
case 3:
return create(Size.Small);
case 4:
return create(Size.Tall);
case 5:
case 6:
case 7:
return create(Size.Small);
case 8:
return create(Size.Tall);
case 9:
case 10:
case 11:
return create(Size.Small);
case 12:
return create(Size.Big);
case 13:
return create(Size.Tall);
case 14:
case 15:
case 16:
return create(Size.Small);
case 17:
return create(Size.Tall);
case 18:
case 19:
case 20:
return create(Size.Small);
case 21:
return create(Size.Tall);
case 22:
case 23:
case 24:
return create(Size.Small);
case 25:
return create(Size.Big);
case 26:
return create(Size.Small);
case 27:
return create(Size.Tall);
case 28:
case 29:
case 30:
return create(Size.Small);
case 31:
return create(Size.Tall);
case 32:
return create(Size.Big);
case 33:
return create(Size.Tall);
case 34:
return create(Size.Small);

}

return create(Size.Small);
}

public static boolean getRandomBoolean(){
return (Math.random() < 0.5);
}

public boolean equals(Object obj){
if(obj != null && obj instanceof QuiltViewPatch){
QuiltViewPatch size = (QuiltViewPatch)obj;
return size.height_ratio == this.height_ratio && size.width_ratio == this.width_ratio;
}

return false;
}

public int hashCode(){
return height_ratio + 100 * width_ratio;
}

public String toString(){
return "Patch: " + height_ratio + " x " + width_ratio;
}

@Override
public int compareTo(Object another) {
if(another != null && another instanceof QuiltViewPatch){
QuiltViewPatch size = (QuiltViewPatch)another;
if(size.equals(this))
return 0;

if(this.height_ratio < size.height_ratio)
return -1;
else if(this.height_ratio > size.height_ratio)
return 1;

if(this.width_ratio < size.width_ratio)
return -1;
else
return 1;
}
return -1;
}
}

0 个答案:

没有答案