├── .gitignore ├── AndroidManifest.xml ├── README.md ├── build.gradle ├── nexus7.png ├── nexus7_mayer.png ├── quilt.png ├── quilt_horz.png ├── res ├── drawable │ └── ic_launcher.png ├── layout │ └── activity_main.xml ├── menu │ └── activity_main.xml └── values │ ├── attrs.xml │ ├── strings.xml │ └── styles.xml ├── sample └── QuiltViewSample │ ├── AndroidManifest.xml │ ├── build.gradle │ ├── res │ ├── drawable-hdpi │ │ └── ic_launcher.png │ ├── drawable-ldpi │ │ └── ic_launcher.png │ ├── drawable-mdpi │ │ └── ic_launcher.png │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ ├── drawable │ │ ├── mayer.png │ │ └── mayer1.png │ ├── layout │ │ └── activity_main.xml │ ├── menu │ │ └── activity_main.xml │ ├── values-v11 │ │ └── styles.xml │ ├── values-v14 │ │ └── styles.xml │ └── values │ │ ├── strings.xml │ │ └── styles.xml │ └── src │ └── com │ └── jake │ └── quiltviewsample │ └── MainActivity.java └── src └── com └── jake └── quiltview ├── QuiltView.java ├── QuiltViewBase.java └── QuiltViewPatch.java /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | 15 | # Local configuration file (sdk path, etc) 16 | local.properties 17 | 18 | # Eclipse project files 19 | .classpath 20 | .project 21 | 22 | # Android Studio 23 | .idea/ 24 | .gradle 25 | /*/local.properties 26 | /*/out 27 | /*/*/build 28 | /*/*/production 29 | *.iml 30 | *.iws 31 | *.ipr 32 | *~ 33 | *.swp 34 | 35 | build 36 | QuiltViewLibrary.iml 37 | -------------------------------------------------------------------------------- /AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 9 | 10 | 15 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QuiltViewLibrary 2 | 3 | QuiltView displays **views of different sizes** in a **scrollable grid**. 4 | 5 | ![quilt](https://raw.github.com/jacobmoncur/QuiltViewLibrary/master/nexus7.png "QuiltView") ![quilt](https://raw.github.com/jacobmoncur/QuiltViewLibrary/master/nexus7_mayer.png "QuiltView") 6 | 7 | Dependencies 8 | ------------ 9 | This library depends on gridlayout_v7 (git@github.com:jacobmoncur/gridlayout_v7.git) 10 | 11 | Setup 12 | ----- 13 | 14 | The QuiltView can be defined by XML: 15 | 16 | 22 | 23 | 29 | 30 | 31 | 32 | 33 | Or programmatically 34 | 35 | boolean isVertical = true|false; //defines which direction the QuiltView will scroll: true = Vertical, false = Horizontal 36 | QuiltView quiltView = new QuiltView(context, isVertical); //(QuiltView) findViewById(R.id.quilt); 37 | 38 | 39 | Adding Children 40 | --------------- 41 | 42 | Children must be added to the QuiltView programmatically as an ArrayList of ImageViews: 43 | 44 | ArrayList images = new ArrayList(); 45 | for(int i = 0; i < num; i++){ 46 | ImageView image = new ImageView(this.getApplicationContext()); 47 | image.setScaleType(ScaleType.CENTER_CROP); 48 | image.setImageResource(R.drawable.bg); 49 | images.add(image); 50 | } 51 | quiltView.addPatchImages(images); 52 | 53 | Or an ArrayList of Views 54 | 55 | ArrayList views = new ArrayList(); 56 | for(int i = 0; i < num; i++){ 57 | FrameLayout patch = new FrameLayout(this.getApplicationContext()); 58 | views.add(patch); 59 | } 60 | quiltView.addPatchViews(views); 61 | 62 | Goals for this view 63 | ------------------- 64 | * Have no _empty_ patches 65 | * Be sudo-random (lays out the children differently each time) 66 | * Looks awesome! 67 | 68 | TODO 69 | ---- 70 | * Custom Adapter to allow view recycling 71 | * Be able to define children in XML 72 | * Adapt better to different screen sizes 73 | 74 | 75 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'android-library' 2 | 3 | android { 4 | compileSdkVersion 17 5 | buildToolsVersion = 17 6 | 7 | sourceSets { 8 | 9 | main { 10 | assets.srcDirs = ['assets'] 11 | res.srcDirs = ['res'] 12 | aidl.srcDirs = ['src'] 13 | resources.srcDirs = ['src'] 14 | renderscript.srcDirs = ['src'] 15 | java.srcDirs = ['src'] 16 | manifest.srcFile 'AndroidManifest.xml' 17 | 18 | } 19 | } 20 | } 21 | 22 | 23 | dependencies { 24 | compile 'com.android.support:support-v4:18.0.+' 25 | compile 'com.android.support:gridlayout-v7:18.0.+' 26 | } 27 | -------------------------------------------------------------------------------- /nexus7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacobmoncur/QuiltViewLibrary/5e789269bb5a06fb88e08edd1733ad1240b04d49/nexus7.png -------------------------------------------------------------------------------- /nexus7_mayer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacobmoncur/QuiltViewLibrary/5e789269bb5a06fb88e08edd1733ad1240b04d49/nexus7_mayer.png -------------------------------------------------------------------------------- /quilt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacobmoncur/QuiltViewLibrary/5e789269bb5a06fb88e08edd1733ad1240b04d49/quilt.png -------------------------------------------------------------------------------- /quilt_horz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacobmoncur/QuiltViewLibrary/5e789269bb5a06fb88e08edd1733ad1240b04d49/quilt_horz.png -------------------------------------------------------------------------------- /res/drawable/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacobmoncur/QuiltViewLibrary/5e789269bb5a06fb88e08edd1733ad1240b04d49/res/drawable/ic_launcher.png -------------------------------------------------------------------------------- /res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /res/menu/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | -------------------------------------------------------------------------------- /res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | QuiltViewLibrary 5 | Hello world! 6 | Settings 7 | MainActivity 8 | 9 | -------------------------------------------------------------------------------- /res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | -------------------------------------------------------------------------------- /sample/QuiltViewSample/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /sample/QuiltViewSample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'android' 2 | 3 | android { 4 | compileSdkVersion 17 5 | buildToolsVersion = 17 6 | 7 | sourceSets { 8 | 9 | main { 10 | assets.srcDirs = ['assets'] 11 | res.srcDirs = ['res'] 12 | aidl.srcDirs = ['src'] 13 | resources.srcDirs = ['src'] 14 | renderscript.srcDirs = ['src'] 15 | java.srcDirs = ['src'] 16 | manifest.srcFile 'AndroidManifest.xml' 17 | 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /sample/QuiltViewSample/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacobmoncur/QuiltViewLibrary/5e789269bb5a06fb88e08edd1733ad1240b04d49/sample/QuiltViewSample/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/QuiltViewSample/res/drawable-ldpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacobmoncur/QuiltViewLibrary/5e789269bb5a06fb88e08edd1733ad1240b04d49/sample/QuiltViewSample/res/drawable-ldpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/QuiltViewSample/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacobmoncur/QuiltViewLibrary/5e789269bb5a06fb88e08edd1733ad1240b04d49/sample/QuiltViewSample/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/QuiltViewSample/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacobmoncur/QuiltViewLibrary/5e789269bb5a06fb88e08edd1733ad1240b04d49/sample/QuiltViewSample/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/QuiltViewSample/res/drawable/mayer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacobmoncur/QuiltViewLibrary/5e789269bb5a06fb88e08edd1733ad1240b04d49/sample/QuiltViewSample/res/drawable/mayer.png -------------------------------------------------------------------------------- /sample/QuiltViewSample/res/drawable/mayer1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacobmoncur/QuiltViewLibrary/5e789269bb5a06fb88e08edd1733ad1240b04d49/sample/QuiltViewSample/res/drawable/mayer1.png -------------------------------------------------------------------------------- /sample/QuiltViewSample/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /sample/QuiltViewSample/res/menu/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | -------------------------------------------------------------------------------- /sample/QuiltViewSample/res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /sample/QuiltViewSample/res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | -------------------------------------------------------------------------------- /sample/QuiltViewSample/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | QuiltViewSample 5 | Hello world! 6 | Settings 7 | 8 | -------------------------------------------------------------------------------- /sample/QuiltViewSample/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | -------------------------------------------------------------------------------- /sample/QuiltViewSample/src/com/jake/quiltviewsample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.jake.quiltviewsample; 2 | 3 | import java.util.ArrayList; 4 | 5 | import com.jake.quiltview.QuiltView; 6 | 7 | import android.os.Bundle; 8 | import android.app.Activity; 9 | import android.view.View; 10 | import android.widget.ImageView; 11 | import android.widget.ImageView.ScaleType; 12 | 13 | public class MainActivity extends Activity { 14 | public QuiltView quiltView; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_main); 20 | quiltView = (QuiltView) findViewById(R.id.quilt); 21 | quiltView.setChildPadding(5); 22 | addTestQuilts(200); 23 | } 24 | 25 | public void addTestQuilts(int num){ 26 | ArrayList images = new ArrayList(); 27 | for(int i = 0; i < num; i++){ 28 | ImageView image = new ImageView(this.getApplicationContext()); 29 | image.setScaleType(ScaleType.CENTER_CROP); 30 | if(i % 2 == 0) 31 | image.setImageResource(R.drawable.mayer); 32 | else 33 | image.setImageResource(R.drawable.mayer1); 34 | images.add(image); 35 | } 36 | quiltView.addPatchImages(images); 37 | } 38 | 39 | } 40 | 41 | -------------------------------------------------------------------------------- /src/com/jake/quiltview/QuiltView.java: -------------------------------------------------------------------------------- 1 | package com.jake.quiltview; 2 | import java.util.ArrayList; 3 | 4 | import android.content.Context; 5 | import android.content.res.TypedArray; 6 | import android.database.DataSetObserver; 7 | import android.util.AttributeSet; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.view.ViewTreeObserver.OnGlobalLayoutListener; 11 | import android.widget.Adapter; 12 | import android.widget.FrameLayout; 13 | import android.widget.HorizontalScrollView; 14 | import android.widget.ImageView; 15 | import android.widget.LinearLayout; 16 | import android.widget.ScrollView; 17 | 18 | 19 | public class QuiltView extends FrameLayout implements OnGlobalLayoutListener { 20 | 21 | public QuiltViewBase quilt; 22 | public ViewGroup scroll; 23 | public int padding = 5; 24 | public boolean isVertical = false; 25 | public ArrayList views; 26 | private Adapter adapter; 27 | 28 | public QuiltView(Context context,boolean isVertical) { 29 | super(context); 30 | this.isVertical = isVertical; 31 | setup(); 32 | } 33 | 34 | public QuiltView(Context context, AttributeSet attrs) { 35 | super(context, attrs); 36 | TypedArray a = context.obtainStyledAttributes(attrs, 37 | R.styleable.QuiltView); 38 | 39 | String orientation = a.getString(R.styleable.QuiltView_scrollOrientation); 40 | if(orientation != null){ 41 | if(orientation.equals("vertical")){ 42 | isVertical = true; 43 | } else { 44 | isVertical = false; 45 | } 46 | } 47 | setup(); 48 | } 49 | 50 | public void setup(){ 51 | views = new ArrayList(); 52 | 53 | if(isVertical){ 54 | scroll = new ScrollView(this.getContext()); 55 | } else { 56 | scroll = new HorizontalScrollView(this.getContext()); 57 | } 58 | quilt = new QuiltViewBase(getContext(), isVertical); 59 | scroll.addView(quilt); 60 | this.addView(scroll); 61 | 62 | } 63 | 64 | private DataSetObserver adapterObserver = new DataSetObserver(){ 65 | public void onChanged(){ 66 | super.onChanged(); 67 | onDataChanged(); 68 | } 69 | 70 | public void onInvalidated(){ 71 | super.onInvalidated(); 72 | onDataChanged(); 73 | } 74 | 75 | public void onDataChanged(){ 76 | setViewsFromAdapter(adapter); 77 | } 78 | }; 79 | 80 | public void setAdapter(Adapter adapter){ 81 | this.adapter = adapter; 82 | adapter.registerDataSetObserver(adapterObserver); 83 | setViewsFromAdapter(adapter); 84 | } 85 | 86 | private void setViewsFromAdapter(Adapter adapter) { 87 | this.removeAllViews(); 88 | for(int i = 0; i < adapter.getCount(); i++){ 89 | quilt.addPatch(adapter.getView(i, null, quilt)); 90 | } 91 | } 92 | 93 | public void addPatchImages(ArrayList images){ 94 | 95 | for(ImageView image: images){ 96 | addPatchImage(image); 97 | } 98 | } 99 | 100 | public void addPatchImage(ImageView image){ 101 | 102 | FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT); 103 | image.setLayoutParams(params); 104 | 105 | LinearLayout wrapper = new LinearLayout(this.getContext()); 106 | wrapper.setPadding(padding, padding, padding, padding); 107 | wrapper.addView(image); 108 | quilt.addPatch(wrapper); 109 | } 110 | 111 | public void addPatchViews(ArrayList views_a){ 112 | for(View view: views_a){ 113 | quilt.addPatch(view); 114 | } 115 | } 116 | 117 | public void addPatchView(View view){ 118 | quilt.addPatch(view); 119 | } 120 | 121 | public void addPatchesOnLayout(){ 122 | for(View view: views){ 123 | quilt.addPatch(view); 124 | } 125 | } 126 | 127 | public void removeQuilt(View view){ 128 | quilt.removeView(view); 129 | } 130 | 131 | public void setChildPadding(int padding){ 132 | this.padding = padding; 133 | } 134 | 135 | public void refresh(){ 136 | quilt.refresh(); 137 | } 138 | 139 | public void setOrientation(boolean isVertical){ 140 | this.isVertical = isVertical; 141 | } 142 | 143 | 144 | @Override 145 | public void onGlobalLayout() { 146 | //addPatchesOnLayout(); 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /src/com/jake/quiltview/QuiltViewBase.java: -------------------------------------------------------------------------------- 1 | package com.jake.quiltview; 2 | 3 | import java.util.ArrayList; 4 | 5 | import android.content.Context; 6 | import android.support.v7.widget.GridLayout; 7 | import android.util.DisplayMetrics; 8 | import android.view.View; 9 | import android.widget.FrameLayout; 10 | 11 | 12 | public class QuiltViewBase extends GridLayout { 13 | 14 | public int[] size; 15 | public int columns; 16 | public int rows; 17 | public int view_width = -1; 18 | public int view_height = -1; 19 | public boolean isVertical = true; 20 | public ArrayList views; 21 | 22 | public QuiltViewBase(Context context, boolean isVertical) { 23 | super(context); 24 | this.isVertical = isVertical; 25 | if(view_width == -1){ 26 | DisplayMetrics metrics = this.getResources().getDisplayMetrics(); 27 | int width = metrics.widthPixels; 28 | int height = metrics.heightPixels - 120; 29 | view_width = width - this.getPaddingLeft() - this.getPaddingRight(); 30 | view_height = height - this.getPaddingTop() - this.getPaddingBottom(); 31 | } 32 | views = new ArrayList(); 33 | setup(); 34 | } 35 | 36 | public void setup(){ 37 | if(isVertical){ 38 | setupVertical(); 39 | } else { 40 | setupHorizontal(); 41 | } 42 | } 43 | 44 | public void setupVertical(){ 45 | size = getBaseSizeVertical(); 46 | this.setColumnCount(columns); 47 | this.setRowCount(-1); 48 | this.setOrientation(this.HORIZONTAL); 49 | FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT); 50 | this.setLayoutParams(params); 51 | } 52 | 53 | public void setupHorizontal(){ 54 | size = getBaseSizeHorizontal(); 55 | this.setRowCount(rows); 56 | this.setColumnCount(-1); 57 | this.setOrientation(this.VERTICAL); 58 | FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT); 59 | this.setLayoutParams(params); 60 | } 61 | 62 | public void addPatch(View view){ 63 | 64 | int count = this.getChildCount(); 65 | 66 | QuiltViewPatch child = QuiltViewPatch.init(count, columns); 67 | 68 | GridLayout.LayoutParams params = new GridLayout.LayoutParams(); 69 | params.width = size[0]*child.width_ratio; 70 | params.height = size[1]*child.height_ratio; 71 | params.rowSpec = GridLayout.spec(Integer.MIN_VALUE, child.height_ratio); 72 | params.columnSpec = GridLayout.spec(Integer.MIN_VALUE, child.width_ratio); 73 | view.setLayoutParams(params); 74 | addView(view); 75 | views.add(view); 76 | } 77 | 78 | public void refresh(){ 79 | this.removeAllViewsInLayout(); 80 | setup(); 81 | for(View view : views){ 82 | addPatch(view); 83 | } 84 | } 85 | 86 | public int[] getBaseSize(){ 87 | int[] size = new int[2]; 88 | 89 | float width_height_ratio = (3.0f/4.0f); 90 | 91 | int base_width = getBaseWidth(); 92 | int base_height = (int) (base_width*width_height_ratio); 93 | 94 | size[0] = base_width; // width 95 | size[1] = base_height; // height 96 | return size; 97 | } 98 | 99 | public int[] getBaseSizeVertical(){ 100 | int[] size = new int[2]; 101 | 102 | float width_height_ratio = (3.0f/4.0f); 103 | 104 | int base_width = getBaseWidth(); 105 | int base_height = (int) (base_width*width_height_ratio); 106 | 107 | size[0] = base_width; // width 108 | size[1] = base_height; // height 109 | return size; 110 | } 111 | 112 | public int[] getBaseSizeHorizontal(){ 113 | int[] size = new int[2]; 114 | 115 | float width_height_ratio = (4.0f/3.0f); 116 | 117 | int base_height = getBaseHeight(); 118 | int base_width = (int) (base_height*width_height_ratio); 119 | 120 | size[0] = base_width; // width 121 | size[1] = base_height; // height 122 | return size; 123 | } 124 | 125 | public int getBaseWidth(){ 126 | if(view_width < 500){ 127 | columns = 2; 128 | } else if(view_width < 801){ 129 | columns = 3; 130 | } else if(view_width < 1201){ 131 | columns = 4; 132 | } else if(view_width < 1601){ 133 | columns = 5; 134 | } else { 135 | columns = 6; 136 | } 137 | return (view_width / columns); 138 | } 139 | 140 | public int getBaseHeight(){ 141 | if(view_height < 350){ 142 | rows = 2; 143 | } else if(view_height < 650){ 144 | rows = 3; 145 | } else if(view_height < 1050){ 146 | rows = 4; 147 | } else if(view_height < 1250){ 148 | rows = 5; 149 | } else { 150 | rows = 6; 151 | } 152 | return (view_height / rows); 153 | } 154 | 155 | /*@Override 156 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 157 | int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 158 | int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 159 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 160 | 161 | view_width = parentWidth; 162 | view_height = parentHeight; 163 | 164 | setup(isVertical); 165 | }*/ 166 | 167 | @Override 168 | protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){ 169 | super.onSizeChanged(xNew, yNew, xOld, yOld); 170 | view_width = xNew; 171 | view_height = yNew; 172 | } 173 | 174 | 175 | } 176 | -------------------------------------------------------------------------------- /src/com/jake/quiltview/QuiltViewPatch.java: -------------------------------------------------------------------------------- 1 | package com.jake.quiltview; 2 | 3 | public class QuiltViewPatch implements Comparable{ 4 | public int width_ratio; 5 | public int height_ratio; 6 | 7 | public QuiltViewPatch(int width_ratio, int height_ratio){ 8 | this.width_ratio = width_ratio; 9 | this.height_ratio = height_ratio; 10 | } 11 | 12 | private static QuiltViewPatch create(Size size){ 13 | switch(size){ 14 | case Big: 15 | return new QuiltViewPatch(2,2); 16 | case Small: 17 | return new QuiltViewPatch(1,1); 18 | case Tall: 19 | return new QuiltViewPatch(1,2); 20 | } 21 | 22 | return new QuiltViewPatch(1,1); 23 | } 24 | 25 | public int getHeightRatio(){return this.height_ratio;} 26 | public int getWidthRatio(){return this.width_ratio;} 27 | 28 | public static QuiltViewPatch create(int view_count){ 29 | 30 | if(view_count == 0) 31 | return new QuiltViewPatch(2,2); 32 | else if((view_count % 11) == 0) 33 | return new QuiltViewPatch(2,2); 34 | else if((view_count % 4) == 0) 35 | return new QuiltViewPatch(1,2); 36 | else 37 | return new QuiltViewPatch(1,1); 38 | 39 | } 40 | 41 | private enum Size{ 42 | Big, 43 | Small, 44 | Tall 45 | } 46 | 47 | public static QuiltViewPatch init(int position, int column){ 48 | switch(column){ 49 | case 2: 50 | return init2(position); 51 | case 3: 52 | return init3(position); 53 | case 4: 54 | return init4(position); 55 | case 5: 56 | return init5(position); 57 | } 58 | 59 | return init3(position); 60 | } 61 | 62 | private static QuiltViewPatch init2(int position){ 63 | switch(position % 15){ 64 | case 0: 65 | return create(Size.Big); 66 | case 1: 67 | case 2: 68 | case 3: 69 | return create(Size.Small); 70 | case 4: 71 | return create(Size.Tall); 72 | case 5: 73 | case 6: 74 | case 7: 75 | return create(Size.Small); 76 | case 8: 77 | return create(Size.Tall); 78 | case 9: 79 | return create(Size.Tall); 80 | case 10: 81 | return create(Size.Small); 82 | case 11: 83 | return create(Size.Small); 84 | case 12: 85 | return create(Size.Big); 86 | case 13: 87 | return create(Size.Tall); 88 | case 14: 89 | return create(Size.Tall); 90 | } 91 | 92 | return create(Size.Small); 93 | } 94 | 95 | private static QuiltViewPatch init3(int position){ 96 | switch(position % 32){ 97 | case 0: 98 | return create(Size.Big); 99 | case 1: 100 | case 2: 101 | case 3: 102 | return create(Size.Small); 103 | case 4: 104 | return create(Size.Tall); 105 | case 5: 106 | case 6: 107 | case 7: 108 | return create(Size.Small); 109 | case 8: 110 | return create(Size.Tall); 111 | case 9: 112 | case 10: 113 | return create(Size.Small); 114 | case 11: 115 | return create(Size.Big); 116 | case 12: 117 | return create(Size.Tall); 118 | case 13: 119 | case 14: 120 | return create(Size.Small); 121 | case 15: 122 | return create(Size.Small); 123 | case 16: 124 | return create(Size.Tall); 125 | case 17: 126 | case 18: 127 | case 19: 128 | return create(Size.Small); 129 | case 20: 130 | return create(Size.Tall); 131 | case 21: 132 | case 22: 133 | return create(Size.Small); 134 | case 23: 135 | return create(Size.Big); 136 | case 24: 137 | return create(Size.Small); 138 | case 25: 139 | return create(Size.Tall); 140 | case 26: 141 | case 27: 142 | case 28: 143 | return create(Size.Small); 144 | case 29: 145 | return create(Size.Tall); 146 | case 30: 147 | case 31: 148 | return create(Size.Small); 149 | } 150 | 151 | return create(Size.Small); 152 | } 153 | 154 | private static QuiltViewPatch init4(int position){ 155 | switch(position % 36){ 156 | case 0: 157 | return create(Size.Big); 158 | case 1: 159 | case 2: 160 | case 3: 161 | return create(Size.Small); 162 | case 4: 163 | return create(Size.Tall); 164 | case 5: 165 | case 6: 166 | case 7: 167 | return create(Size.Small); 168 | case 8: 169 | return create(Size.Tall); 170 | case 9: 171 | case 10: 172 | case 11: 173 | return create(Size.Small); 174 | case 12: 175 | return create(Size.Big); 176 | case 13: 177 | return create(Size.Tall); 178 | case 14: 179 | case 15: 180 | case 16: 181 | return create(Size.Small); 182 | case 17: 183 | return create(Size.Tall); 184 | case 18: 185 | case 19: 186 | case 20: 187 | return create(Size.Small); 188 | case 21: 189 | return create(Size.Tall); 190 | case 22: 191 | case 23: 192 | return create(Size.Small); 193 | case 24: 194 | return create(Size.Small); 195 | case 25: 196 | return create(Size.Big); 197 | case 26: 198 | return create(Size.Small); 199 | case 27: 200 | return create(Size.Tall); 201 | case 28: 202 | case 29: 203 | case 30: 204 | return create(Size.Small); 205 | case 31: 206 | return create(Size.Tall); 207 | case 32: 208 | case 33: 209 | case 34: 210 | case 35: 211 | return create(Size.Small); 212 | } 213 | 214 | return create(Size.Small); 215 | } 216 | 217 | private static QuiltViewPatch init5(int position){ 218 | switch(position % 35){ 219 | case 0: 220 | return create(Size.Big); 221 | case 1: 222 | case 2: 223 | case 3: 224 | return create(Size.Small); 225 | case 4: 226 | return create(Size.Tall); 227 | case 5: 228 | case 6: 229 | case 7: 230 | return create(Size.Small); 231 | case 8: 232 | return create(Size.Tall); 233 | case 9: 234 | case 10: 235 | case 11: 236 | return create(Size.Small); 237 | case 12: 238 | return create(Size.Big); 239 | case 13: 240 | return create(Size.Tall); 241 | case 14: 242 | case 15: 243 | case 16: 244 | return create(Size.Small); 245 | case 17: 246 | return create(Size.Tall); 247 | case 18: 248 | case 19: 249 | case 20: 250 | return create(Size.Small); 251 | case 21: 252 | return create(Size.Tall); 253 | case 22: 254 | case 23: 255 | case 24: 256 | return create(Size.Small); 257 | case 25: 258 | return create(Size.Big); 259 | case 26: 260 | return create(Size.Small); 261 | case 27: 262 | return create(Size.Tall); 263 | case 28: 264 | case 29: 265 | case 30: 266 | return create(Size.Small); 267 | case 31: 268 | return create(Size.Tall); 269 | case 32: 270 | return create(Size.Big); 271 | case 33: 272 | return create(Size.Tall); 273 | case 34: 274 | return create(Size.Small); 275 | 276 | } 277 | 278 | return create(Size.Small); 279 | } 280 | 281 | public static boolean getRandomBoolean(){ 282 | return (Math.random() < 0.5); 283 | } 284 | 285 | public boolean equals(Object obj){ 286 | if(obj != null && obj instanceof QuiltViewPatch){ 287 | QuiltViewPatch size = (QuiltViewPatch)obj; 288 | return size.height_ratio == this.height_ratio && size.width_ratio == this.width_ratio; 289 | } 290 | 291 | return false; 292 | } 293 | 294 | public int hashCode(){ 295 | return height_ratio + 100 * width_ratio; 296 | } 297 | 298 | public String toString(){ 299 | return "Patch: " + height_ratio + " x " + width_ratio; 300 | } 301 | 302 | @Override 303 | public int compareTo(Object another) { 304 | if(another != null && another instanceof QuiltViewPatch){ 305 | QuiltViewPatch size = (QuiltViewPatch)another; 306 | if(size.equals(this)) 307 | return 0; 308 | 309 | if(this.height_ratio < size.height_ratio) 310 | return -1; 311 | else if(this.height_ratio > size.height_ratio) 312 | return 1; 313 | 314 | if(this.width_ratio < size.width_ratio) 315 | return -1; 316 | else 317 | return 1; 318 | } 319 | return -1; 320 | } 321 | } 322 | --------------------------------------------------------------------------------