13f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne/* 23f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Copyright (C) 2011 The Android Open Source Project 33f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 43f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Licensed under the Apache License, Version 2.0 (the "License"); 53f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * you may not use this file except in compliance with the License. 63f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * You may obtain a copy of the License at 73f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 83f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * http://www.apache.org/licenses/LICENSE-2.0 93f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 103f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Unless required by applicable law or agreed to in writing, software 113f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * distributed under the License is distributed on an "AS IS" BASIS, 123f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * See the License for the specific language governing permissions and 143f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * limitations under the License. 153f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 163f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 173f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milnepackage android.widget; 183f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 19d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbyeimport android.annotation.IntDef; 203f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport android.content.Context; 213f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport android.content.res.TypedArray; 223f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport android.graphics.Canvas; 233f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport android.graphics.Color; 241557fd7809078e421f751efc7d2539b3efdc54b2Philip Milneimport android.graphics.Insets; 253f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport android.graphics.Paint; 263f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport android.util.AttributeSet; 273f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport android.util.Log; 28211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milneimport android.util.LogPrinter; 2948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milneimport android.util.Pair; 30211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milneimport android.util.Printer; 313f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport android.view.Gravity; 323f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport android.view.View; 333f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport android.view.ViewGroup; 34edd69518ffec87fb7b1931e708678ef441152cdePhilip Milneimport android.widget.RemoteViews.RemoteView; 3510ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milneimport com.android.internal.R; 363f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 37d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbyeimport java.lang.annotation.Retention; 38d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbyeimport java.lang.annotation.RetentionPolicy; 393f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport java.lang.reflect.Array; 403f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport java.util.ArrayList; 413f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport java.util.Arrays; 423f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport java.util.HashMap; 433f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport java.util.List; 443f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport java.util.Map; 453f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 465125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milneimport static android.view.Gravity.*; 47899d5922870c78e0e663bc5661849eb468afc984Philip Milneimport static android.view.View.MeasureSpec.EXACTLY; 48899d5922870c78e0e663bc5661849eb468afc984Philip Milneimport static android.view.View.MeasureSpec.makeMeasureSpec; 493f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport static java.lang.Math.max; 503f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milneimport static java.lang.Math.min; 513f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 523f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne/** 533f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * A layout that places its children in a rectangular <em>grid</em>. 543f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <p> 553f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * The grid is composed of a set of infinitely thin lines that separate the 563f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * viewing area into <em>cells</em>. Throughout the API, grid lines are referenced 577fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * by grid <em>indices</em>. A grid with {@code N} columns 587fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * has {@code N + 1} grid indices that run from {@code 0} 597fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * through {@code N} inclusive. Regardless of how GridLayout is 607fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * configured, grid index {@code 0} is fixed to the leading edge of the 617fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * container and grid index {@code N} is fixed to its trailing edge 623f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * (after padding is taken into account). 633f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 6493cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * <h4>Row and Column Specs</h4> 653f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 663f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Children occupy one or more contiguous cells, as defined 6793cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * by their {@link GridLayout.LayoutParams#rowSpec rowSpec} and 6893cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * {@link GridLayout.LayoutParams#columnSpec columnSpec} layout parameters. 6993cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * Each spec defines the set of rows or columns that are to be 703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * occupied; and how children should be aligned within the resulting group of cells. 713f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Although cells do not normally overlap in a GridLayout, GridLayout does 723f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * not prevent children being defined to occupy the same cell or group of cells. 733f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * In this case however, there is no guarantee that children will not themselves 743f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * overlap after the layout operation completes. 753f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 763f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <h4>Default Cell Assignment</h4> 773f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 7848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne * If a child does not specify the row and column indices of the cell it 793f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * wishes to occupy, GridLayout assigns cell locations automatically using its: 803f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@link GridLayout#setOrientation(int) orientation}, 813f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@link GridLayout#setRowCount(int) rowCount} and 823f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@link GridLayout#setColumnCount(int) columnCount} properties. 833f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 843f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <h4>Space</h4> 853f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 863f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Space between children may be specified either by using instances of the 873f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * dedicated {@link Space} view or by setting the 883f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 893f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@link ViewGroup.MarginLayoutParams#leftMargin leftMargin}, 903f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@link ViewGroup.MarginLayoutParams#topMargin topMargin}, 913f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@link ViewGroup.MarginLayoutParams#rightMargin rightMargin} and 923f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@link ViewGroup.MarginLayoutParams#bottomMargin bottomMargin} 933f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 943f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * layout parameters. When the 953f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@link GridLayout#setUseDefaultMargins(boolean) useDefaultMargins} 963f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * property is set, default margins around children are automatically 97f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * allocated based on the prevailing UI style guide for the platform. 98f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * Each of the margins so defined may be independently overridden by an assignment 993f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * to the appropriate layout parameter. 100f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * Default values will generally produce a reasonable spacing between components 101f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * but values may change between different releases of the platform. 1023f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 1033f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <h4>Excess Space Distribution</h4> 1043f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 10587260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * As of API 21, GridLayout's distribution of excess space accomodates the principle of weight. 10687260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * In the event that no weights are specified, the previous conventions are respected and 10787260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * columns and rows are taken as flexible if their views specify some form of alignment 10887260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * within their groups. 109f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * <p> 11087260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * The flexibility of a view is therefore influenced by its alignment which is, 11187260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * in turn, typically defined by setting the 11287260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * {@link LayoutParams#setGravity(int) gravity} property of the child's layout parameters. 11387260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * If either a weight or alignment were defined along a given axis then the component 11487260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * is taken as <em>flexible</em> in that direction. If no weight or alignment was set, 115f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * the component is instead assumed to be <em>inflexible</em>. 116f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * <p> 117f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * Multiple components in the same row or column group are 118f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * considered to act in <em>parallel</em>. Such a 119899d5922870c78e0e663bc5661849eb468afc984Philip Milne * group is flexible only if <em>all</em> of the components 120899d5922870c78e0e663bc5661849eb468afc984Philip Milne * within it are flexible. Row and column groups that sit either side of a common boundary 121899d5922870c78e0e663bc5661849eb468afc984Philip Milne * are instead considered to act in <em>series</em>. The composite group made of these two 122899d5922870c78e0e663bc5661849eb468afc984Philip Milne * elements is flexible if <em>one</em> of its elements is flexible. 123899d5922870c78e0e663bc5661849eb468afc984Philip Milne * <p> 124899d5922870c78e0e663bc5661849eb468afc984Philip Milne * To make a column stretch, make sure all of the components inside it define a 12587260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * weight or a gravity. To prevent a column from stretching, ensure that one of the components 12687260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * in the column does not define a weight or a gravity. 1273f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <p> 128f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * When the principle of flexibility does not provide complete disambiguation, 129f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * GridLayout's algorithms favour rows and columns that are closer to its <em>right</em> 13087260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * and <em>bottom</em> edges. To be more precise, GridLayout treats each of its layout 13187260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * parameters as a constraint in the a set of variables that define the grid-lines along a 13287260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * given axis. During layout, GridLayout solves the constraints so as to return the unique 13387260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * solution to those constraints for which all variables are less-than-or-equal-to 13487260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * the corresponding value in any other valid solution. 135f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * 136a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne * <h4>Interpretation of GONE</h4> 137a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne * 138a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne * For layout purposes, GridLayout treats views whose visibility status is 139a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne * {@link View#GONE GONE}, as having zero width and height. This is subtly different from 140a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne * the policy of ignoring views that are marked as GONE outright. If, for example, a gone-marked 141a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne * view was alone in a column, that column would itself collapse to zero width if and only if 142a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne * no gravity was defined on the view. If gravity was defined, then the gone-marked 143a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne * view has no effect on the layout and the container should be laid out as if the view 1446dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar * had never been added to it. GONE views are taken to have zero weight during excess space 1456dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar * distribution. 1466dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar * <p> 147a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne * These statements apply equally to rows as well as columns, and to groups of rows or columns. 148a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne * 1493f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <p> 1503f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * See {@link GridLayout.LayoutParams} for a full description of the 1513f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * layout parameters used by GridLayout. 1523f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 1533f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_orientation 1543f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_rowCount 1553f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_columnCount 1563f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_useDefaultMargins 1573f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_rowOrderPreserved 1583f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_columnOrderPreserved 1593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 160edd69518ffec87fb7b1931e708678ef441152cdePhilip Milne@RemoteView 1613f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milnepublic class GridLayout extends ViewGroup { 1623f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1633f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Public constants 1643f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 165d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbye /** @hide */ 166d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbye @IntDef({HORIZONTAL, VERTICAL}) 167d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbye @Retention(RetentionPolicy.SOURCE) 168d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbye public @interface Orientation {} 169d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbye 1703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 1713f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * The horizontal orientation. 1723f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 1733f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public static final int HORIZONTAL = LinearLayout.HORIZONTAL; 174aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 1753f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 1763f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * The vertical orientation. 1773f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 1783f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public static final int VERTICAL = LinearLayout.VERTICAL; 1793f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 180aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne /** 181aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * The constant used to indicate that a value is undefined. 182aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * Fields can use this value to indicate that their values 183aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * have not yet been set. Similarly, methods can return this value 184aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * to indicate that there is no suitable value that the implementation 185aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * can return. 186aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * The value used for the constant (currently {@link Integer#MIN_VALUE}) is 187aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * intended to avoid confusion between valid values whose sign may not be known. 188aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne */ 189aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne public static final int UNDEFINED = Integer.MIN_VALUE; 190aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 191d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbye /** @hide */ 192d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbye @IntDef({ALIGN_BOUNDS, ALIGN_MARGINS}) 193d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbye @Retention(RetentionPolicy.SOURCE) 194d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbye public @interface AlignmentMode {} 195d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbye 1961e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne /** 1971e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * This constant is an {@link #setAlignmentMode(int) alignmentMode}. 1981e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * When the {@code alignmentMode} is set to {@link #ALIGN_BOUNDS}, alignment 1991e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * is made between the edges of each component's raw 2001e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * view boundary: i.e. the area delimited by the component's: 2011e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * {@link android.view.View#getTop() top}, 2021e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * {@link android.view.View#getLeft() left}, 2031e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * {@link android.view.View#getBottom() bottom} and 2041e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * {@link android.view.View#getRight() right} properties. 2051e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * <p> 2061e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * For example, when {@code GridLayout} is in {@link #ALIGN_BOUNDS} mode, 2071e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * children that belong to a row group that uses {@link #TOP} alignment will 2081e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * all return the same value when their {@link android.view.View#getTop()} 2091e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * method is called. 2101e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * 2111e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * @see #setAlignmentMode(int) 2121e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne */ 2131e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne public static final int ALIGN_BOUNDS = 0; 2141e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne 2151e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne /** 2161e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * This constant is an {@link #setAlignmentMode(int) alignmentMode}. 2171e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * When the {@code alignmentMode} is set to {@link #ALIGN_MARGINS}, 2181e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * the bounds of each view are extended outwards, according 2191e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * to their margins, before the edges of the resulting rectangle are aligned. 2201e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * <p> 2211e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * For example, when {@code GridLayout} is in {@link #ALIGN_MARGINS} mode, 2221e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * the quantity {@code top - layoutParams.topMargin} is the same for all children that 2231e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * belong to a row group that uses {@link #TOP} alignment. 2241e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * 2251e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * @see #setAlignmentMode(int) 2261e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne */ 2271e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne public static final int ALIGN_MARGINS = 1; 2281e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne 2293f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Misc constants 2303f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 231f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne static final int MAX_SIZE = 100000; 232f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne static final int DEFAULT_CONTAINER_MARGIN = 0; 233d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne static final int UNINITIALIZED_HASH = 0; 234a2353621524472f71850a54b5a41188dd8281039Philip Milne static final Printer LOG_PRINTER = new LogPrinter(Log.DEBUG, GridLayout.class.getName()); 235a2353621524472f71850a54b5a41188dd8281039Philip Milne static final Printer NO_PRINTER = new Printer() { 236a2353621524472f71850a54b5a41188dd8281039Philip Milne @Override 237a2353621524472f71850a54b5a41188dd8281039Philip Milne public void println(String x) { 238a2353621524472f71850a54b5a41188dd8281039Philip Milne } 239a2353621524472f71850a54b5a41188dd8281039Philip Milne }; 2403f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2413f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Defaults 2423f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2433f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private static final int DEFAULT_ORIENTATION = HORIZONTAL; 2443f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private static final int DEFAULT_COUNT = UNDEFINED; 2453f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private static final boolean DEFAULT_USE_DEFAULT_MARGINS = false; 246899d5922870c78e0e663bc5661849eb468afc984Philip Milne private static final boolean DEFAULT_ORDER_PRESERVED = true; 2471e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne private static final int DEFAULT_ALIGNMENT_MODE = ALIGN_MARGINS; 2483f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2493f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // TypedArray indices 2503f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 251b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int ORIENTATION = R.styleable.GridLayout_orientation; 252b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int ROW_COUNT = R.styleable.GridLayout_rowCount; 253b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int COLUMN_COUNT = R.styleable.GridLayout_columnCount; 254b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int USE_DEFAULT_MARGINS = R.styleable.GridLayout_useDefaultMargins; 255b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int ALIGNMENT_MODE = R.styleable.GridLayout_alignmentMode; 256b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int ROW_ORDER_PRESERVED = R.styleable.GridLayout_rowOrderPreserved; 257b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int COLUMN_ORDER_PRESERVED = R.styleable.GridLayout_columnOrderPreserved; 2583f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Instance variables 2603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 261465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell final Axis mHorizontalAxis = new Axis(true); 262465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell final Axis mVerticalAxis = new Axis(false); 263465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell int mOrientation = DEFAULT_ORIENTATION; 264465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell boolean mUseDefaultMargins = DEFAULT_USE_DEFAULT_MARGINS; 265465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell int mAlignmentMode = DEFAULT_ALIGNMENT_MODE; 266465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell int mDefaultGap; 267465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell int mLastLayoutParamsHashCode = UNINITIALIZED_HASH; 268465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell Printer mPrinter = LOG_PRINTER; 269aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 2703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Constructors 2713f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 272d6479ec5eec13914f656f6be996d95fe1610fd57Alan Viverette public GridLayout(Context context) { 273d6479ec5eec13914f656f6be996d95fe1610fd57Alan Viverette this(context, null); 274d6479ec5eec13914f656f6be996d95fe1610fd57Alan Viverette } 275d6479ec5eec13914f656f6be996d95fe1610fd57Alan Viverette 276d6479ec5eec13914f656f6be996d95fe1610fd57Alan Viverette public GridLayout(Context context, AttributeSet attrs) { 277d6479ec5eec13914f656f6be996d95fe1610fd57Alan Viverette this(context, attrs, 0); 278d6479ec5eec13914f656f6be996d95fe1610fd57Alan Viverette } 279d6479ec5eec13914f656f6be996d95fe1610fd57Alan Viverette 280617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette public GridLayout(Context context, AttributeSet attrs, int defStyleAttr) { 281617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette this(context, attrs, defStyleAttr, 0); 282617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette } 283617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette 284617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette public GridLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 285617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette super(context, attrs, defStyleAttr, defStyleRes); 286465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell mDefaultGap = context.getResources().getDimensionPixelOffset(R.dimen.default_gap); 287617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette final TypedArray a = context.obtainStyledAttributes( 288617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette attrs, R.styleable.GridLayout, defStyleAttr, defStyleRes); 2893f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne try { 2901e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne setRowCount(a.getInt(ROW_COUNT, DEFAULT_COUNT)); 2911e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne setColumnCount(a.getInt(COLUMN_COUNT, DEFAULT_COUNT)); 2925125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne setOrientation(a.getInt(ORIENTATION, DEFAULT_ORIENTATION)); 2935125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne setUseDefaultMargins(a.getBoolean(USE_DEFAULT_MARGINS, DEFAULT_USE_DEFAULT_MARGINS)); 2945125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne setAlignmentMode(a.getInt(ALIGNMENT_MODE, DEFAULT_ALIGNMENT_MODE)); 2953f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne setRowOrderPreserved(a.getBoolean(ROW_ORDER_PRESERVED, DEFAULT_ORDER_PRESERVED)); 2963f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne setColumnOrderPreserved(a.getBoolean(COLUMN_ORDER_PRESERVED, DEFAULT_ORDER_PRESERVED)); 2973f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } finally { 2983f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne a.recycle(); 2993f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 3003f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 3013f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 3023f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Implementation 3033f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 3043f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 3053f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Returns the current orientation. 3063f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 3077fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * @return either {@link #HORIZONTAL} or {@link #VERTICAL} 3083f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 3093f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see #setOrientation(int) 3103f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 3113f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_orientation 3123f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 313d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbye @Orientation 3143f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public int getOrientation() { 315465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell return mOrientation; 3163f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 3173f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 3183f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 319b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * 320b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * GridLayout uses the orientation property for two purposes: 321b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * <ul> 322b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * <li> 323b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * To control the 'direction' in which default row/column indices are generated 324b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * when they are not specified in a component's layout parameters. 325b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * </li> 326b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * <li> 327b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * To control which axis should be processed first during the layout operation: 328b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * when orientation is {@link #HORIZONTAL} the horizontal axis is laid out first. 329b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * </li> 330b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * </ul> 331b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * 332b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * The order in which axes are laid out is important if, for example, the height of 333b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * one of GridLayout's children is dependent on its width - and its width is, in turn, 334b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * dependent on the widths of other components. 335b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * <p> 336b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * If your layout contains a {@link TextView} (or derivative: 337b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * {@code Button}, {@code EditText}, {@code CheckBox}, etc.) which is 338b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * in multi-line mode (the default) it is normally best to leave GridLayout's 339b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * orientation as {@code HORIZONTAL} - because {@code TextView} is capable of 340b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * deriving its height for a given width, but not the other way around. 341b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * <p> 342b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * Other than the effects above, orientation does not affect the actual layout operation of 343b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * GridLayout, so it's fine to leave GridLayout in {@code HORIZONTAL} mode even if 344b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne * the height of the intended layout greatly exceeds its width. 3457fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * <p> 3467fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * The default value of this property is {@link #HORIZONTAL}. 3473f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 3487fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * @param orientation either {@link #HORIZONTAL} or {@link #VERTICAL} 3493f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 3503f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see #getOrientation() 3513f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 3523f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_orientation 3533f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 354d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbye public void setOrientation(@Orientation int orientation) { 355465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell if (this.mOrientation != orientation) { 356465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell this.mOrientation = orientation; 357f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne invalidateStructure(); 3583f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne requestLayout(); 3593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 3603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 3613f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 3623f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 3633f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Returns the current number of rows. This is either the last value that was set 3643f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * with {@link #setRowCount(int)} or, if no such value was set, the maximum 36593cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * value of each the upper bounds defined in {@link LayoutParams#rowSpec}. 3663f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 3673f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @return the current number of rows 3683f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 3693f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see #setRowCount(int) 37093cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * @see LayoutParams#rowSpec 3713f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 3723f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_rowCount 3733f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 3743f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public int getRowCount() { 375465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell return mVerticalAxis.getCount(); 3763f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 3773f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 3783f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 379f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * RowCount is used only to generate default row/column indices when 380f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * they are not specified by a component's layout parameters. 3813f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 3827fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * @param rowCount the number of rows 3833f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 3843f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see #getRowCount() 38593cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * @see LayoutParams#rowSpec 3863f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 3873f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_rowCount 3883f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 3893f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public void setRowCount(int rowCount) { 390465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell mVerticalAxis.setCount(rowCount); 391f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne invalidateStructure(); 392f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne requestLayout(); 3933f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 3943f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 3953f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 3963f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Returns the current number of columns. This is either the last value that was set 3973f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * with {@link #setColumnCount(int)} or, if no such value was set, the maximum 39893cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * value of each the upper bounds defined in {@link LayoutParams#columnSpec}. 3993f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 4003f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @return the current number of columns 4013f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 4023f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see #setColumnCount(int) 40393cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * @see LayoutParams#columnSpec 4043f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 4053f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_columnCount 4063f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 4073f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public int getColumnCount() { 408465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell return mHorizontalAxis.getCount(); 4093f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 4103f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 4113f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 412f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * ColumnCount is used only to generate default column/column indices when 413f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * they are not specified by a component's layout parameters. 4143f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 4153f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @param columnCount the number of columns. 4163f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 4173f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see #getColumnCount() 41893cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * @see LayoutParams#columnSpec 4193f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 4203f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_columnCount 4213f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 4223f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public void setColumnCount(int columnCount) { 423465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell mHorizontalAxis.setCount(columnCount); 424f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne invalidateStructure(); 425f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne requestLayout(); 4263f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 4273f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 4283f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 4293f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Returns whether or not this GridLayout will allocate default margins when no 4303f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * corresponding layout parameters are defined. 4313f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 4327fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * @return {@code true} if default margins should be allocated 4333f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 4343f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see #setUseDefaultMargins(boolean) 4353f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 4363f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_useDefaultMargins 4373f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 4383f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public boolean getUseDefaultMargins() { 439465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell return mUseDefaultMargins; 4403f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 4413f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 4423f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 4437fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * When {@code true}, GridLayout allocates default margins around children 4443f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * based on the child's visual characteristics. Each of the 4453f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * margins so defined may be independently overridden by an assignment 4463f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * to the appropriate layout parameter. 4473f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <p> 4487fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * When {@code false}, the default value of all margins is zero. 449aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * <p> 4507fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * When setting to {@code true}, consider setting the value of the 4511e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * {@link #setAlignmentMode(int) alignmentMode} 4521e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * property to {@link #ALIGN_BOUNDS}. 4537fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * <p> 4547fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * The default value of this property is {@code false}. 4553f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 4567fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * @param useDefaultMargins use {@code true} to make GridLayout allocate default margins 4573f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 4583f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see #getUseDefaultMargins() 4591e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * @see #setAlignmentMode(int) 4603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 4613f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see MarginLayoutParams#leftMargin 4623f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see MarginLayoutParams#topMargin 4633f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see MarginLayoutParams#rightMargin 4643f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see MarginLayoutParams#bottomMargin 4653f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 4663f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_useDefaultMargins 4673f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 4683f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public void setUseDefaultMargins(boolean useDefaultMargins) { 469465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell this.mUseDefaultMargins = useDefaultMargins; 470aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne requestLayout(); 471aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne } 472aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 473aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne /** 4741e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * Returns the alignment mode. 4751e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * 4761e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * @return the alignment mode; either {@link #ALIGN_BOUNDS} or {@link #ALIGN_MARGINS} 477aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * 4781e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * @see #ALIGN_BOUNDS 4791e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * @see #ALIGN_MARGINS 480aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * 4811e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * @see #setAlignmentMode(int) 482aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * 4831e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * @attr ref android.R.styleable#GridLayout_alignmentMode 484aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne */ 485d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbye @AlignmentMode 4861e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne public int getAlignmentMode() { 487465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell return mAlignmentMode; 488aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne } 489aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 490aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne /** 4911e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * Sets the alignment mode to be used for all of the alignments between the 4921e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * children of this container. 4937fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * <p> 4941e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * The default value of this property is {@link #ALIGN_MARGINS}. 4951e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * 4961e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * @param alignmentMode either {@link #ALIGN_BOUNDS} or {@link #ALIGN_MARGINS} 497aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * 4981e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * @see #ALIGN_BOUNDS 4991e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * @see #ALIGN_MARGINS 500aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * 5011e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * @see #getAlignmentMode() 502aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * 5031e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * @attr ref android.R.styleable#GridLayout_alignmentMode 504aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne */ 505d9273d6f289d9b55da3fd0db2f659fdfb48106a8Tor Norbye public void setAlignmentMode(@AlignmentMode int alignmentMode) { 506465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell this.mAlignmentMode = alignmentMode; 507aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne requestLayout(); 5083f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 5093f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 5103f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 5113f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Returns whether or not row boundaries are ordered by their grid indices. 5123f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 5137fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * @return {@code true} if row boundaries must appear in the order of their indices, 5147fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * {@code false} otherwise 5153f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 5163f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see #setRowOrderPreserved(boolean) 5173f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 5183f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_rowOrderPreserved 5193f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 5203f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public boolean isRowOrderPreserved() { 521465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell return mVerticalAxis.isOrderPreserved(); 5223f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 5233f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 5243f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 5257fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * When this property is {@code true}, GridLayout is forced to place the row boundaries 526aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * so that their associated grid indices are in ascending order in the view. 5273f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <p> 528899d5922870c78e0e663bc5661849eb468afc984Philip Milne * When this property is {@code false} GridLayout is at liberty to place the vertical row 529899d5922870c78e0e663bc5661849eb468afc984Philip Milne * boundaries in whatever order best fits the given constraints. 5307fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * <p> 531899d5922870c78e0e663bc5661849eb468afc984Philip Milne * The default value of this property is {@code true}. 5327fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne 5337fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * @param rowOrderPreserved {@code true} to force GridLayout to respect the order 5347fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * of row boundaries 5353f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 5363f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see #isRowOrderPreserved() 5373f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 5383f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_rowOrderPreserved 5393f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 5403f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public void setRowOrderPreserved(boolean rowOrderPreserved) { 541465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell mVerticalAxis.setOrderPreserved(rowOrderPreserved); 542aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne invalidateStructure(); 543aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne requestLayout(); 5443f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 5453f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 5463f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 5473f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Returns whether or not column boundaries are ordered by their grid indices. 5483f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 5497fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * @return {@code true} if column boundaries must appear in the order of their indices, 5507fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * {@code false} otherwise 5513f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 5523f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see #setColumnOrderPreserved(boolean) 5533f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 5543f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_columnOrderPreserved 5553f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 5563f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public boolean isColumnOrderPreserved() { 557465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell return mHorizontalAxis.isOrderPreserved(); 5583f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 5593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 5603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 5617fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * When this property is {@code true}, GridLayout is forced to place the column boundaries 562aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * so that their associated grid indices are in ascending order in the view. 5633f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <p> 564899d5922870c78e0e663bc5661849eb468afc984Philip Milne * When this property is {@code false} GridLayout is at liberty to place the horizontal column 565899d5922870c78e0e663bc5661849eb468afc984Philip Milne * boundaries in whatever order best fits the given constraints. 5667fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * <p> 567899d5922870c78e0e663bc5661849eb468afc984Philip Milne * The default value of this property is {@code true}. 5683f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 5697fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * @param columnOrderPreserved use {@code true} to force GridLayout to respect the order 5703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * of column boundaries. 5713f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 5723f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see #isColumnOrderPreserved() 5733f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 5743f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_columnOrderPreserved 5753f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 5763f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public void setColumnOrderPreserved(boolean columnOrderPreserved) { 577465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell mHorizontalAxis.setOrderPreserved(columnOrderPreserved); 578aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne invalidateStructure(); 579aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne requestLayout(); 5803f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 5813f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 582211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne /** 583211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne * Return the printer that will log diagnostics from this layout. 584211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne * 585211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne * @see #setPrinter(android.util.Printer) 586211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne * 587211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne * @return the printer associated with this view 588465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell * 589465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell * @hide 590211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne */ 591211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne public Printer getPrinter() { 592465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell return mPrinter; 593211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne } 594211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne 595211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne /** 596211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne * Set the printer that will log diagnostics from this layout. 597211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne * The default value is created by {@link android.util.LogPrinter}. 598211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne * 599211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne * @param printer the printer associated with this layout 600211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne * 601211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne * @see #getPrinter() 602465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell * 603465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell * @hide 604211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne */ 605211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne public void setPrinter(Printer printer) { 606465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell this.mPrinter = (printer == null) ? NO_PRINTER : printer; 607211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne } 608211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne 6095125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne // Static utility methods 6105125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne 611f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne static int max2(int[] a, int valueIfEmpty) { 61251f17d54613c33638c8a2da8affcd9ba35994cb3Philip Milne int result = valueIfEmpty; 61351f17d54613c33638c8a2da8affcd9ba35994cb3Philip Milne for (int i = 0, N = a.length; i < N; i++) { 61451f17d54613c33638c8a2da8affcd9ba35994cb3Philip Milne result = Math.max(result, a[i]); 61551f17d54613c33638c8a2da8affcd9ba35994cb3Philip Milne } 61651f17d54613c33638c8a2da8affcd9ba35994cb3Philip Milne return result; 61751f17d54613c33638c8a2da8affcd9ba35994cb3Philip Milne } 61851f17d54613c33638c8a2da8affcd9ba35994cb3Philip Milne 619899d5922870c78e0e663bc5661849eb468afc984Philip Milne @SuppressWarnings("unchecked") 620f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne static <T> T[] append(T[] a, T[] b) { 62148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne T[] result = (T[]) Array.newInstance(a.getClass().getComponentType(), a.length + b.length); 62248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne System.arraycopy(a, 0, result, 0, a.length); 62348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne System.arraycopy(b, 0, result, a.length, b.length); 6243f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return result; 6253f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 6263f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 627f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne static Alignment getAlignment(int gravity, boolean horizontal) { 6285125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne int mask = horizontal ? HORIZONTAL_GRAVITY_MASK : VERTICAL_GRAVITY_MASK; 6295125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne int shift = horizontal ? AXIS_X_SHIFT : AXIS_Y_SHIFT; 6305125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne int flags = (gravity & mask) >> shift; 6315125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne switch (flags) { 6325125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne case (AXIS_SPECIFIED | AXIS_PULL_BEFORE): 6331557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne return horizontal ? LEFT : TOP; 6345125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne case (AXIS_SPECIFIED | AXIS_PULL_AFTER): 6351557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne return horizontal ? RIGHT : BOTTOM; 6365125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne case (AXIS_SPECIFIED | AXIS_PULL_BEFORE | AXIS_PULL_AFTER): 6375125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne return FILL; 6385125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne case AXIS_SPECIFIED: 6395125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne return CENTER; 64047d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio case (AXIS_SPECIFIED | AXIS_PULL_BEFORE | RELATIVE_LAYOUT_DIRECTION): 64147d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio return START; 64247d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio case (AXIS_SPECIFIED | AXIS_PULL_AFTER | RELATIVE_LAYOUT_DIRECTION): 64347d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio return END; 6445125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne default: 6455125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne return UNDEFINED_ALIGNMENT; 6465125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne } 6475125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne } 6485125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne 6494c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne /** @noinspection UnusedParameters*/ 6501fd16378812792913a6aa6923acbec20037e09ffPhilip Milne private int getDefaultMargin(View c, boolean horizontal, boolean leading) { 651899d5922870c78e0e663bc5661849eb468afc984Philip Milne if (c.getClass() == Space.class) { 652899d5922870c78e0e663bc5661849eb468afc984Philip Milne return 0; 653899d5922870c78e0e663bc5661849eb468afc984Philip Milne } 654465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell return mDefaultGap / 2; 6553f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 6563f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 6571fd16378812792913a6aa6923acbec20037e09ffPhilip Milne private int getDefaultMargin(View c, boolean isAtEdge, boolean horizontal, boolean leading) { 6587b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne return /*isAtEdge ? DEFAULT_CONTAINER_MARGIN :*/ getDefaultMargin(c, horizontal, leading); 6593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 6603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 6617a23b49a8ceb07d3fa12c45fd42cd16131fd746aPhilip Milne private int getDefaultMargin(View c, LayoutParams p, boolean horizontal, boolean leading) { 662465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell if (!mUseDefaultMargins) { 6633f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return 0; 6643f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 66593cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne Spec spec = horizontal ? p.columnSpec : p.rowSpec; 666465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis; 66793cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne Interval span = spec.span; 66847d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio boolean leading1 = (horizontal && isLayoutRtl()) ? !leading : leading; 66947d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio boolean isAtEdge = leading1 ? (span.min == 0) : (span.max == axis.getCount()); 6703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 6711fd16378812792913a6aa6923acbec20037e09ffPhilip Milne return getDefaultMargin(c, isAtEdge, horizontal, leading); 6723f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 6733f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 674f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne int getMargin1(View view, boolean horizontal, boolean leading) { 6753f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne LayoutParams lp = getLayoutParams(view); 6763f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int margin = horizontal ? 677aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne (leading ? lp.leftMargin : lp.rightMargin) : 678aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne (leading ? lp.topMargin : lp.bottomMargin); 6797a23b49a8ceb07d3fa12c45fd42cd16131fd746aPhilip Milne return margin == UNDEFINED ? getDefaultMargin(view, lp, horizontal, leading) : margin; 6801fd16378812792913a6aa6923acbec20037e09ffPhilip Milne } 6811fd16378812792913a6aa6923acbec20037e09ffPhilip Milne 6824c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne private int getMargin(View view, boolean horizontal, boolean leading) { 683465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell if (mAlignmentMode == ALIGN_MARGINS) { 6844c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne return getMargin1(view, horizontal, leading); 6854c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne } else { 686465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis; 6874c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne int[] margins = leading ? axis.getLeadingMargins() : axis.getTrailingMargins(); 6884c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne LayoutParams lp = getLayoutParams(view); 6894c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; 6904c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne int index = leading ? spec.span.min : spec.span.max; 6914c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne return margins[index]; 6924c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne } 6934c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne } 6944c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne 6951fd16378812792913a6aa6923acbec20037e09ffPhilip Milne private int getTotalMargin(View child, boolean horizontal) { 6961fd16378812792913a6aa6923acbec20037e09ffPhilip Milne return getMargin(child, horizontal, true) + getMargin(child, horizontal, false); 6973f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 6983f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 699899d5922870c78e0e663bc5661849eb468afc984Philip Milne private static boolean fits(int[] a, int value, int start, int end) { 700899d5922870c78e0e663bc5661849eb468afc984Philip Milne if (end > a.length) { 701899d5922870c78e0e663bc5661849eb468afc984Philip Milne return false; 702899d5922870c78e0e663bc5661849eb468afc984Philip Milne } 703899d5922870c78e0e663bc5661849eb468afc984Philip Milne for (int i = start; i < end; i++) { 704899d5922870c78e0e663bc5661849eb468afc984Philip Milne if (a[i] > value) { 705899d5922870c78e0e663bc5661849eb468afc984Philip Milne return false; 7063f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 707899d5922870c78e0e663bc5661849eb468afc984Philip Milne } 708899d5922870c78e0e663bc5661849eb468afc984Philip Milne return true; 709899d5922870c78e0e663bc5661849eb468afc984Philip Milne } 7103f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 711899d5922870c78e0e663bc5661849eb468afc984Philip Milne private static void procrusteanFill(int[] a, int start, int end, int value) { 712899d5922870c78e0e663bc5661849eb468afc984Philip Milne int length = a.length; 713899d5922870c78e0e663bc5661849eb468afc984Philip Milne Arrays.fill(a, Math.min(start, length), Math.min(end, length), value); 714899d5922870c78e0e663bc5661849eb468afc984Philip Milne } 7153f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 716899d5922870c78e0e663bc5661849eb468afc984Philip Milne private static void setCellGroup(LayoutParams lp, int row, int rowSpan, int col, int colSpan) { 717899d5922870c78e0e663bc5661849eb468afc984Philip Milne lp.setRowSpecSpan(new Interval(row, row + rowSpan)); 718899d5922870c78e0e663bc5661849eb468afc984Philip Milne lp.setColumnSpecSpan(new Interval(col, col + colSpan)); 719899d5922870c78e0e663bc5661849eb468afc984Philip Milne } 7203f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 721899d5922870c78e0e663bc5661849eb468afc984Philip Milne // Logic to avert infinite loops by ensuring that the cells can be placed somewhere. 722899d5922870c78e0e663bc5661849eb468afc984Philip Milne private static int clip(Interval minorRange, boolean minorWasDefined, int count) { 723899d5922870c78e0e663bc5661849eb468afc984Philip Milne int size = minorRange.size(); 724899d5922870c78e0e663bc5661849eb468afc984Philip Milne if (count == 0) { 725899d5922870c78e0e663bc5661849eb468afc984Philip Milne return size; 726899d5922870c78e0e663bc5661849eb468afc984Philip Milne } 727899d5922870c78e0e663bc5661849eb468afc984Philip Milne int min = minorWasDefined ? min(minorRange.min, count) : 0; 728899d5922870c78e0e663bc5661849eb468afc984Philip Milne return min(size, count - min); 729899d5922870c78e0e663bc5661849eb468afc984Philip Milne } 730f474870fe1189f73cf8ffbaba9e524ef194b5043Philip Milne 731899d5922870c78e0e663bc5661849eb468afc984Philip Milne // install default indices for cells that don't define them 732899d5922870c78e0e663bc5661849eb468afc984Philip Milne private void validateLayoutParams() { 733465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell final boolean horizontal = (mOrientation == HORIZONTAL); 734465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell final Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis; 735782c04b864dc5f71f7ec0c8e36cbaf5b838dc69cJim Miller final int count = (axis.definedCount != UNDEFINED) ? axis.definedCount : 0; 736f474870fe1189f73cf8ffbaba9e524ef194b5043Philip Milne 737899d5922870c78e0e663bc5661849eb468afc984Philip Milne int major = 0; 738899d5922870c78e0e663bc5661849eb468afc984Philip Milne int minor = 0; 739899d5922870c78e0e663bc5661849eb468afc984Philip Milne int[] maxSizes = new int[count]; 740f474870fe1189f73cf8ffbaba9e524ef194b5043Philip Milne 741899d5922870c78e0e663bc5661849eb468afc984Philip Milne for (int i = 0, N = getChildCount(); i < N; i++) { 742d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne LayoutParams lp = (LayoutParams) getChildAt(i).getLayoutParams(); 743899d5922870c78e0e663bc5661849eb468afc984Philip Milne 744f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final Spec majorSpec = horizontal ? lp.rowSpec : lp.columnSpec; 745f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final Interval majorRange = majorSpec.span; 746f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final boolean majorWasDefined = majorSpec.startDefined; 747899d5922870c78e0e663bc5661849eb468afc984Philip Milne final int majorSpan = majorRange.size(); 748899d5922870c78e0e663bc5661849eb468afc984Philip Milne if (majorWasDefined) { 749899d5922870c78e0e663bc5661849eb468afc984Philip Milne major = majorRange.min; 750899d5922870c78e0e663bc5661849eb468afc984Philip Milne } 751899d5922870c78e0e663bc5661849eb468afc984Philip Milne 752f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final Spec minorSpec = horizontal ? lp.columnSpec : lp.rowSpec; 753f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final Interval minorRange = minorSpec.span; 754f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final boolean minorWasDefined = minorSpec.startDefined; 755899d5922870c78e0e663bc5661849eb468afc984Philip Milne final int minorSpan = clip(minorRange, minorWasDefined, count); 756899d5922870c78e0e663bc5661849eb468afc984Philip Milne if (minorWasDefined) { 757899d5922870c78e0e663bc5661849eb468afc984Philip Milne minor = minorRange.min; 758899d5922870c78e0e663bc5661849eb468afc984Philip Milne } 759899d5922870c78e0e663bc5661849eb468afc984Philip Milne 760899d5922870c78e0e663bc5661849eb468afc984Philip Milne if (count != 0) { 761899d5922870c78e0e663bc5661849eb468afc984Philip Milne // Find suitable row/col values when at least one is undefined. 762899d5922870c78e0e663bc5661849eb468afc984Philip Milne if (!majorWasDefined || !minorWasDefined) { 763899d5922870c78e0e663bc5661849eb468afc984Philip Milne while (!fits(maxSizes, major, minor, minor + minorSpan)) { 764899d5922870c78e0e663bc5661849eb468afc984Philip Milne if (minorWasDefined) { 765899d5922870c78e0e663bc5661849eb468afc984Philip Milne major++; 766899d5922870c78e0e663bc5661849eb468afc984Philip Milne } else { 767899d5922870c78e0e663bc5661849eb468afc984Philip Milne if (minor + minorSpan <= count) { 768899d5922870c78e0e663bc5661849eb468afc984Philip Milne minor++; 769899d5922870c78e0e663bc5661849eb468afc984Philip Milne } else { 770899d5922870c78e0e663bc5661849eb468afc984Philip Milne minor = 0; 771899d5922870c78e0e663bc5661849eb468afc984Philip Milne major++; 772899d5922870c78e0e663bc5661849eb468afc984Philip Milne } 773f474870fe1189f73cf8ffbaba9e524ef194b5043Philip Milne } 774f474870fe1189f73cf8ffbaba9e524ef194b5043Philip Milne } 7753f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 776899d5922870c78e0e663bc5661849eb468afc984Philip Milne procrusteanFill(maxSizes, minor, minor + minorSpan, major + majorSpan); 7773f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 778899d5922870c78e0e663bc5661849eb468afc984Philip Milne 779899d5922870c78e0e663bc5661849eb468afc984Philip Milne if (horizontal) { 780899d5922870c78e0e663bc5661849eb468afc984Philip Milne setCellGroup(lp, major, majorSpan, minor, minorSpan); 781899d5922870c78e0e663bc5661849eb468afc984Philip Milne } else { 782899d5922870c78e0e663bc5661849eb468afc984Philip Milne setCellGroup(lp, minor, minorSpan, major, majorSpan); 783899d5922870c78e0e663bc5661849eb468afc984Philip Milne } 784899d5922870c78e0e663bc5661849eb468afc984Philip Milne 785899d5922870c78e0e663bc5661849eb468afc984Philip Milne minor = minor + minorSpan; 786899d5922870c78e0e663bc5661849eb468afc984Philip Milne } 7873f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 7883f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 7893f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private void invalidateStructure() { 790465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell mLastLayoutParamsHashCode = UNINITIALIZED_HASH; 791465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell mHorizontalAxis.invalidateStructure(); 792465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell mVerticalAxis.invalidateStructure(); 793899d5922870c78e0e663bc5661849eb468afc984Philip Milne // This can end up being done twice. Better twice than not at all. 7943f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne invalidateValues(); 7953f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 7963f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 7973f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private void invalidateValues() { 798aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne // Need null check because requestLayout() is called in View's initializer, 799aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne // before we are set up. 800465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell if (mHorizontalAxis != null && mVerticalAxis != null) { 801465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell mHorizontalAxis.invalidateValues(); 802465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell mVerticalAxis.invalidateValues(); 803aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne } 8043f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 8053f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 806d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne /** @hide */ 807d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne @Override 808d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne protected void onSetLayoutParams(View child, ViewGroup.LayoutParams layoutParams) { 809d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne super.onSetLayoutParams(child, layoutParams); 8100f57cea8989cc03bcae19b621096e50b035b7308Philip Milne 8110f57cea8989cc03bcae19b621096e50b035b7308Philip Milne if (!checkLayoutParams(layoutParams)) { 8120f57cea8989cc03bcae19b621096e50b035b7308Philip Milne handleInvalidParams("supplied LayoutParams are of the wrong type"); 8130f57cea8989cc03bcae19b621096e50b035b7308Philip Milne } 8140f57cea8989cc03bcae19b621096e50b035b7308Philip Milne 815d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne invalidateStructure(); 8163f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 8173f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 818f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final LayoutParams getLayoutParams(View c) { 819d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne return (LayoutParams) c.getLayoutParams(); 8203f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 8213f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 8220f57cea8989cc03bcae19b621096e50b035b7308Philip Milne private static void handleInvalidParams(String msg) { 8230f57cea8989cc03bcae19b621096e50b035b7308Philip Milne throw new IllegalArgumentException(msg + ". "); 8240f57cea8989cc03bcae19b621096e50b035b7308Philip Milne } 8250f57cea8989cc03bcae19b621096e50b035b7308Philip Milne 8260f57cea8989cc03bcae19b621096e50b035b7308Philip Milne private void checkLayoutParams(LayoutParams lp, boolean horizontal) { 8270f57cea8989cc03bcae19b621096e50b035b7308Philip Milne String groupName = horizontal ? "column" : "row"; 8280f57cea8989cc03bcae19b621096e50b035b7308Philip Milne Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; 8290f57cea8989cc03bcae19b621096e50b035b7308Philip Milne Interval span = spec.span; 8300f57cea8989cc03bcae19b621096e50b035b7308Philip Milne if (span.min != UNDEFINED && span.min < 0) { 8310f57cea8989cc03bcae19b621096e50b035b7308Philip Milne handleInvalidParams(groupName + " indices must be positive"); 8320f57cea8989cc03bcae19b621096e50b035b7308Philip Milne } 833465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis; 8340f57cea8989cc03bcae19b621096e50b035b7308Philip Milne int count = axis.definedCount; 8350f57cea8989cc03bcae19b621096e50b035b7308Philip Milne if (count != UNDEFINED) { 8360f57cea8989cc03bcae19b621096e50b035b7308Philip Milne if (span.max > count) { 8370f57cea8989cc03bcae19b621096e50b035b7308Philip Milne handleInvalidParams(groupName + 8380f57cea8989cc03bcae19b621096e50b035b7308Philip Milne " indices (start + span) mustn't exceed the " + groupName + " count"); 8390f57cea8989cc03bcae19b621096e50b035b7308Philip Milne } 8400f57cea8989cc03bcae19b621096e50b035b7308Philip Milne if (span.size() > count) { 8410f57cea8989cc03bcae19b621096e50b035b7308Philip Milne handleInvalidParams(groupName + " span mustn't exceed the " + groupName + " count"); 8420f57cea8989cc03bcae19b621096e50b035b7308Philip Milne } 8430f57cea8989cc03bcae19b621096e50b035b7308Philip Milne } 8440f57cea8989cc03bcae19b621096e50b035b7308Philip Milne } 8450f57cea8989cc03bcae19b621096e50b035b7308Philip Milne 8460f57cea8989cc03bcae19b621096e50b035b7308Philip Milne @Override 8470f57cea8989cc03bcae19b621096e50b035b7308Philip Milne protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { 8480f57cea8989cc03bcae19b621096e50b035b7308Philip Milne if (!(p instanceof LayoutParams)) { 8490f57cea8989cc03bcae19b621096e50b035b7308Philip Milne return false; 8500f57cea8989cc03bcae19b621096e50b035b7308Philip Milne } 8510f57cea8989cc03bcae19b621096e50b035b7308Philip Milne LayoutParams lp = (LayoutParams) p; 8520f57cea8989cc03bcae19b621096e50b035b7308Philip Milne 8530f57cea8989cc03bcae19b621096e50b035b7308Philip Milne checkLayoutParams(lp, true); 8540f57cea8989cc03bcae19b621096e50b035b7308Philip Milne checkLayoutParams(lp, false); 8550f57cea8989cc03bcae19b621096e50b035b7308Philip Milne 8560f57cea8989cc03bcae19b621096e50b035b7308Philip Milne return true; 8570f57cea8989cc03bcae19b621096e50b035b7308Philip Milne } 8580f57cea8989cc03bcae19b621096e50b035b7308Philip Milne 8593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @Override 8603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne protected LayoutParams generateDefaultLayoutParams() { 8613f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return new LayoutParams(); 8623f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 8633f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 8643f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @Override 8653f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public LayoutParams generateLayoutParams(AttributeSet attrs) { 8665125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne return new LayoutParams(getContext(), attrs); 8673f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 8683f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 8693f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @Override 8703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { 8713f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return new LayoutParams(p); 8723f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 8733f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 8743f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Draw grid 8753f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 8763f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private void drawLine(Canvas graphics, int x1, int y1, int x2, int y2, Paint paint) { 87747d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio if (isLayoutRtl()) { 87847d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio int width = getWidth(); 8797b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne graphics.drawLine(width - x1, y1, width - x2, y2, paint); 88047d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio } else { 8817b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne graphics.drawLine(x1, y1, x2, y2, paint); 88247d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio } 8833f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 8843f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 88510ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne /** 88610ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne * @hide 88710ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne */ 88810ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne @Override 8897b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne protected void onDebugDrawMargins(Canvas canvas, Paint paint) { 89010ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne // Apply defaults, so as to remove UNDEFINED values 89110ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne LayoutParams lp = new LayoutParams(); 89210ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne for (int i = 0; i < getChildCount(); i++) { 89310ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne View c = getChildAt(i); 89410ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne lp.setMargins( 8957b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne getMargin1(c, true, true), 8967b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne getMargin1(c, false, true), 8977b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne getMargin1(c, true, false), 8987b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne getMargin1(c, false, false)); 8997b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne lp.onDebugDraw(c, canvas, paint); 90010ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne } 901b559976a50c34848d602cc7138859507a379893cPhilip Milne } 902b559976a50c34848d602cc7138859507a379893cPhilip Milne 90310ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne /** 90410ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne * @hide 90510ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne */ 9063f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @Override 90710ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne protected void onDebugDraw(Canvas canvas) { 90810ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne Paint paint = new Paint(); 90910ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne paint.setStyle(Paint.Style.STROKE); 91010ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne paint.setColor(Color.argb(50, 255, 255, 255)); 91110ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne 9127b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne Insets insets = getOpticalInsets(); 9137b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne 9147b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne int top = getPaddingTop() + insets.top; 9157b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne int left = getPaddingLeft() + insets.left; 9167b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne int right = getWidth() - getPaddingRight() - insets.right; 9177b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne int bottom = getHeight() - getPaddingBottom() - insets.bottom; 9187b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne 919465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell int[] xs = mHorizontalAxis.locations; 92010ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne if (xs != null) { 92110ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne for (int i = 0, length = xs.length; i < length; i++) { 9227b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne int x = left + xs[i]; 9237b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne drawLine(canvas, x, top, x, bottom, paint); 924b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne } 92510ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne } 926b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne 927465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell int[] ys = mVerticalAxis.locations; 92810ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne if (ys != null) { 92910ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne for (int i = 0, length = ys.length; i < length; i++) { 9307b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne int y = top + ys[i]; 9317b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne drawLine(canvas, left, y, right, y, paint); 9323f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 9333f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 93410ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne 93510ca24a97cefc14fca1b26f59e627f487b3b108bPhilip Milne super.onDebugDraw(canvas); 9363f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 9373f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 9383f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @Override 9396690d018b9c08757d20182170948a3cba961e84cAdam Powell public void onViewAdded(View child) { 940f51d91c3ab232154b6c00d7f71377ff2421f79bfPhilip Milne super.onViewAdded(child); 9413f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne invalidateStructure(); 9423f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 9433f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 9443f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @Override 9456690d018b9c08757d20182170948a3cba961e84cAdam Powell public void onViewRemoved(View child) { 946f51d91c3ab232154b6c00d7f71377ff2421f79bfPhilip Milne super.onViewRemoved(child); 947b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne invalidateStructure(); 948350f0a63c9e785304063a95a6df9e128a67ec64fPhilip Milne } 949350f0a63c9e785304063a95a6df9e128a67ec64fPhilip Milne 950350f0a63c9e785304063a95a6df9e128a67ec64fPhilip Milne /** 951350f0a63c9e785304063a95a6df9e128a67ec64fPhilip Milne * We need to call invalidateStructure() when a child's GONE flag changes state. 952350f0a63c9e785304063a95a6df9e128a67ec64fPhilip Milne * This implementation is a catch-all, invalidating on any change in the visibility flags. 953350f0a63c9e785304063a95a6df9e128a67ec64fPhilip Milne * 954350f0a63c9e785304063a95a6df9e128a67ec64fPhilip Milne * @hide 955350f0a63c9e785304063a95a6df9e128a67ec64fPhilip Milne */ 956350f0a63c9e785304063a95a6df9e128a67ec64fPhilip Milne @Override 9570d29936ec3b5545a415e8d032150ea987aab36e3Chet Haase protected void onChildVisibilityChanged(View child, int oldVisibility, int newVisibility) { 9580d29936ec3b5545a415e8d032150ea987aab36e3Chet Haase super.onChildVisibilityChanged(child, oldVisibility, newVisibility); 9590d29936ec3b5545a415e8d032150ea987aab36e3Chet Haase if (oldVisibility == GONE || newVisibility == GONE) { 960a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne invalidateStructure(); 9610d29936ec3b5545a415e8d032150ea987aab36e3Chet Haase } 962b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne } 963b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne 964d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne private int computeLayoutParamsHashCode() { 965d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne int result = 1; 966d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne for (int i = 0, N = getChildCount(); i < N; i++) { 967d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne View c = getChildAt(i); 968d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne if (c.getVisibility() == View.GONE) continue; 969d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne LayoutParams lp = (LayoutParams) c.getLayoutParams(); 970d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne result = 31 * result + lp.hashCode(); 971d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne } 972d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne return result; 973d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne } 9743f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 975edd69518ffec87fb7b1931e708678ef441152cdePhilip Milne private void consistencyCheck() { 976465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell if (mLastLayoutParamsHashCode == UNINITIALIZED_HASH) { 977edd69518ffec87fb7b1931e708678ef441152cdePhilip Milne validateLayoutParams(); 978465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell mLastLayoutParamsHashCode = computeLayoutParamsHashCode(); 979465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell } else if (mLastLayoutParamsHashCode != computeLayoutParamsHashCode()) { 980465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell mPrinter.println("The fields of some layout parameters were modified in between " 981211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne + "layout operations. Check the javadoc for GridLayout.LayoutParams#rowSpec."); 982edd69518ffec87fb7b1931e708678ef441152cdePhilip Milne invalidateStructure(); 983edd69518ffec87fb7b1931e708678ef441152cdePhilip Milne consistencyCheck(); 984d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne } 985b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne } 986b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne 987d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne // Measurement 988d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne 989e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne // Note: padding has already been removed from the supplied specs 9904a145d72622772b920f60195e80942058984259cPhilip Milne private void measureChildWithMargins2(View child, int parentWidthSpec, int parentHeightSpec, 991edd69518ffec87fb7b1931e708678ef441152cdePhilip Milne int childWidth, int childHeight) { 9924a145d72622772b920f60195e80942058984259cPhilip Milne int childWidthSpec = getChildMeasureSpec(parentWidthSpec, 993e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne getTotalMargin(child, true), childWidth); 9944a145d72622772b920f60195e80942058984259cPhilip Milne int childHeightSpec = getChildMeasureSpec(parentHeightSpec, 995e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne getTotalMargin(child, false), childHeight); 9964a145d72622772b920f60195e80942058984259cPhilip Milne child.measure(childWidthSpec, childHeightSpec); 997b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne } 998b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne 999e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne // Note: padding has already been removed from the supplied specs 10004a145d72622772b920f60195e80942058984259cPhilip Milne private void measureChildrenWithMargins(int widthSpec, int heightSpec, boolean firstPass) { 1001b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne for (int i = 0, N = getChildCount(); i < N; i++) { 1002b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne View c = getChildAt(i); 1003d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne if (c.getVisibility() == View.GONE) continue; 10044a145d72622772b920f60195e80942058984259cPhilip Milne LayoutParams lp = getLayoutParams(c); 10054a145d72622772b920f60195e80942058984259cPhilip Milne if (firstPass) { 10064a145d72622772b920f60195e80942058984259cPhilip Milne measureChildWithMargins2(c, widthSpec, heightSpec, lp.width, lp.height); 10074a145d72622772b920f60195e80942058984259cPhilip Milne } else { 1008465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell boolean horizontal = (mOrientation == HORIZONTAL); 1009ecab1178648670f2c72b47faf250040fcded3d13Philip Milne Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; 10106dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar if (spec.getAbsoluteAlignment(horizontal) == FILL) { 10114a145d72622772b920f60195e80942058984259cPhilip Milne Interval span = spec.span; 1012465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis; 10134a145d72622772b920f60195e80942058984259cPhilip Milne int[] locations = axis.getLocations(); 1014ecab1178648670f2c72b47faf250040fcded3d13Philip Milne int cellSize = locations[span.max] - locations[span.min]; 1015ecab1178648670f2c72b47faf250040fcded3d13Philip Milne int viewSize = cellSize - getTotalMargin(c, horizontal); 1016ecab1178648670f2c72b47faf250040fcded3d13Philip Milne if (horizontal) { 1017ecab1178648670f2c72b47faf250040fcded3d13Philip Milne measureChildWithMargins2(c, widthSpec, heightSpec, viewSize, lp.height); 10184a145d72622772b920f60195e80942058984259cPhilip Milne } else { 1019ecab1178648670f2c72b47faf250040fcded3d13Philip Milne measureChildWithMargins2(c, widthSpec, heightSpec, lp.width, viewSize); 10204a145d72622772b920f60195e80942058984259cPhilip Milne } 10214a145d72622772b920f60195e80942058984259cPhilip Milne } 10224a145d72622772b920f60195e80942058984259cPhilip Milne } 1023b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne } 1024b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne } 1025b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne 1026e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne static int adjust(int measureSpec, int delta) { 1027e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne return makeMeasureSpec( 1028e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne MeasureSpec.getSize(measureSpec + delta), MeasureSpec.getMode(measureSpec)); 1029e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne } 1030e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne 1031aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne @Override 1032aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne protected void onMeasure(int widthSpec, int heightSpec) { 1033edd69518ffec87fb7b1931e708678ef441152cdePhilip Milne consistencyCheck(); 1034d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne 10354a145d72622772b920f60195e80942058984259cPhilip Milne /** If we have been called by {@link View#measure(int, int)}, one of width or height 10364a145d72622772b920f60195e80942058984259cPhilip Milne * is likely to have changed. We must invalidate if so. */ 10374a145d72622772b920f60195e80942058984259cPhilip Milne invalidateValues(); 10384a145d72622772b920f60195e80942058984259cPhilip Milne 1039e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne int hPadding = getPaddingLeft() + getPaddingRight(); 1040e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne int vPadding = getPaddingTop() + getPaddingBottom(); 1041e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne 1042e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne int widthSpecSansPadding = adjust( widthSpec, -hPadding); 1043e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne int heightSpecSansPadding = adjust(heightSpec, -vPadding); 10444a145d72622772b920f60195e80942058984259cPhilip Milne 1045e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne measureChildrenWithMargins(widthSpecSansPadding, heightSpecSansPadding, true); 1046e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne 1047e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne int widthSansPadding; 1048e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne int heightSansPadding; 10494a145d72622772b920f60195e80942058984259cPhilip Milne 10504a145d72622772b920f60195e80942058984259cPhilip Milne // Use the orientation property to decide which axis should be laid out first. 1051465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell if (mOrientation == HORIZONTAL) { 1052465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell widthSansPadding = mHorizontalAxis.getMeasure(widthSpecSansPadding); 1053e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne measureChildrenWithMargins(widthSpecSansPadding, heightSpecSansPadding, false); 1054465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell heightSansPadding = mVerticalAxis.getMeasure(heightSpecSansPadding); 10554a145d72622772b920f60195e80942058984259cPhilip Milne } else { 1056465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell heightSansPadding = mVerticalAxis.getMeasure(heightSpecSansPadding); 1057e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne measureChildrenWithMargins(widthSpecSansPadding, heightSpecSansPadding, false); 1058465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell widthSansPadding = mHorizontalAxis.getMeasure(widthSpecSansPadding); 10594a145d72622772b920f60195e80942058984259cPhilip Milne } 1060aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 1061e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne int measuredWidth = Math.max(widthSansPadding + hPadding, getSuggestedMinimumWidth()); 1062e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne int measuredHeight = Math.max(heightSansPadding + vPadding, getSuggestedMinimumHeight()); 106309e2d4d0d5101e2fc44909b83965465a7b90afabPhilip Milne 10643f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne setMeasuredDimension( 1065e0b85cd706da22d1eeeba92b842662d69090dbc5Philip Milne resolveSizeAndState(measuredWidth, widthSpec, 0), 106609e2d4d0d5101e2fc44909b83965465a7b90afabPhilip Milne resolveSizeAndState(measuredHeight, heightSpec, 0)); 10673f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 10683f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 106948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private int getMeasurement(View c, boolean horizontal) { 10707b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne return horizontal ? c.getMeasuredWidth() : c.getMeasuredHeight(); 10713f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 10723f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1073f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final int getMeasurementIncludingMargin(View c, boolean horizontal) { 1074d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne if (c.getVisibility() == View.GONE) { 10755125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne return 0; 10765125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne } 10774c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne return getMeasurement(c, horizontal) + getTotalMargin(c, horizontal); 1078aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne } 10793f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1080aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne @Override 1081aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne public void requestLayout() { 1082aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne super.requestLayout(); 1083aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne invalidateValues(); 1084aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne } 1085aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 10863f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Layout container 10873f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1088aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne /** 1089aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * {@inheritDoc} 1090aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne */ 1091aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne /* 1092aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne The layout operation is implemented by delegating the heavy lifting to the 1093aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne to the mHorizontalAxis and mVerticalAxis instances of the internal Axis class. 1094aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne Together they compute the locations of the vertical and horizontal lines of 1095aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne the grid (respectively!). 1096aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 1097aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne This method is then left with the simpler task of applying margins, gravity 1098aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne and sizing to each child view and then placing it in its cell. 1099aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne */ 11003f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @Override 110109e2d4d0d5101e2fc44909b83965465a7b90afabPhilip Milne protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 1102edd69518ffec87fb7b1931e708678ef441152cdePhilip Milne consistencyCheck(); 1103d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne 110409e2d4d0d5101e2fc44909b83965465a7b90afabPhilip Milne int targetWidth = right - left; 110509e2d4d0d5101e2fc44909b83965465a7b90afabPhilip Milne int targetHeight = bottom - top; 11063f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 11073f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int paddingLeft = getPaddingLeft(); 11083f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int paddingTop = getPaddingTop(); 11093f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int paddingRight = getPaddingRight(); 11103f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int paddingBottom = getPaddingBottom(); 11113f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1112465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell mHorizontalAxis.layout(targetWidth - paddingLeft - paddingRight); 1113465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell mVerticalAxis.layout(targetHeight - paddingTop - paddingBottom); 11143f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1115465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell int[] hLocations = mHorizontalAxis.getLocations(); 1116465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell int[] vLocations = mVerticalAxis.getLocations(); 11174c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne 1118b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne for (int i = 0, N = getChildCount(); i < N; i++) { 1119b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne View c = getChildAt(i); 1120d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne if (c.getVisibility() == View.GONE) continue; 1121b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne LayoutParams lp = getLayoutParams(c); 112293cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne Spec columnSpec = lp.columnSpec; 112393cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne Spec rowSpec = lp.rowSpec; 1124aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 112593cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne Interval colSpan = columnSpec.span; 112693cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne Interval rowSpan = rowSpec.span; 11273f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 11284c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne int x1 = hLocations[colSpan.min]; 11294c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne int y1 = vLocations[rowSpan.min]; 11303f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 11314c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne int x2 = hLocations[colSpan.max]; 11324c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne int y2 = vLocations[rowSpan.max]; 11333f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 11343f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int cellWidth = x2 - x1; 11353f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int cellHeight = y2 - y1; 11363f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 113748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne int pWidth = getMeasurement(c, true); 113848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne int pHeight = getMeasurement(c, false); 11393f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 11406dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar Alignment hAlign = columnSpec.getAbsoluteAlignment(true); 11416dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar Alignment vAlign = rowSpec.getAbsoluteAlignment(false); 1142aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 1143465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell Bounds boundsX = mHorizontalAxis.getGroupBounds().getValue(i); 1144465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell Bounds boundsY = mVerticalAxis.getGroupBounds().getValue(i); 11457fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne 11467fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne // Gravity offsets: the location of the alignment group relative to its cell group. 11476216e87fe8ad3273855233965b34049d22763e94Philip Milne int gravityOffsetX = hAlign.getGravityOffset(c, cellWidth - boundsX.size(true)); 11486216e87fe8ad3273855233965b34049d22763e94Philip Milne int gravityOffsetY = vAlign.getGravityOffset(c, cellHeight - boundsY.size(true)); 11496216e87fe8ad3273855233965b34049d22763e94Philip Milne 11506216e87fe8ad3273855233965b34049d22763e94Philip Milne int leftMargin = getMargin(c, true, true); 11514c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne int topMargin = getMargin(c, false, true); 11526216e87fe8ad3273855233965b34049d22763e94Philip Milne int rightMargin = getMargin(c, true, false); 11534c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne int bottomMargin = getMargin(c, false, false); 11547fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne 11551557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne int sumMarginsX = leftMargin + rightMargin; 11561557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne int sumMarginsY = topMargin + bottomMargin; 11571557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne 11584c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne // Alignment offsets: the location of the view relative to its alignment group. 11591557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne int alignmentOffsetX = boundsX.getOffset(this, c, hAlign, pWidth + sumMarginsX, true); 11601557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne int alignmentOffsetY = boundsY.getOffset(this, c, vAlign, pHeight + sumMarginsY, false); 11617fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne 11621557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne int width = hAlign.getSizeInCell(c, pWidth, cellWidth - sumMarginsX); 11631557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne int height = vAlign.getSizeInCell(c, pHeight, cellHeight - sumMarginsY); 11647fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne 11656216e87fe8ad3273855233965b34049d22763e94Philip Milne int dx = x1 + gravityOffsetX + alignmentOffsetX; 11663f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 11676216e87fe8ad3273855233965b34049d22763e94Philip Milne int cx = !isLayoutRtl() ? paddingLeft + leftMargin + dx : 11686216e87fe8ad3273855233965b34049d22763e94Philip Milne targetWidth - width - paddingRight - rightMargin - dx; 11696216e87fe8ad3273855233965b34049d22763e94Philip Milne int cy = paddingTop + y1 + gravityOffsetY + alignmentOffsetY + topMargin; 11703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1171899d5922870c78e0e663bc5661849eb468afc984Philip Milne if (width != c.getMeasuredWidth() || height != c.getMeasuredHeight()) { 1172899d5922870c78e0e663bc5661849eb468afc984Philip Milne c.measure(makeMeasureSpec(width, EXACTLY), makeMeasureSpec(height, EXACTLY)); 1173899d5922870c78e0e663bc5661849eb468afc984Philip Milne } 1174b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne c.layout(cx, cy, cx + width, cy + height); 11753f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 11763f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 11773f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 11788a78fd4d9572dff95432fcc4ba0e87563415b728Svetoslav Ganov @Override 1179a7bb6fbeab933326d58aa806d8194b7b13239d34Dianne Hackborn public CharSequence getAccessibilityClassName() { 1180a7bb6fbeab933326d58aa806d8194b7b13239d34Dianne Hackborn return GridLayout.class.getName(); 11818a78fd4d9572dff95432fcc4ba0e87563415b728Svetoslav Ganov } 11828a78fd4d9572dff95432fcc4ba0e87563415b728Svetoslav Ganov 11833f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Inner classes 11843f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1185aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne /* 1186aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne This internal class houses the algorithm for computing the locations of grid lines; 1187aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne along either the horizontal or vertical axis. A GridLayout uses two instances of this class - 1188aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne distinguished by the "horizontal" flag which is true for the horizontal axis and false 1189aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne for the vertical one. 1190aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne */ 1191f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final class Axis { 119248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private static final int NEW = 0; 11933f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private static final int PENDING = 1; 11943f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private static final int COMPLETE = 2; 11953f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 11963f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public final boolean horizontal; 11973f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1198f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public int definedCount = UNDEFINED; 11994a145d72622772b920f60195e80942058984259cPhilip Milne private int maxIndex = UNDEFINED; 12003f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 120193cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne PackedMap<Spec, Bounds> groupBounds; 12023f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public boolean groupBoundsValid = false; 12033f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 120448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne PackedMap<Interval, MutableInt> forwardLinks; 120548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne public boolean forwardLinksValid = false; 120648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 120748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne PackedMap<Interval, MutableInt> backwardLinks; 120848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne public boolean backwardLinksValid = false; 12093f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 12103f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public int[] leadingMargins; 1211aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne public boolean leadingMarginsValid = false; 1212aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 12133f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public int[] trailingMargins; 1214aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne public boolean trailingMarginsValid = false; 12153f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 12163f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public Arc[] arcs; 12173f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public boolean arcsValid = false; 12183f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1219aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne public int[] locations; 122048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne public boolean locationsValid = false; 1221aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 12229505480ee37494a33e12565db4febfb18b6d7494Philip Milne public boolean hasWeights; 12239505480ee37494a33e12565db4febfb18b6d7494Philip Milne public boolean hasWeightsValid = false; 12249505480ee37494a33e12565db4febfb18b6d7494Philip Milne public int[] deltas; 12259505480ee37494a33e12565db4febfb18b6d7494Philip Milne 1226f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne boolean orderPreserved = DEFAULT_ORDER_PRESERVED; 12273f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 122848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private MutableInt parentMin = new MutableInt(0); 122948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private MutableInt parentMax = new MutableInt(-MAX_SIZE); 123048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 12313f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private Axis(boolean horizontal) { 12323f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne this.horizontal = horizontal; 12333f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 12343f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 12354a145d72622772b920f60195e80942058984259cPhilip Milne private int calculateMaxIndex() { 12364a145d72622772b920f60195e80942058984259cPhilip Milne // the number Integer.MIN_VALUE + 1 comes up in undefined cells 12374a145d72622772b920f60195e80942058984259cPhilip Milne int result = -1; 1238b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne for (int i = 0, N = getChildCount(); i < N; i++) { 1239b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne View c = getChildAt(i); 1240b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne LayoutParams params = getLayoutParams(c); 124193cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne Spec spec = horizontal ? params.columnSpec : params.rowSpec; 12424a145d72622772b920f60195e80942058984259cPhilip Milne Interval span = spec.span; 12434a145d72622772b920f60195e80942058984259cPhilip Milne result = max(result, span.min); 12444a145d72622772b920f60195e80942058984259cPhilip Milne result = max(result, span.max); 12450f57cea8989cc03bcae19b621096e50b035b7308Philip Milne result = max(result, span.size()); 12463f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 12474a145d72622772b920f60195e80942058984259cPhilip Milne return result == -1 ? UNDEFINED : result; 12483f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 12493f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 12504a145d72622772b920f60195e80942058984259cPhilip Milne private int getMaxIndex() { 12514a145d72622772b920f60195e80942058984259cPhilip Milne if (maxIndex == UNDEFINED) { 12524a145d72622772b920f60195e80942058984259cPhilip Milne maxIndex = max(0, calculateMaxIndex()); // use zero when there are no children 12533f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 12544a145d72622772b920f60195e80942058984259cPhilip Milne return maxIndex; 1255f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne } 1256f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne 1257f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public int getCount() { 12584a145d72622772b920f60195e80942058984259cPhilip Milne return max(definedCount, getMaxIndex()); 12593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 12603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 12613f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public void setCount(int count) { 12620f57cea8989cc03bcae19b621096e50b035b7308Philip Milne if (count != UNDEFINED && count < getMaxIndex()) { 12630f57cea8989cc03bcae19b621096e50b035b7308Philip Milne handleInvalidParams((horizontal ? "column" : "row") + 12640f57cea8989cc03bcae19b621096e50b035b7308Philip Milne "Count must be greater than or equal to the maximum of all grid indices " + 12650f57cea8989cc03bcae19b621096e50b035b7308Philip Milne "(and spans) defined in the LayoutParams of each child"); 12660f57cea8989cc03bcae19b621096e50b035b7308Philip Milne } 1267f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne this.definedCount = count; 12683f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 12693f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 12703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public boolean isOrderPreserved() { 1271f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne return orderPreserved; 12723f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 12733f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 12743f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public void setOrderPreserved(boolean orderPreserved) { 1275f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne this.orderPreserved = orderPreserved; 12763f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne invalidateStructure(); 12773f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 12783f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 127993cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne private PackedMap<Spec, Bounds> createGroupBounds() { 128093cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne Assoc<Spec, Bounds> assoc = Assoc.of(Spec.class, Bounds.class); 128148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne for (int i = 0, N = getChildCount(); i < N; i++) { 1282b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne View c = getChildAt(i); 1283a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne // we must include views that are GONE here, see introductory javadoc 12845125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne LayoutParams lp = getLayoutParams(c); 12855125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; 12866dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar Bounds bounds = spec.getAbsoluteAlignment(horizontal).getBounds(); 12875125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne assoc.put(spec, bounds); 12883f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 128948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return assoc.pack(); 12903f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 12913f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 12923f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private void computeGroupBounds() { 1293b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne Bounds[] values = groupBounds.values; 1294b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne for (int i = 0; i < values.length; i++) { 1295b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne values[i].reset(); 12963f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 1297aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne for (int i = 0, N = getChildCount(); i < N; i++) { 12983f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne View c = getChildAt(i); 1299a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne // we must include views that are GONE here, see introductory javadoc 13003f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne LayoutParams lp = getLayoutParams(c); 130193cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; 1302fbd9997e681dedffa85831948b3f22dd47f09f2aYigit Boyar int size = getMeasurementIncludingMargin(c, horizontal) + 1303fbd9997e681dedffa85831948b3f22dd47f09f2aYigit Boyar ((spec.weight == 0) ? 0 : getDeltas()[i]); 13049505480ee37494a33e12565db4febfb18b6d7494Philip Milne groupBounds.getValue(i).include(GridLayout.this, c, spec, this, size); 13053f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 13063f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 13073f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1308f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public PackedMap<Spec, Bounds> getGroupBounds() { 13093f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne if (groupBounds == null) { 13103f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne groupBounds = createGroupBounds(); 13113f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 13123f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne if (!groupBoundsValid) { 13133f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne computeGroupBounds(); 13143f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne groupBoundsValid = true; 13153f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 13163f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return groupBounds; 13173f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 13183f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 13193f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Add values computed by alignment - taking the max of all alignments in each span 132048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private PackedMap<Interval, MutableInt> createLinks(boolean min) { 132148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne Assoc<Interval, MutableInt> result = Assoc.of(Interval.class, MutableInt.class); 132293cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne Spec[] keys = getGroupBounds().keys; 132348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne for (int i = 0, N = keys.length; i < N; i++) { 132448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne Interval span = min ? keys[i].span : keys[i].span.inverse(); 132548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne result.put(span, new MutableInt()); 13263f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 132748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return result.pack(); 13283f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 13293f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 133048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private void computeLinks(PackedMap<Interval, MutableInt> links, boolean min) { 133148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne MutableInt[] spans = links.values; 13323f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne for (int i = 0; i < spans.length; i++) { 13333f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne spans[i].reset(); 13343f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 13353f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 13365d1a9840aaf57ae90716f0ac34abdcd09f7f4ed6Philip Milne // Use getter to trigger a re-evaluation 133748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne Bounds[] bounds = getGroupBounds().values; 13383f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne for (int i = 0; i < bounds.length; i++) { 133948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne int size = bounds[i].size(min); 134048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne MutableInt valueHolder = links.getValue(i); 13415125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne // this effectively takes the max() of the minima and the min() of the maxima 13425125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne valueHolder.value = max(valueHolder.value, min ? size : -size); 13433f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 13443f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 13453f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 134648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private PackedMap<Interval, MutableInt> getForwardLinks() { 134748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne if (forwardLinks == null) { 134848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne forwardLinks = createLinks(true); 13493f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 135048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne if (!forwardLinksValid) { 135148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne computeLinks(forwardLinks, true); 135248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne forwardLinksValid = true; 13533f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 135448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return forwardLinks; 13553f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 13563f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 135748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private PackedMap<Interval, MutableInt> getBackwardLinks() { 135848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne if (backwardLinks == null) { 135948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne backwardLinks = createLinks(false); 13603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 136148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne if (!backwardLinksValid) { 136248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne computeLinks(backwardLinks, false); 136348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne backwardLinksValid = true; 136448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 136548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return backwardLinks; 13663f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 13673f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 136848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private void include(List<Arc> arcs, Interval key, MutableInt size, 1369edd69518ffec87fb7b1931e708678ef441152cdePhilip Milne boolean ignoreIfAlreadyPresent) { 137048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne /* 137148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne Remove self referential links. 137248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne These appear: 137348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne . as parental constraints when GridLayout has no children 137448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne . when components have been marked as GONE 137548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne */ 137648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne if (key.size() == 0) { 137748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return; 13783f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 137948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne // this bit below should really be computed outside here - 138048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne // its just to stop default (row/col > 0) constraints obliterating valid entries 138148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne if (ignoreIfAlreadyPresent) { 138248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne for (Arc arc : arcs) { 138348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne Interval span = arc.span; 138448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne if (span.equals(key)) { 138548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return; 138648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 138748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 138848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 138948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne arcs.add(new Arc(key, size)); 13903f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 13913f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 139248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private void include(List<Arc> arcs, Interval key, MutableInt size) { 139348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne include(arcs, key, size, true); 13943f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 13953f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1396aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne // Group arcs by their first vertex, returning an array of arrays. 13973f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // This is linear in the number of arcs. 1398f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne Arc[][] groupArcsByFirstVertex(Arc[] arcs) { 139948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne int N = getCount() + 1; // the number of vertices 14003f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne Arc[][] result = new Arc[N][]; 14013f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int[] sizes = new int[N]; 14023f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne for (Arc arc : arcs) { 14033f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne sizes[arc.span.min]++; 14045d1a9840aaf57ae90716f0ac34abdcd09f7f4ed6Philip Milne } 14053f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne for (int i = 0; i < sizes.length; i++) { 14063f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne result[i] = new Arc[sizes[i]]; 14073f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 14083f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // reuse the sizes array to hold the current last elements as we insert each arc 14093f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne Arrays.fill(sizes, 0); 14103f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne for (Arc arc : arcs) { 14113f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int i = arc.span.min; 14123f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne result[i][sizes[i]++] = arc; 14133f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 14143f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 14153f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return result; 14163f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 14173f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 141848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private Arc[] topologicalSort(final Arc[] arcs) { 141948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return new Object() { 142048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne Arc[] result = new Arc[arcs.length]; 142148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne int cursor = result.length - 1; 142248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne Arc[][] arcsByVertex = groupArcsByFirstVertex(arcs); 14233f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int[] visited = new int[getCount() + 1]; 14243f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 142548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne void walk(int loc) { 142648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne switch (visited[loc]) { 142748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne case NEW: { 142848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne visited[loc] = PENDING; 142948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne for (Arc arc : arcsByVertex[loc]) { 143048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne walk(arc.span.max); 143148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne result[cursor--] = arc; 14323f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 143348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne visited[loc] = COMPLETE; 143448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne break; 143548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 143648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne case PENDING: { 1437b65408fc0fefa82161475de7f7f32284f6f60a56Philip Milne // le singe est dans l'arbre 143848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne assert false; 143948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne break; 144048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 144148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne case COMPLETE: { 144248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne break; 14433f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 14443f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 14453f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 144648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 144748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne Arc[] sort() { 144848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne for (int loc = 0, N = arcsByVertex.length; loc < N; loc++) { 144948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne walk(loc); 145048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 145148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne assert cursor == -1; 145248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return result; 145348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 145448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne }.sort(); 145548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 145648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 145748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private Arc[] topologicalSort(List<Arc> arcs) { 145848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return topologicalSort(arcs.toArray(new Arc[arcs.size()])); 14593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 14603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 146148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private void addComponentSizes(List<Arc> result, PackedMap<Interval, MutableInt> links) { 146248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne for (int i = 0; i < links.keys.length; i++) { 146348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne Interval key = links.keys[i]; 146448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne include(result, key, links.values[i], false); 146548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 146648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 146748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 1468aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne private Arc[] createArcs() { 146948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne List<Arc> mins = new ArrayList<Arc>(); 147048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne List<Arc> maxs = new ArrayList<Arc>(); 14713f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 147248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne // Add the minimum values from the components. 147348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne addComponentSizes(mins, getForwardLinks()); 147448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne // Add the maximum values from the components. 147548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne addComponentSizes(maxs, getBackwardLinks()); 14763f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 147748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne // Add ordering constraints to prevent row/col sizes from going negative 1478f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne if (orderPreserved) { 147948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne // Add a constraint for every row/col 14803f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne for (int i = 0; i < getCount(); i++) { 1481899d5922870c78e0e663bc5661849eb468afc984Philip Milne include(mins, new Interval(i, i + 1), new MutableInt(0)); 14823f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 14833f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 148448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 148548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne // Add the container constraints. Use the version of include that allows 148648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne // duplicate entries in case a child spans the entire grid. 148748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne int N = getCount(); 148848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne include(mins, new Interval(0, N), parentMin, false); 148948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne include(maxs, new Interval(N, 0), parentMax, false); 149048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 149148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne // Sort 149248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne Arc[] sMins = topologicalSort(mins); 149348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne Arc[] sMaxs = topologicalSort(maxs); 149448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 149548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return append(sMins, sMaxs); 149648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 149748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 149848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private void computeArcs() { 149948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne // getting the links validates the values that are shared by the arc list 150048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne getForwardLinks(); 150148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne getBackwardLinks(); 15023f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 15033f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1504aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne public Arc[] getArcs() { 15053f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne if (arcs == null) { 1506aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne arcs = createArcs(); 15073f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 15083f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne if (!arcsValid) { 150948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne computeArcs(); 15103f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne arcsValid = true; 15113f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 15123f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return arcs; 15133f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 15143f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1515aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne private boolean relax(int[] locations, Arc entry) { 151648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne if (!entry.valid) { 151748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return false; 151848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 15193f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne Interval span = entry.span; 15203f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int u = span.min; 15213f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int v = span.max; 15223f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int value = entry.value.value; 15233f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int candidate = locations[u] + value; 1524aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne if (candidate > locations[v]) { 15253f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne locations[v] = candidate; 15263f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return true; 15273f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 15283f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return false; 15293f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 15303f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1531f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne private void init(int[] locations) { 15324a145d72622772b920f60195e80942058984259cPhilip Milne Arrays.fill(locations, 0); 1533f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne } 1534f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne 1535f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne private String arcsToString(List<Arc> arcs) { 15364a145d72622772b920f60195e80942058984259cPhilip Milne String var = horizontal ? "x" : "y"; 1537f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne StringBuilder result = new StringBuilder(); 15384a145d72622772b920f60195e80942058984259cPhilip Milne boolean first = true; 15394a145d72622772b920f60195e80942058984259cPhilip Milne for (Arc arc : arcs) { 15404a145d72622772b920f60195e80942058984259cPhilip Milne if (first) { 15414a145d72622772b920f60195e80942058984259cPhilip Milne first = false; 1542f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne } else { 15434a145d72622772b920f60195e80942058984259cPhilip Milne result = result.append(", "); 1544f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne } 1545f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne int src = arc.span.min; 1546f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne int dst = arc.span.max; 1547f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne int value = arc.value.value; 1548f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne result.append((src < dst) ? 1549edd69518ffec87fb7b1931e708678ef441152cdePhilip Milne var + dst + "-" + var + src + ">=" + value : 1550edd69518ffec87fb7b1931e708678ef441152cdePhilip Milne var + src + "-" + var + dst + "<=" + -value); 1551f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne 1552f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne } 1553f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne return result.toString(); 1554f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne } 1555f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne 1556f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne private void logError(String axisName, Arc[] arcs, boolean[] culprits0) { 1557f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne List<Arc> culprits = new ArrayList<Arc>(); 1558f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne List<Arc> removed = new ArrayList<Arc>(); 1559f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne for (int c = 0; c < arcs.length; c++) { 1560f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne Arc arc = arcs[c]; 1561f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne if (culprits0[c]) { 1562f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne culprits.add(arc); 1563f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne } 1564f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne if (!arc.valid) { 1565f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne removed.add(arc); 1566f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne } 1567f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne } 1568465ea74234dd9f7c8e2f5927f51eb087de74ed2eAdam Powell mPrinter.println(axisName + " constraints: " + arcsToString(culprits) + 1569211d03322cf6e8a6c8a947bf8a494abd78a767c2Philip Milne " are inconsistent; permanently removing: " + arcsToString(removed) + ". "); 1570f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne } 1571f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne 1572aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne /* 1573aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne Bellman-Ford variant - modified to reduce typical running time from O(N^2) to O(N) 1574aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 1575aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne GridLayout converts its requirements into a system of linear constraints of the 1576aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne form: 1577aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 1578aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne x[i] - x[j] < a[k] 1579aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 1580aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne Where the x[i] are variables and the a[k] are constants. 1581aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 1582aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne For example, if the variables were instead labeled x, y, z we might have: 1583aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 1584aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne x - y < 17 1585aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne y - z < 23 1586aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne z - x < 42 1587aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 1588aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne This is a special case of the Linear Programming problem that is, in turn, 1589aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne equivalent to the single-source shortest paths problem on a digraph, for 1590aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne which the O(n^2) Bellman-Ford algorithm the most commonly used general solution. 1591aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne */ 159298d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar private boolean solve(Arc[] arcs, int[] locations) { 159398d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar return solve(arcs, locations, true); 159498d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar } 159598d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar 159698d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar private boolean solve(Arc[] arcs, int[] locations, boolean modifyOnError) { 1597f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne String axisName = horizontal ? "horizontal" : "vertical"; 15983f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int N = getCount() + 1; // The number of vertices is the number of columns/rows + 1. 1599f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne boolean[] originalCulprits = null; 16003f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1601f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne for (int p = 0; p < arcs.length; p++) { 1602f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne init(locations); 1603f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne 1604f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne // We take one extra pass over traditional Bellman-Ford (and omit their final step) 1605f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne for (int i = 0; i < N; i++) { 1606f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne boolean changed = false; 1607f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne for (int j = 0, length = arcs.length; j < length; j++) { 1608f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne changed |= relax(locations, arcs[j]); 1609f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne } 1610f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne if (!changed) { 1611f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne if (originalCulprits != null) { 1612f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne logError(axisName, arcs, originalCulprits); 1613f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne } 161498d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar return true; 16153f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 16163f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 161748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 161898d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar if (!modifyOnError) { 161998d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar return false; // cannot solve with these constraints 162098d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar } 162198d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar 1622f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne boolean[] culprits = new boolean[arcs.length]; 1623f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne for (int i = 0; i < N; i++) { 1624f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne for (int j = 0, length = arcs.length; j < length; j++) { 1625f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne culprits[j] |= relax(locations, arcs[j]); 1626f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne } 1627f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne } 162848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 1629f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne if (p == 0) { 1630f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne originalCulprits = culprits; 163148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 1632f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne 1633f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne for (int i = 0; i < arcs.length; i++) { 1634f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne if (culprits[i]) { 1635f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne Arc arc = arcs[i]; 1636f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne // Only remove max values, min values alone cannot be inconsistent 1637f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne if (arc.span.min < arc.span.max) { 1638f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne continue; 1639f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne } 1640f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne arc.valid = false; 1641f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne break; 164248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 164348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 164448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 164598d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar return true; 16463f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 16473f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1648aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne private void computeMargins(boolean leading) { 1649aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne int[] margins = leading ? leadingMargins : trailingMargins; 1650b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne for (int i = 0, N = getChildCount(); i < N; i++) { 16513f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne View c = getChildAt(i); 1652d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne if (c.getVisibility() == View.GONE) continue; 16533f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne LayoutParams lp = getLayoutParams(c); 165493cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; 165593cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne Interval span = spec.span; 16563f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int index = leading ? span.min : span.max; 16574c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne margins[index] = max(margins[index], getMargin1(c, horizontal, leading)); 16583f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 16593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 16603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1661f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne // External entry points 1662f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne 1663f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public int[] getLeadingMargins() { 1664aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne if (leadingMargins == null) { 1665aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne leadingMargins = new int[getCount() + 1]; 1666aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne } 1667aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne if (!leadingMarginsValid) { 1668aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne computeMargins(true); 1669aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne leadingMarginsValid = true; 1670aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne } 1671aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne return leadingMargins; 1672aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne } 1673aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 1674f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public int[] getTrailingMargins() { 1675aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne if (trailingMargins == null) { 1676aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne trailingMargins = new int[getCount() + 1]; 1677aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne } 1678aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne if (!trailingMarginsValid) { 1679aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne computeMargins(false); 1680aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne trailingMarginsValid = true; 1681aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne } 1682aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne return trailingMargins; 1683aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne } 16843f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 168598d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar private boolean solve(int[] a) { 168698d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar return solve(getArcs(), a); 168787260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne } 168887260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne 16899505480ee37494a33e12565db4febfb18b6d7494Philip Milne private boolean computeHasWeights() { 16909505480ee37494a33e12565db4febfb18b6d7494Philip Milne for (int i = 0, N = getChildCount(); i < N; i++) { 16916dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar final View child = getChildAt(i); 16926dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar if (child.getVisibility() == View.GONE) { 16936dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar continue; 16946dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar } 16956dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar LayoutParams lp = getLayoutParams(child); 16969505480ee37494a33e12565db4febfb18b6d7494Philip Milne Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; 16979505480ee37494a33e12565db4febfb18b6d7494Philip Milne if (spec.weight != 0) { 169887260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne return true; 169987260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne } 170087260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne } 170187260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne return false; 170287260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne } 170387260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne 17049505480ee37494a33e12565db4febfb18b6d7494Philip Milne private boolean hasWeights() { 17059505480ee37494a33e12565db4febfb18b6d7494Philip Milne if (!hasWeightsValid) { 17069505480ee37494a33e12565db4febfb18b6d7494Philip Milne hasWeights = computeHasWeights(); 17079505480ee37494a33e12565db4febfb18b6d7494Philip Milne hasWeightsValid = true; 17089505480ee37494a33e12565db4febfb18b6d7494Philip Milne } 17099505480ee37494a33e12565db4febfb18b6d7494Philip Milne return hasWeights; 17109505480ee37494a33e12565db4febfb18b6d7494Philip Milne } 17119505480ee37494a33e12565db4febfb18b6d7494Philip Milne 17129505480ee37494a33e12565db4febfb18b6d7494Philip Milne public int[] getDeltas() { 17139505480ee37494a33e12565db4febfb18b6d7494Philip Milne if (deltas == null) { 17149505480ee37494a33e12565db4febfb18b6d7494Philip Milne deltas = new int[getChildCount()]; 17159505480ee37494a33e12565db4febfb18b6d7494Philip Milne } 17169505480ee37494a33e12565db4febfb18b6d7494Philip Milne return deltas; 17179505480ee37494a33e12565db4febfb18b6d7494Philip Milne } 17189505480ee37494a33e12565db4febfb18b6d7494Philip Milne 171998d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar private void shareOutDelta(int totalDelta, float totalWeight) { 172098d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar Arrays.fill(deltas, 0); 17219505480ee37494a33e12565db4febfb18b6d7494Philip Milne for (int i = 0, N = getChildCount(); i < N; i++) { 17226dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar final View c = getChildAt(i); 17236dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar if (c.getVisibility() == View.GONE) { 17246dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar continue; 17256dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar } 17269505480ee37494a33e12565db4febfb18b6d7494Philip Milne LayoutParams lp = getLayoutParams(c); 17279505480ee37494a33e12565db4febfb18b6d7494Philip Milne Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; 17289505480ee37494a33e12565db4febfb18b6d7494Philip Milne float weight = spec.weight; 17299505480ee37494a33e12565db4febfb18b6d7494Philip Milne if (weight != 0) { 173087260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne int delta = Math.round((weight * totalDelta / totalWeight)); 17319505480ee37494a33e12565db4febfb18b6d7494Philip Milne deltas[i] = delta; 173298d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar // the two adjustments below are to counter the above rounding and avoid 173398d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar // off-by-ones at the end 173487260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne totalDelta -= delta; 173587260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne totalWeight -= weight; 173687260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne } 173787260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne } 173887260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne } 173987260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne 174087260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne private void solveAndDistributeSpace(int[] a) { 17419505480ee37494a33e12565db4febfb18b6d7494Philip Milne Arrays.fill(getDeltas(), 0); 17429505480ee37494a33e12565db4febfb18b6d7494Philip Milne solve(a); 174398d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar int deltaMax = parentMin.value * getChildCount() + 1; //exclusive 174498d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar if (deltaMax < 2) { 174598d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar return; //don't have any delta to distribute 174698d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar } 174798d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar int deltaMin = 0; //inclusive 174898d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar 174998d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar float totalWeight = calculateTotalWeight(); 175098d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar 175198d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar int validDelta = -1; //delta for which a solution exists 175298d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar boolean validSolution = true; 175398d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar // do a binary search to find the max delta that won't conflict with constraints 175498d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar while(deltaMin < deltaMax) { 175598d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar final int delta = (deltaMin + deltaMax) / 2; 175698d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar invalidateValues(); 175798d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar shareOutDelta(delta, totalWeight); 175898d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar validSolution = solve(getArcs(), a, false); 175998d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar if (validSolution) { 176098d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar validDelta = delta; 176198d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar deltaMin = delta + 1; 176298d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar } else { 176398d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar deltaMax = delta; 176498d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar } 176598d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar } 176698d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar if (validDelta > 0 && !validSolution) { 176798d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar // last solution was not successful but we have a successful one. Use it. 176898d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar invalidateValues(); 176998d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar shareOutDelta(validDelta, totalWeight); 177098d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar solve(a); 177198d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar } 177298d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar } 177398d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar 177498d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar private float calculateTotalWeight() { 177598d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar float totalWeight = 0f; 177698d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar for (int i = 0, N = getChildCount(); i < N; i++) { 177798d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar View c = getChildAt(i); 17786dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar if (c.getVisibility() == View.GONE) { 17796dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar continue; 17806dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar } 178198d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar LayoutParams lp = getLayoutParams(c); 178298d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; 178398d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar totalWeight += spec.weight; 178498d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar } 178598d5f04f91580bf8b517c17132eeb8d42008b61dYigit Boyar return totalWeight; 178687260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne } 178787260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne 178887260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne private void computeLocations(int[] a) { 17899505480ee37494a33e12565db4febfb18b6d7494Philip Milne if (!hasWeights()) { 17909505480ee37494a33e12565db4febfb18b6d7494Philip Milne solve(a); 17919505480ee37494a33e12565db4febfb18b6d7494Philip Milne } else { 17929505480ee37494a33e12565db4febfb18b6d7494Philip Milne solveAndDistributeSpace(a); 17939505480ee37494a33e12565db4febfb18b6d7494Philip Milne } 17944a145d72622772b920f60195e80942058984259cPhilip Milne if (!orderPreserved) { 17954a145d72622772b920f60195e80942058984259cPhilip Milne // Solve returns the smallest solution to the constraint system for which all 17964a145d72622772b920f60195e80942058984259cPhilip Milne // values are positive. One value is therefore zero - though if the row/col 17974a145d72622772b920f60195e80942058984259cPhilip Milne // order is not preserved this may not be the first vertex. For consistency, 17984a145d72622772b920f60195e80942058984259cPhilip Milne // translate all the values so that they measure the distance from a[0]; the 17994a145d72622772b920f60195e80942058984259cPhilip Milne // leading edge of the parent. After this transformation some values may be 18004a145d72622772b920f60195e80942058984259cPhilip Milne // negative. 18014a145d72622772b920f60195e80942058984259cPhilip Milne int a0 = a[0]; 18024a145d72622772b920f60195e80942058984259cPhilip Milne for (int i = 0, N = a.length; i < N; i++) { 18034a145d72622772b920f60195e80942058984259cPhilip Milne a[i] = a[i] - a0; 18044a145d72622772b920f60195e80942058984259cPhilip Milne } 18054a145d72622772b920f60195e80942058984259cPhilip Milne } 18063f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 18073f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1808f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public int[] getLocations() { 1809aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne if (locations == null) { 1810aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne int N = getCount() + 1; 1811aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne locations = new int[N]; 1812aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne } 181348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne if (!locationsValid) { 181448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne computeLocations(locations); 181548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne locationsValid = true; 181648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 1817aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne return locations; 18183f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 18193f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1820aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne private int size(int[] locations) { 18214a145d72622772b920f60195e80942058984259cPhilip Milne // The parental edges are attached to vertices 0 and N - even when order is not 18224a145d72622772b920f60195e80942058984259cPhilip Milne // being preserved and other vertices fall outside this range. Measure the distance 18234a145d72622772b920f60195e80942058984259cPhilip Milne // between vertices 0 and N, assuming that locations[0] = 0. 18244a145d72622772b920f60195e80942058984259cPhilip Milne return locations[getCount()]; 1825aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne } 1826aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 182748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private void setParentConstraints(int min, int max) { 182848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne parentMin.value = min; 182948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne parentMax.value = -max; 183048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne locationsValid = false; 18313f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 18323f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 183348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private int getMeasure(int min, int max) { 183448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne setParentConstraints(min, max); 183548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return size(getLocations()); 183648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 1837aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 1838f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public int getMeasure(int measureSpec) { 183948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne int mode = MeasureSpec.getMode(measureSpec); 184048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne int size = MeasureSpec.getSize(measureSpec); 184148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne switch (mode) { 184248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne case MeasureSpec.UNSPECIFIED: { 184393cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne return getMeasure(0, MAX_SIZE); 184448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 184548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne case MeasureSpec.EXACTLY: { 184648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return getMeasure(size, size); 184748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 184848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne case MeasureSpec.AT_MOST: { 184948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return getMeasure(0, size); 185048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 185148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne default: { 185248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne assert false; 185348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return 0; 185448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 18553f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 185648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 1857aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 1858f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public void layout(int size) { 185948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne setParentConstraints(size, size); 186048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne getLocations(); 18613f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 18623f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1863f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public void invalidateStructure() { 18644a145d72622772b920f60195e80942058984259cPhilip Milne maxIndex = UNDEFINED; 1865aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 18663f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne groupBounds = null; 186748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne forwardLinks = null; 186848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne backwardLinks = null; 186948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 1870aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne leadingMargins = null; 1871aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne trailingMargins = null; 1872c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne arcs = null; 187348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 1874aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne locations = null; 18753f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 18769505480ee37494a33e12565db4febfb18b6d7494Philip Milne deltas = null; 18779505480ee37494a33e12565db4febfb18b6d7494Philip Milne hasWeightsValid = false; 18789505480ee37494a33e12565db4febfb18b6d7494Philip Milne 18793f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne invalidateValues(); 18803f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 18813f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1882f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public void invalidateValues() { 18833f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne groupBoundsValid = false; 188448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne forwardLinksValid = false; 188548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne backwardLinksValid = false; 188648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 1887aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne leadingMarginsValid = false; 1888aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne trailingMarginsValid = false; 188948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne arcsValid = false; 189048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 189148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne locationsValid = false; 18923f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 18933f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 18943f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 18953f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 18963f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Layout information associated with each of the children of a GridLayout. 18973f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <p> 18983f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * GridLayout supports both row and column spanning and arbitrary forms of alignment within 18993f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * each cell group. The fundamental parameters associated with each cell group are 19003f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * gathered into their vertical and horizontal components and stored 190193cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * in the {@link #rowSpec} and {@link #columnSpec} layout parameters. 19026216e87fe8ad3273855233965b34049d22763e94Philip Milne * {@link GridLayout.Spec Specs} are immutable structures 1903b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne * and may be shared between the layout parameters of different children. 19043f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <p> 190593cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * The row and column specs contain the leading and trailing indices along each axis 1906aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * and together specify the four grid indices that delimit the cells of this cell group. 19073f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <p> 190893cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * The alignment properties of the row and column specs together specify 19093f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * both aspects of alignment within the cell group. It is also possible to specify a child's 19103f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * alignment within its cell group by using the {@link GridLayout.LayoutParams#setGravity(int)} 19113f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * method. 191287260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * <p> 191387260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * The weight property is also included in Spec and specifies the proportion of any 19149505480ee37494a33e12565db4febfb18b6d7494Philip Milne * excess space that is due to the associated view. 1915f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * 1916f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * <h4>WRAP_CONTENT and MATCH_PARENT</h4> 1917f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * 1918f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * Because the default values of the {@link #width} and {@link #height} 1919f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * properties are both {@link #WRAP_CONTENT}, this value never needs to be explicitly 1920f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * declared in the layout parameters of GridLayout's children. In addition, 1921f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * GridLayout does not distinguish the special size value {@link #MATCH_PARENT} from 1922f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * {@link #WRAP_CONTENT}. A component's ability to expand to the size of the parent is 1923f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * instead controlled by the principle of <em>flexibility</em>, 1924f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * as discussed in {@link GridLayout}. 1925f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * 1926f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * <h4>Summary</h4> 1927f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * 1928f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * You should not need to use either of the special size values: 1929f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * {@code WRAP_CONTENT} or {@code MATCH_PARENT} when configuring the children of 1930f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * a GridLayout. 19313f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 19323f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <h4>Default values</h4> 19333f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 19343f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <ul> 19353f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <li>{@link #width} = {@link #WRAP_CONTENT}</li> 19363f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <li>{@link #height} = {@link #WRAP_CONTENT}</li> 19373f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <li>{@link #topMargin} = 0 when 19383f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@link GridLayout#setUseDefaultMargins(boolean) useDefaultMargins} is 19397fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * {@code false}; otherwise {@link #UNDEFINED}, to 19403f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * indicate that a default value should be computed on demand. </li> 19413f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <li>{@link #leftMargin} = 0 when 19423f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@link GridLayout#setUseDefaultMargins(boolean) useDefaultMargins} is 19437fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * {@code false}; otherwise {@link #UNDEFINED}, to 19443f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * indicate that a default value should be computed on demand. </li> 19453f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <li>{@link #bottomMargin} = 0 when 19463f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@link GridLayout#setUseDefaultMargins(boolean) useDefaultMargins} is 19477fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * {@code false}; otherwise {@link #UNDEFINED}, to 19483f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * indicate that a default value should be computed on demand. </li> 19493f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <li>{@link #rightMargin} = 0 when 19503f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@link GridLayout#setUseDefaultMargins(boolean) useDefaultMargins} is 19517fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * {@code false}; otherwise {@link #UNDEFINED}, to 19523f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * indicate that a default value should be computed on demand. </li> 1953f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * <li>{@link #rowSpec}<code>.row</code> = {@link #UNDEFINED} </li> 1954f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * <li>{@link #rowSpec}<code>.rowSpan</code> = 1 </li> 1955f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * <li>{@link #rowSpec}<code>.alignment</code> = {@link #BASELINE} </li> 195687260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * <li>{@link #rowSpec}<code>.weight</code> = 0 </li> 1957f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * <li>{@link #columnSpec}<code>.column</code> = {@link #UNDEFINED} </li> 1958f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * <li>{@link #columnSpec}<code>.columnSpan</code> = 1 </li> 19596216e87fe8ad3273855233965b34049d22763e94Philip Milne * <li>{@link #columnSpec}<code>.alignment</code> = {@link #START} </li> 196087260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * <li>{@link #columnSpec}<code>.weight</code> = 0 </li> 19613f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * </ul> 19623f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 1963f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * See {@link GridLayout} for a more complete description of the conventions 1964f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * used by GridLayout in the interpretation of the properties of this class. 1965f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * 19663f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_Layout_layout_row 19673f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_Layout_layout_rowSpan 196887260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * @attr ref android.R.styleable#GridLayout_Layout_layout_rowWeight 19693f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_Layout_layout_column 19703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_Layout_layout_columnSpan 197187260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * @attr ref android.R.styleable#GridLayout_Layout_layout_columnWeight 19723f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_Layout_layout_gravity 19733f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 19743f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public static class LayoutParams extends MarginLayoutParams { 19753f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 19763f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Default values 19773f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 19783f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private static final int DEFAULT_WIDTH = WRAP_CONTENT; 19793f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private static final int DEFAULT_HEIGHT = WRAP_CONTENT; 19803f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private static final int DEFAULT_MARGIN = UNDEFINED; 19813f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private static final int DEFAULT_ROW = UNDEFINED; 19823f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private static final int DEFAULT_COLUMN = UNDEFINED; 1983f474870fe1189f73cf8ffbaba9e524ef194b5043Philip Milne private static final Interval DEFAULT_SPAN = new Interval(UNDEFINED, UNDEFINED + 1); 19843f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private static final int DEFAULT_SPAN_SIZE = DEFAULT_SPAN.size(); 19853f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 19863f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // TypedArray indices 19873f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 1988b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int MARGIN = R.styleable.ViewGroup_MarginLayout_layout_margin; 1989b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int LEFT_MARGIN = R.styleable.ViewGroup_MarginLayout_layout_marginLeft; 1990b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int TOP_MARGIN = R.styleable.ViewGroup_MarginLayout_layout_marginTop; 1991b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int RIGHT_MARGIN = 1992b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne R.styleable.ViewGroup_MarginLayout_layout_marginRight; 19933f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private static final int BOTTOM_MARGIN = 1994b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne R.styleable.ViewGroup_MarginLayout_layout_marginBottom; 1995b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int COLUMN = R.styleable.GridLayout_Layout_layout_column; 1996b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int COLUMN_SPAN = R.styleable.GridLayout_Layout_layout_columnSpan; 199787260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne private static final int COLUMN_WEIGHT = R.styleable.GridLayout_Layout_layout_columnWeight; 19985d1a9840aaf57ae90716f0ac34abdcd09f7f4ed6Philip Milne 1999b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int ROW = R.styleable.GridLayout_Layout_layout_row; 2000b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int ROW_SPAN = R.styleable.GridLayout_Layout_layout_rowSpan; 200187260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne private static final int ROW_WEIGHT = R.styleable.GridLayout_Layout_layout_rowWeight; 20025d1a9840aaf57ae90716f0ac34abdcd09f7f4ed6Philip Milne 2003b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne private static final int GRAVITY = R.styleable.GridLayout_Layout_layout_gravity; 20043f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 20053f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Instance variables 20063f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 20073f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 2008f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * The spec that defines the vertical characteristics of the cell group 20093f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * described by these layout parameters. 2010d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * If an assignment is made to this field after a measurement or layout operation 2011d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * has already taken place, a call to 2012d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * {@link ViewGroup#setLayoutParams(ViewGroup.LayoutParams)} 2013d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * must be made to notify GridLayout of the change. GridLayout is normally able 2014d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * to detect when code fails to observe this rule, issue a warning and take steps to 2015d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * compensate for the omission. This facility is implemented on a best effort basis 2016d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * and should not be relied upon in production code - so it is best to include the above 2017d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * calls to remove the warnings as soon as it is practical. 20183f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 2019f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public Spec rowSpec = Spec.UNDEFINED; 2020f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne 20213f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 2022f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne * The spec that defines the horizontal characteristics of the cell group 20233f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * described by these layout parameters. 2024d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * If an assignment is made to this field after a measurement or layout operation 2025d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * has already taken place, a call to 2026d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * {@link ViewGroup#setLayoutParams(ViewGroup.LayoutParams)} 2027d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * must be made to notify GridLayout of the change. GridLayout is normally able 2028d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * to detect when code fails to observe this rule, issue a warning and take steps to 2029d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * compensate for the omission. This facility is implemented on a best effort basis 2030d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * and should not be relied upon in production code - so it is best to include the above 2031d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne * calls to remove the warnings as soon as it is practical. 20323f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 2033f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public Spec columnSpec = Spec.UNDEFINED; 20343f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 20353f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Constructors 20363f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 20373f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private LayoutParams( 20383f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int width, int height, 20393f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int left, int top, int right, int bottom, 204093cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne Spec rowSpec, Spec columnSpec) { 20413f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne super(width, height); 20423f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne setMargins(left, top, right, bottom); 204393cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne this.rowSpec = rowSpec; 204493cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne this.columnSpec = columnSpec; 20453f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 20463f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 20473f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 204893cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * Constructs a new LayoutParams instance for this <code>rowSpec</code> 204993cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * and <code>columnSpec</code>. All other fields are initialized with 20503f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * default values as defined in {@link LayoutParams}. 20513f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 205293cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * @param rowSpec the rowSpec 205393cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * @param columnSpec the columnSpec 20543f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 205593cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne public LayoutParams(Spec rowSpec, Spec columnSpec) { 20563f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne this(DEFAULT_WIDTH, DEFAULT_HEIGHT, 20573f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne DEFAULT_MARGIN, DEFAULT_MARGIN, DEFAULT_MARGIN, DEFAULT_MARGIN, 205893cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne rowSpec, columnSpec); 20593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 20603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 20613f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 20623f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Constructs a new LayoutParams with default values as defined in {@link LayoutParams}. 20633f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 20643f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public LayoutParams() { 2065f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne this(Spec.UNDEFINED, Spec.UNDEFINED); 20663f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 20673f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 20683f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Copying constructors 20693f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 20703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 20713f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@inheritDoc} 20723f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 20733f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public LayoutParams(ViewGroup.LayoutParams params) { 20743f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne super(params); 20753f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 20763f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 20773f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 20783f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@inheritDoc} 20793f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 20803f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public LayoutParams(MarginLayoutParams params) { 20813f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne super(params); 20823f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 20833f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 20843f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 20850a0e155cadecd32599a7354a1836232c885f4bd2Alan Viverette * Copy constructor. Clones the width, height, margin values, row spec, 20860a0e155cadecd32599a7354a1836232c885f4bd2Alan Viverette * and column spec of the source. 20870a0e155cadecd32599a7354a1836232c885f4bd2Alan Viverette * 20880a0e155cadecd32599a7354a1836232c885f4bd2Alan Viverette * @param source The layout params to copy from. 20893f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 20900a0e155cadecd32599a7354a1836232c885f4bd2Alan Viverette public LayoutParams(LayoutParams source) { 20910a0e155cadecd32599a7354a1836232c885f4bd2Alan Viverette super(source); 20920a0e155cadecd32599a7354a1836232c885f4bd2Alan Viverette 20930a0e155cadecd32599a7354a1836232c885f4bd2Alan Viverette this.rowSpec = source.rowSpec; 20940a0e155cadecd32599a7354a1836232c885f4bd2Alan Viverette this.columnSpec = source.columnSpec; 20953f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 20963f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 20973f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // AttributeSet constructors 20983f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 20993f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 21003f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@inheritDoc} 21013f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 21023f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Values not defined in the attribute set take the default values 21033f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * defined in {@link LayoutParams}. 21043f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 21053f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public LayoutParams(Context context, AttributeSet attrs) { 21065125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne super(context, attrs); 21075125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne reInitSuper(context, attrs); 21085125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne init(context, attrs); 21093f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 21103f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 21113f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Implementation 21123f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 21133f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Reinitialise the margins using a different default policy than MarginLayoutParams. 21143f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // Here we use the value UNDEFINED (as distinct from zero) to represent the undefined state 21153f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // so that a layout manager default can be accessed post set up. We need this as, at the 21163f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // point of installation, we do not know how many rows/cols there are and therefore 21173f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // which elements are positioned next to the container's trailing edges. We need to 21183f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // know this as margins around the container's boundary should have different 21193f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // defaults to those between peers. 21203f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 21213f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // This method could be parametrized and moved into MarginLayout. 21223f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private void reInitSuper(Context context, AttributeSet attrs) { 2123b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne TypedArray a = 2124b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne context.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout); 21253f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne try { 21263f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int margin = a.getDimensionPixelSize(MARGIN, DEFAULT_MARGIN); 21273f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 21283f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne this.leftMargin = a.getDimensionPixelSize(LEFT_MARGIN, margin); 21293f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne this.topMargin = a.getDimensionPixelSize(TOP_MARGIN, margin); 21303f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne this.rightMargin = a.getDimensionPixelSize(RIGHT_MARGIN, margin); 21313f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne this.bottomMargin = a.getDimensionPixelSize(BOTTOM_MARGIN, margin); 21323f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } finally { 21333f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne a.recycle(); 21343f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 21353f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 21363f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 21375125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne private void init(Context context, AttributeSet attrs) { 2138b0ce49b5ad53631ff0c3cdd8266e82f3c20c65dcPhilip Milne TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GridLayout_Layout); 21393f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne try { 21405125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne int gravity = a.getInt(GRAVITY, Gravity.NO_GRAVITY); 21413f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 21421e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne int column = a.getInt(COLUMN, DEFAULT_COLUMN); 21435125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne int colSpan = a.getInt(COLUMN_SPAN, DEFAULT_SPAN_SIZE); 214487260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne float colWeight = a.getFloat(COLUMN_WEIGHT, Spec.DEFAULT_WEIGHT); 214587260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne this.columnSpec = spec(column, colSpan, getAlignment(gravity, true), colWeight); 21463f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 21471e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne int row = a.getInt(ROW, DEFAULT_ROW); 21481e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne int rowSpan = a.getInt(ROW_SPAN, DEFAULT_SPAN_SIZE); 214987260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne float rowWeight = a.getFloat(ROW_WEIGHT, Spec.DEFAULT_WEIGHT); 215087260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne this.rowSpec = spec(row, rowSpan, getAlignment(gravity, false), rowWeight); 21513f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } finally { 21523f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne a.recycle(); 21533f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 21543f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 21553f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 21563f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 21577fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * Describes how the child views are positioned. Default is {@code LEFT | BASELINE}. 21586216e87fe8ad3273855233965b34049d22763e94Philip Milne * See {@link Gravity}. 21593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 21607fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * @param gravity the new gravity value 21613f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 21623f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @attr ref android.R.styleable#GridLayout_Layout_layout_gravity 21633f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 21643f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public void setGravity(int gravity) { 21655125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne rowSpec = rowSpec.copyWriteAlignment(getAlignment(gravity, false)); 21665125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne columnSpec = columnSpec.copyWriteAlignment(getAlignment(gravity, true)); 21673f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 21683f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 21693f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @Override 21703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne protected void setBaseAttributes(TypedArray attributes, int widthAttr, int heightAttr) { 21713f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne this.width = attributes.getLayoutDimension(widthAttr, DEFAULT_WIDTH); 21723f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne this.height = attributes.getLayoutDimension(heightAttr, DEFAULT_HEIGHT); 21733f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 21743f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2175f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final void setRowSpecSpan(Interval span) { 217693cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne rowSpec = rowSpec.copyWriteSpan(span); 21773f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 21783f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2179f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final void setColumnSpecSpan(Interval span) { 218093cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne columnSpec = columnSpec.copyWriteSpan(span); 21813f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 2182d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne 2183d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne @Override 2184d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne public boolean equals(Object o) { 2185d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne if (this == o) return true; 2186d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne if (o == null || getClass() != o.getClass()) return false; 2187d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne 2188d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne LayoutParams that = (LayoutParams) o; 2189d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne 2190d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne if (!columnSpec.equals(that.columnSpec)) return false; 2191d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne if (!rowSpec.equals(that.rowSpec)) return false; 2192d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne 2193d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne return true; 2194d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne } 2195d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne 2196d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne @Override 2197d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne public int hashCode() { 2198d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne int result = rowSpec.hashCode(); 2199d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne result = 31 * result + columnSpec.hashCode(); 2200d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne return result; 2201d7dd89095ff2041f0793317c4ee8e8be49388148Philip Milne } 22023f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 22033f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2204aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne /* 2205aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne In place of a HashMap from span to Int, use an array of key/value pairs - stored in Arcs. 2206aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne Add the mutables completesCycle flag to avoid creating another hash table for detecting cycles. 2207aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne */ 2208f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final static class Arc { 22093f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public final Interval span; 2210aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne public final MutableInt value; 221148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne public boolean valid = true; 22123f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2213aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne public Arc(Interval span, MutableInt value) { 22143f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne this.span = span; 22153f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne this.value = value; 22163f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 22173f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 22183f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @Override 22193f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public String toString() { 222048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return span + " " + (!valid ? "+>" : "->") + " " + value; 22213f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 22223f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 22233f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 22243f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // A mutable Integer - used to avoid heap allocation during the layout operation 22253f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2226f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final static class MutableInt { 22273f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public int value; 22283f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2229f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public MutableInt() { 22303f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne reset(); 22313f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 22323f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2233f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public MutableInt(int value) { 22343f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne this.value = value; 22353f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 22363f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2237f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public void reset() { 22383f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne value = Integer.MIN_VALUE; 22393f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 224048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 224148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne @Override 224248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne public String toString() { 224348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return Integer.toString(value); 224448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 224548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 224648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 2247f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final static class Assoc<K, V> extends ArrayList<Pair<K, V>> { 224848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private final Class<K> keyType; 224948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private final Class<V> valueType; 225048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 225148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne private Assoc(Class<K> keyType, Class<V> valueType) { 225248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne this.keyType = keyType; 225348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne this.valueType = valueType; 225448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 225548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 2256f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public static <K, V> Assoc<K, V> of(Class<K> keyType, Class<V> valueType) { 225748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return new Assoc<K, V>(keyType, valueType); 225848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 225948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 226048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne public void put(K key, V value) { 226148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne add(Pair.create(key, value)); 226248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 226348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 226448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne @SuppressWarnings(value = "unchecked") 226548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne public PackedMap<K, V> pack() { 226648b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne int N = size(); 226748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne K[] keys = (K[]) Array.newInstance(keyType, N); 226848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne V[] values = (V[]) Array.newInstance(valueType, N); 226948b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne for (int i = 0; i < N; i++) { 227048b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne keys[i] = get(i).first; 227148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne values[i] = get(i).second; 227248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 227348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return new PackedMap<K, V>(keys, values); 227448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 22753f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 22763f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2277aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne /* 2278aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne This data structure is used in place of a Map where we have an index that refers to the order 2279aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne in which each key/value pairs were added to the map. In this case we store keys and values 2280aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne in arrays of a length that is equal to the number of unique keys. We also maintain an 2281aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne array of indexes from insertion order to the compacted arrays of keys and values. 2282aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 2283aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne Note that behavior differs from that of a LinkedHashMap in that repeated entries 2284aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne *do* get added multiples times. So the length of index is equals to the number of 2285aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne items added. 2286aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 2287aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne This is useful in the GridLayout class where we can rely on the order of children not 2288aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne changing during layout - to use integer-based lookup for our internal structures 2289aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne rather than using (and storing) an implementation of Map<Key, ?>. 2290aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne */ 22913f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @SuppressWarnings(value = "unchecked") 2292f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final static class PackedMap<K, V> { 22933f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public final int[] index; 22943f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public final K[] keys; 22953f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public final V[] values; 22963f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 22973f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private PackedMap(K[] keys, V[] values) { 22983f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne this.index = createIndex(keys); 22993f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2300aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne this.keys = compact(keys, index); 2301aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne this.values = compact(values, index); 23023f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 23033f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2304f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne public V getValue(int i) { 23053f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return values[index[i]]; 23063f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 23073f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 23083f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private static <K> int[] createIndex(K[] keys) { 23093f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int size = keys.length; 23103f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int[] result = new int[size]; 23113f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 23123f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne Map<K, Integer> keyToIndex = new HashMap<K, Integer>(); 23133f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne for (int i = 0; i < size; i++) { 23143f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne K key = keys[i]; 23153f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne Integer index = keyToIndex.get(key); 23163f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne if (index == null) { 23173f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne index = keyToIndex.size(); 23183f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne keyToIndex.put(key, index); 23193f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 23203f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne result[i] = index; 23213f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 23223f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return result; 23233f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 23243f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2325aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne /* 2326aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne Create a compact array of keys or values using the supplied index. 2327aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne */ 2328aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne private static <K> K[] compact(K[] a, int[] index) { 2329aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne int size = a.length; 2330aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne Class<?> componentType = a.getClass().getComponentType(); 233151f17d54613c33638c8a2da8affcd9ba35994cb3Philip Milne K[] result = (K[]) Array.newInstance(componentType, max2(index, -1) + 1); 23323f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 23333f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne // this overwrite duplicates, retaining the last equivalent entry 23343f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne for (int i = 0; i < size; i++) { 2335aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne result[index[i]] = a[i]; 23363f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 23373f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return result; 23383f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 23393f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 23403f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2341aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne /* 234293cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne For each group (with a given alignment) we need to store the amount of space required 23437fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne before the alignment point and the amount of space required after it. One side of this 234447d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio calculation is always 0 for START and END alignments but we don't make use of this. 2345aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne For CENTER and BASELINE alignments both sides are needed and in the BASELINE case no 2346aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne simple optimisations are possible. 2347aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 2348aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne The general algorithm therefore is to create a Map (actually a PackedMap) from 234993cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne group to Bounds and to loop through all Views in the group taking the maximum 2350aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne of the values for each View. 2351aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne */ 2352f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne static class Bounds { 23537fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne public int before; 23547fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne public int after; 23555125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne public int flexibility; // we're flexible iff all included specs are flexible 23563f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 23573f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne private Bounds() { 23583f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne reset(); 23593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 23603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2361a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne protected void reset() { 23627fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne before = Integer.MIN_VALUE; 23637fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne after = Integer.MIN_VALUE; 23645125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne flexibility = CAN_STRETCH; // from the above, we're flexible when empty 23653f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 23663f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2367a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne protected void include(int before, int after) { 23687fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne this.before = max(this.before, before); 23697fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne this.after = max(this.after, after); 23703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 23713f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 237248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne protected int size(boolean min) { 23735d1a9840aaf57ae90716f0ac34abdcd09f7f4ed6Philip Milne if (!min) { 23745125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne if (canStretch(flexibility)) { 23755d1a9840aaf57ae90716f0ac34abdcd09f7f4ed6Philip Milne return MAX_SIZE; 23765d1a9840aaf57ae90716f0ac34abdcd09f7f4ed6Philip Milne } 237748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 23784885f2f773915a48d0e2f67bc604b081327372a0Philip Milne return before + after; 23793f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 23803f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 23811557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne protected int getOffset(GridLayout gl, View c, Alignment a, int size, boolean horizontal) { 23827a23b49a8ceb07d3fa12c45fd42cd16131fd746aPhilip Milne return before - a.getAlignmentValue(c, size, gl.getLayoutMode()); 238348b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne } 238448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 23859505480ee37494a33e12565db4febfb18b6d7494Philip Milne protected final void include(GridLayout gl, View c, Spec spec, Axis axis, int size) { 23865125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne this.flexibility &= spec.getFlexibility(); 23871557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne boolean horizontal = axis.horizontal; 23886dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar Alignment alignment = spec.getAbsoluteAlignment(axis.horizontal); 23894c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne // todo test this works correctly when the returned value is UNDEFINED 23907a23b49a8ceb07d3fa12c45fd42cd16131fd746aPhilip Milne int before = alignment.getAlignmentValue(c, size, gl.getLayoutMode()); 239148b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne include(before, size - before); 2392a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne } 2393a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne 23943f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @Override 23953f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public String toString() { 23963f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return "Bounds{" + 23977fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne "before=" + before + 23987fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne ", after=" + after + 23993f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne '}'; 24003f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 24013f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 24023f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 24033f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 24043f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * An Interval represents a contiguous range of values that lie between 24053f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * the interval's {@link #min} and {@link #max} values. 24063f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <p> 24073f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Intervals are immutable so may be passed as values and used as keys in hash tables. 24083f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * It is not necessary to have multiple instances of Intervals which have the same 24093f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * {@link #min} and {@link #max} values. 24103f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <p> 24117fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * Intervals are often written as {@code [min, max]} and represent the set of values 24127fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * {@code x} such that {@code min <= x < max}. 24133f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 2414f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final static class Interval { 24153f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 24163f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * The minimum value. 24173f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 24183f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public final int min; 2419aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne 24203f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 24213f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * The maximum value. 24223f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 24233f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public final int max; 24243f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 24253f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 24267fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * Construct a new Interval, {@code interval}, where: 24273f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <ul> 24287fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * <li> {@code interval.min = min} </li> 24297fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * <li> {@code interval.max = max} </li> 24303f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * </ul> 24313f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 24323f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @param min the minimum value. 24333f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @param max the maximum value. 24343f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 24353f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public Interval(int min, int max) { 24363f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne this.min = min; 24373f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne this.max = max; 24383f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 24393f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2440f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne int size() { 24413f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return max - min; 24423f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 24433f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2444f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne Interval inverse() { 24453f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return new Interval(max, min); 24463f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 24473f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 24483f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 24497fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * Returns {@code true} if the {@link #getClass class}, 24507fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * {@link #min} and {@link #max} properties of this Interval and the 24517fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * supplied parameter are pairwise equal; {@code false} otherwise. 24523f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 24537fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * @param that the object to compare this interval with 24543f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 24553f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @return {@code true} if the specified object is equal to this 24567fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * {@code Interval}, {@code false} otherwise. 24573f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 24583f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @Override 24593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public boolean equals(Object that) { 24603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne if (this == that) { 24613f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return true; 24623f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 24633f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne if (that == null || getClass() != that.getClass()) { 24643f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return false; 24653f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 24663f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 24673f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne Interval interval = (Interval) that; 24683f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 24693f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne if (max != interval.max) { 24703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return false; 24713f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 24724c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne //noinspection RedundantIfStatement 24733f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne if (min != interval.min) { 24743f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return false; 24753f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 24763f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 24773f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return true; 24783f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 24793f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 24803f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @Override 24813f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public int hashCode() { 24823f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int result = min; 24833f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne result = 31 * result + max; 24843f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return result; 24853f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 24863f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 24873f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @Override 24883f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public String toString() { 24893f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return "[" + min + ", " + max + "]"; 24903f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 24913f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 24923f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2493899d5922870c78e0e663bc5661849eb468afc984Philip Milne /** 2494899d5922870c78e0e663bc5661849eb468afc984Philip Milne * A Spec defines the horizontal or vertical characteristics of a group of 24954a145d72622772b920f60195e80942058984259cPhilip Milne * cells. Each spec. defines the <em>grid indices</em> and <em>alignment</em> 24964a145d72622772b920f60195e80942058984259cPhilip Milne * along the appropriate axis. 2497899d5922870c78e0e663bc5661849eb468afc984Philip Milne * <p> 2498899d5922870c78e0e663bc5661849eb468afc984Philip Milne * The <em>grid indices</em> are the leading and trailing edges of this cell group. 2499899d5922870c78e0e663bc5661849eb468afc984Philip Milne * See {@link GridLayout} for a description of the conventions used by GridLayout 2500899d5922870c78e0e663bc5661849eb468afc984Philip Milne * for grid indices. 2501899d5922870c78e0e663bc5661849eb468afc984Philip Milne * <p> 2502899d5922870c78e0e663bc5661849eb468afc984Philip Milne * The <em>alignment</em> property specifies how cells should be aligned in this group. 2503899d5922870c78e0e663bc5661849eb468afc984Philip Milne * For row groups, this specifies the vertical alignment. 2504899d5922870c78e0e663bc5661849eb468afc984Philip Milne * For column groups, this specifies the horizontal alignment. 25054a145d72622772b920f60195e80942058984259cPhilip Milne * <p> 25064a145d72622772b920f60195e80942058984259cPhilip Milne * Use the following static methods to create specs: 25074a145d72622772b920f60195e80942058984259cPhilip Milne * <ul> 25084a145d72622772b920f60195e80942058984259cPhilip Milne * <li>{@link #spec(int)}</li> 25094a145d72622772b920f60195e80942058984259cPhilip Milne * <li>{@link #spec(int, int)}</li> 25104a145d72622772b920f60195e80942058984259cPhilip Milne * <li>{@link #spec(int, Alignment)}</li> 25114a145d72622772b920f60195e80942058984259cPhilip Milne * <li>{@link #spec(int, int, Alignment)}</li> 251287260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * <li>{@link #spec(int, float)}</li> 251387260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * <li>{@link #spec(int, int, float)}</li> 251487260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * <li>{@link #spec(int, Alignment, float)}</li> 251587260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * <li>{@link #spec(int, int, Alignment, float)}</li> 25164a145d72622772b920f60195e80942058984259cPhilip Milne * </ul> 25174a145d72622772b920f60195e80942058984259cPhilip Milne * 2518899d5922870c78e0e663bc5661849eb468afc984Philip Milne */ 251993cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne public static class Spec { 2520f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne static final Spec UNDEFINED = spec(GridLayout.UNDEFINED); 252187260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne static final float DEFAULT_WEIGHT = 0; 2522f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne 2523f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final boolean startDefined; 252448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne final Interval span; 252593cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne final Alignment alignment; 252687260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne final float weight; 25273f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 252887260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne private Spec(boolean startDefined, Interval span, Alignment alignment, float weight) { 2529f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne this.startDefined = startDefined; 25305d1a9840aaf57ae90716f0ac34abdcd09f7f4ed6Philip Milne this.span = span; 25315d1a9840aaf57ae90716f0ac34abdcd09f7f4ed6Philip Milne this.alignment = alignment; 253287260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne this.weight = weight; 25335d1a9840aaf57ae90716f0ac34abdcd09f7f4ed6Philip Milne } 25345d1a9840aaf57ae90716f0ac34abdcd09f7f4ed6Philip Milne 253587260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne private Spec(boolean startDefined, int start, int size, Alignment alignment, float weight) { 253687260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne this(startDefined, new Interval(start, start + size), alignment, weight); 25373f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 25383f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 253938d909a57d95d29e6c47f40d360bc10325d7b228Yigit Boyar private Alignment getAbsoluteAlignment(boolean horizontal) { 25406dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar if (alignment != UNDEFINED_ALIGNMENT) { 25416dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar return alignment; 25426dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar } 25436dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar if (weight == 0f) { 25446dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar return horizontal ? START : BASELINE; 25456dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar } 25466dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar return FILL; 25476dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar } 25486dafd87fb4174447018b044bc67818d54fab57d8Yigit Boyar 2549f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final Spec copyWriteSpan(Interval span) { 255087260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne return new Spec(startDefined, span, alignment, weight); 25513f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 25523f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2553f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final Spec copyWriteAlignment(Alignment alignment) { 255487260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne return new Spec(startDefined, span, alignment, weight); 25555125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne } 25565125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne 2557f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne final int getFlexibility() { 255887260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne return (alignment == UNDEFINED_ALIGNMENT && weight == 0) ? INFLEXIBLE : CAN_STRETCH; 25593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 25603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 25613f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 256293cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * Returns {@code true} if the {@code class}, {@code alignment} and {@code span} 256393cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * properties of this Spec and the supplied parameter are pairwise equal, 25647fd948756947506db62a2bafca2ed45ff53ac0a0Philip Milne * {@code false} otherwise. 25653f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 256693cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * @param that the object to compare this spec with 25673f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 25683f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @return {@code true} if the specified object is equal to this 256993cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * {@code Spec}; {@code false} otherwise 25703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 25713f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @Override 25723f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public boolean equals(Object that) { 25733f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne if (this == that) { 25743f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return true; 25753f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 25763f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne if (that == null || getClass() != that.getClass()) { 25773f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return false; 25783f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 25793f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 258093cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne Spec spec = (Spec) that; 25813f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 258293cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne if (!alignment.equals(spec.alignment)) { 25833f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return false; 25843f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 25854c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne //noinspection RedundantIfStatement 258693cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne if (!span.equals(spec.span)) { 25873f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return false; 25883f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 25893f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 25903f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return true; 25913f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 25923f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 25933f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne @Override 25943f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public int hashCode() { 25953f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int result = span.hashCode(); 25963f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne result = 31 * result + alignment.hashCode(); 25973f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return result; 25983f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 25993f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 26003f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 26013f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 260293cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * Return a Spec, {@code spec}, where: 260393cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * <ul> 260493cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * <li> {@code spec.span = [start, start + size]} </li> 260593cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * <li> {@code spec.alignment = alignment} </li> 260687260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * <li> {@code spec.weight = weight} </li> 260793cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * </ul> 26087b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne * <p> 26097b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne * To leave the start index undefined, use the value {@link #UNDEFINED}. 261093cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * 261193cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * @param start the start 261293cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * @param size the size 261393cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * @param alignment the alignment 261487260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * @param weight the weight 261587260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne */ 261687260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne public static Spec spec(int start, int size, Alignment alignment, float weight) { 261787260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne return new Spec(start != UNDEFINED, start, size, alignment, weight); 261887260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne } 261987260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne 262087260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne /** 262187260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * Equivalent to: {@code spec(start, 1, alignment, weight)}. 262287260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * 262387260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * @param start the start 262487260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * @param alignment the alignment 262587260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * @param weight the weight 262687260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne */ 262787260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne public static Spec spec(int start, Alignment alignment, float weight) { 262887260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne return spec(start, 1, alignment, weight); 262987260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne } 263087260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne 263187260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne /** 263287260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * Equivalent to: {@code spec(start, 1, default_alignment, weight)} - 263387260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * where {@code default_alignment} is specified in 263487260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * {@link android.widget.GridLayout.LayoutParams}. 263587260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * 263687260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * @param start the start 263787260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * @param size the size 263887260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * @param weight the weight 263987260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne */ 264087260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne public static Spec spec(int start, int size, float weight) { 264187260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne return spec(start, size, UNDEFINED_ALIGNMENT, weight); 264287260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne } 264387260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne 264487260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne /** 264587260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * Equivalent to: {@code spec(start, 1, weight)}. 264687260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * 264787260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * @param start the start 264887260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * @param weight the weight 264987260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne */ 265087260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne public static Spec spec(int start, float weight) { 265187260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne return spec(start, 1, weight); 265287260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne } 265387260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne 265487260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne /** 265587260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * Equivalent to: {@code spec(start, size, alignment, 0f)}. 265687260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * 265787260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * @param start the start 265887260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * @param size the size 265987260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne * @param alignment the alignment 266093cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne */ 266193cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne public static Spec spec(int start, int size, Alignment alignment) { 266287260844afc3ff2adf63b156c052e15a2f8c68f0Philip Milne return spec(start, size, alignment, Spec.DEFAULT_WEIGHT); 266393cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne } 266493cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne 266593cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne /** 266693cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * Return a Spec, {@code spec}, where: 266793cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * <ul> 266893cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * <li> {@code spec.span = [start, start + 1]} </li> 266993cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * <li> {@code spec.alignment = alignment} </li> 267093cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * </ul> 26717b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne * <p> 26727b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne * To leave the start index undefined, use the value {@link #UNDEFINED}. 267393cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * 267493cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * @param start the start index 267593cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * @param alignment the alignment 26767b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne * 26777b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne * @see #spec(int, int, Alignment) 267893cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne */ 267993cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne public static Spec spec(int start, Alignment alignment) { 268093cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne return spec(start, 1, alignment); 268193cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne } 268293cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne 268393cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne /** 26845125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne * Return a Spec, {@code spec}, where: 26855125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne * <ul> 26865125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne * <li> {@code spec.span = [start, start + size]} </li> 26875125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne * </ul> 26887b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne * <p> 26897b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne * To leave the start index undefined, use the value {@link #UNDEFINED}. 26905125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne * 26915125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne * @param start the start 26925125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne * @param size the size 26937b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne * 26947b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne * @see #spec(int, Alignment) 26955125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne */ 26965125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne public static Spec spec(int start, int size) { 26975125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne return spec(start, size, UNDEFINED_ALIGNMENT); 26985125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne } 26995125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne 27005125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne /** 27015125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne * Return a Spec, {@code spec}, where: 27025125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne * <ul> 27035125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne * <li> {@code spec.span = [start, start + 1]} </li> 27045125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne * </ul> 27057b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne * <p> 27067b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne * To leave the start index undefined, use the value {@link #UNDEFINED}. 27075125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne * 27085125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne * @param start the start index 27097b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne * 27107b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne * @see #spec(int, int) 27115125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne */ 27125125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne public static Spec spec(int start) { 27135125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne return spec(start, 1); 27145125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne } 27155125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne 27165125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne /** 27173f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Alignments specify where a view should be placed within a cell group and 27183f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * what size it should be. 27193f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <p> 272093cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * The {@link LayoutParams} class contains a {@link LayoutParams#rowSpec rowSpec} 272193cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * and a {@link LayoutParams#columnSpec columnSpec} each of which contains an 272293cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * {@code alignment}. Overall placement of the view in the cell 27233f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * group is specified by the two alignments which act along each axis independently. 27243f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <p> 2725a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne * The GridLayout class defines the most common alignments used in general layout: 27266216e87fe8ad3273855233965b34049d22763e94Philip Milne * {@link #TOP}, {@link #LEFT}, {@link #BOTTOM}, {@link #RIGHT}, {@link #START}, 27276216e87fe8ad3273855233965b34049d22763e94Philip Milne * {@link #END}, {@link #CENTER}, {@link #BASELINE} and {@link #FILL}. 2728a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne */ 2729a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne /* 2730c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne * An Alignment implementation must define {@link #getAlignmentValue(View, int, int)}, 27313f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * to return the appropriate value for the type of alignment being defined. 27323f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * The enclosing algorithms position the children 27331e54825135a7ccde421aa7fc400ab69e9348b5d6Philip Milne * so that the locations defined by the alignment values 27343f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * are the same for all of the views in a group. 27353f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * <p> 27363f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 2737c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne public static abstract class Alignment { 273848b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne Alignment() { 2739a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne } 2740a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne 27416216e87fe8ad3273855233965b34049d22763e94Philip Milne abstract int getGravityOffset(View view, int cellDelta); 27426216e87fe8ad3273855233965b34049d22763e94Philip Milne 27433f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 27443f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Returns an alignment value. In the case of vertical alignments the value 27453f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * returned should indicate the distance from the top of the view to the 27463f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * alignment location. 27473f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * For horizontal alignments measurement is made from the left edge of the component. 27483f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 2749c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne * @param view the view to which this alignment should be applied 2750c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne * @param viewSize the measured size of the view 27517a23b49a8ceb07d3fa12c45fd42cd16131fd746aPhilip Milne * @param mode the basis of alignment: CLIP or OPTICAL 2752b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne * @return the alignment value 27533f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 27547a23b49a8ceb07d3fa12c45fd42cd16131fd746aPhilip Milne abstract int getAlignmentValue(View view, int viewSize, int mode); 27553f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 27563f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 27573f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Returns the size of the view specified by this alignment. 27583f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * In the case of vertical alignments this method should return a height; for 27593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * horizontal alignments this method should return the width. 2760c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne * <p> 2761c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne * The default implementation returns {@code viewSize}. 2762c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne * 2763c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne * @param view the view to which this alignment should be applied 2764c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne * @param viewSize the measured size of the view 2765c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne * @param cellSize the size of the cell into which this view will be placed 2766b3a8c54389c9be2b37c5524ad8eb3112054221a7Philip Milne * @return the aligned size 27673f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 27686216e87fe8ad3273855233965b34049d22763e94Philip Milne int getSizeInCell(View view, int viewSize, int cellSize) { 27693f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return viewSize; 27703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 2771a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne 277248b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne Bounds getBounds() { 2773a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne return new Bounds(); 2774a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne } 27753f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 27763f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2777f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne static final Alignment UNDEFINED_ALIGNMENT = new Alignment() { 277847d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio @Override 27796216e87fe8ad3273855233965b34049d22763e94Philip Milne int getGravityOffset(View view, int cellDelta) { 27806216e87fe8ad3273855233965b34049d22763e94Philip Milne return UNDEFINED; 27816216e87fe8ad3273855233965b34049d22763e94Philip Milne } 27826216e87fe8ad3273855233965b34049d22763e94Philip Milne 27836216e87fe8ad3273855233965b34049d22763e94Philip Milne @Override 27847a23b49a8ceb07d3fa12c45fd42cd16131fd746aPhilip Milne public int getAlignmentValue(View view, int viewSize, int mode) { 27855125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne return UNDEFINED; 27865125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne } 27875125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne }; 27885125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne 278947d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio /** 279047d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio * Indicates that a view should be aligned with the <em>start</em> 279147d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio * edges of the other views in its cell group. 279247d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio */ 2793c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne private static final Alignment LEADING = new Alignment() { 279447d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio @Override 27956216e87fe8ad3273855233965b34049d22763e94Philip Milne int getGravityOffset(View view, int cellDelta) { 27966216e87fe8ad3273855233965b34049d22763e94Philip Milne return 0; 27976216e87fe8ad3273855233965b34049d22763e94Philip Milne } 27986216e87fe8ad3273855233965b34049d22763e94Philip Milne 27996216e87fe8ad3273855233965b34049d22763e94Philip Milne @Override 28007a23b49a8ceb07d3fa12c45fd42cd16131fd746aPhilip Milne public int getAlignmentValue(View view, int viewSize, int mode) { 28013f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return 0; 28023f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 28033f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne }; 28043f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 280547d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio /** 280647d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio * Indicates that a view should be aligned with the <em>end</em> 280747d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio * edges of the other views in its cell group. 280847d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio */ 2809c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne private static final Alignment TRAILING = new Alignment() { 281047d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio @Override 28116216e87fe8ad3273855233965b34049d22763e94Philip Milne int getGravityOffset(View view, int cellDelta) { 28126216e87fe8ad3273855233965b34049d22763e94Philip Milne return cellDelta; 28136216e87fe8ad3273855233965b34049d22763e94Philip Milne } 28146216e87fe8ad3273855233965b34049d22763e94Philip Milne 28156216e87fe8ad3273855233965b34049d22763e94Philip Milne @Override 28167a23b49a8ceb07d3fa12c45fd42cd16131fd746aPhilip Milne public int getAlignmentValue(View view, int viewSize, int mode) { 28173f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return viewSize; 28183f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 28193f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne }; 28203f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 28213f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 28223f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Indicates that a view should be aligned with the <em>top</em> 28233f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * edges of the other views in its cell group. 28243f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 28253f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public static final Alignment TOP = LEADING; 28263f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 28273f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 28283f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Indicates that a view should be aligned with the <em>bottom</em> 28293f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * edges of the other views in its cell group. 28303f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 28313f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public static final Alignment BOTTOM = TRAILING; 28323f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 28333f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 283447d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio * Indicates that a view should be aligned with the <em>start</em> 28353f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * edges of the other views in its cell group. 28363f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 283747d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio public static final Alignment START = LEADING; 283847d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio 283947d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio /** 284047d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio * Indicates that a view should be aligned with the <em>end</em> 284147d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio * edges of the other views in its cell group. 284247d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio */ 284347d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio public static final Alignment END = TRAILING; 284447d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio 28456216e87fe8ad3273855233965b34049d22763e94Philip Milne private static Alignment createSwitchingAlignment(final Alignment ltr, final Alignment rtl) { 284647d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio return new Alignment() { 284747d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio @Override 28486216e87fe8ad3273855233965b34049d22763e94Philip Milne int getGravityOffset(View view, int cellDelta) { 28496216e87fe8ad3273855233965b34049d22763e94Philip Milne return (!view.isLayoutRtl() ? ltr : rtl).getGravityOffset(view, cellDelta); 28506216e87fe8ad3273855233965b34049d22763e94Philip Milne } 28516216e87fe8ad3273855233965b34049d22763e94Philip Milne 28526216e87fe8ad3273855233965b34049d22763e94Philip Milne @Override 28537a23b49a8ceb07d3fa12c45fd42cd16131fd746aPhilip Milne public int getAlignmentValue(View view, int viewSize, int mode) { 28547a23b49a8ceb07d3fa12c45fd42cd16131fd746aPhilip Milne return (!view.isLayoutRtl() ? ltr : rtl).getAlignmentValue(view, viewSize, mode); 285547d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio } 285647d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio }; 285747d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio } 28583f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 28593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 28603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Indicates that a view should be aligned with the <em>left</em> 28613f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * edges of the other views in its cell group. 28623f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 28636216e87fe8ad3273855233965b34049d22763e94Philip Milne public static final Alignment LEFT = createSwitchingAlignment(START, END); 286447d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio 286547d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio /** 286647d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio * Indicates that a view should be aligned with the <em>right</em> 286747d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio * edges of the other views in its cell group. 286847d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio */ 28696216e87fe8ad3273855233965b34049d22763e94Philip Milne public static final Alignment RIGHT = createSwitchingAlignment(END, START); 28703f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 28713f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 28723f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Indicates that a view should be <em>centered</em> with the other views in its cell group. 287393cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * This constant may be used in both {@link LayoutParams#rowSpec rowSpecs} and {@link 287493cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * LayoutParams#columnSpec columnSpecs}. 28753f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 2876c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne public static final Alignment CENTER = new Alignment() { 287747d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio @Override 28786216e87fe8ad3273855233965b34049d22763e94Philip Milne int getGravityOffset(View view, int cellDelta) { 28796216e87fe8ad3273855233965b34049d22763e94Philip Milne return cellDelta >> 1; 28806216e87fe8ad3273855233965b34049d22763e94Philip Milne } 28816216e87fe8ad3273855233965b34049d22763e94Philip Milne 28826216e87fe8ad3273855233965b34049d22763e94Philip Milne @Override 28837a23b49a8ceb07d3fa12c45fd42cd16131fd746aPhilip Milne public int getAlignmentValue(View view, int viewSize, int mode) { 28843f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return viewSize >> 1; 28853f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 28863f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne }; 28873f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 28883f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 28893f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Indicates that a view should be aligned with the <em>baselines</em> 28903f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * of the other views in its cell group. 289193cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * This constant may only be used as an alignment in {@link LayoutParams#rowSpec rowSpecs}. 28923f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * 28933f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * @see View#getBaseline() 28943f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 2895c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne public static final Alignment BASELINE = new Alignment() { 289647d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio @Override 28976216e87fe8ad3273855233965b34049d22763e94Philip Milne int getGravityOffset(View view, int cellDelta) { 28986216e87fe8ad3273855233965b34049d22763e94Philip Milne return 0; // baseline gravity is top 28996216e87fe8ad3273855233965b34049d22763e94Philip Milne } 29006216e87fe8ad3273855233965b34049d22763e94Philip Milne 29016216e87fe8ad3273855233965b34049d22763e94Philip Milne @Override 29027a23b49a8ceb07d3fa12c45fd42cd16131fd746aPhilip Milne public int getAlignmentValue(View view, int viewSize, int mode) { 2903a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne if (view.getVisibility() == GONE) { 2904a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne return 0; 2905a841644367772f51d4ac5f2af2a14cc2d0e36df1Philip Milne } 29063f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne int baseline = view.getBaseline(); 29077b7578184567f4e4f0740ce935cc192765410ccaPhilip Milne return baseline == -1 ? UNDEFINED : baseline; 2908a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne } 2909a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne 2910a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne @Override 2911a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne public Bounds getBounds() { 2912a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne return new Bounds() { 2913a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne /* 2914a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne In a baseline aligned row in which some components define a baseline 2915a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne and some don't, we need a third variable to properly account for all 2916a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne the sizes. This tracks the maximum size of all the components - 2917a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne including those that don't define a baseline. 2918a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne */ 2919a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne private int size; 2920a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne 2921a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne @Override 2922a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne protected void reset() { 2923a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne super.reset(); 292448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne size = Integer.MIN_VALUE; 2925a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne } 2926a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne 2927a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne @Override 2928a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne protected void include(int before, int after) { 2929a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne super.include(before, after); 2930a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne size = max(size, before + after); 2931a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne } 2932a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne 2933a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne @Override 293448b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne protected int size(boolean min) { 293548b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne return max(super.size(min), size); 2936a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne } 2937a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne 2938a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne @Override 29391557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne protected int getOffset(GridLayout gl, View c, Alignment a, int size, boolean hrz) { 29401557fd7809078e421f751efc7d2539b3efdc54b2Philip Milne return max(0, super.getOffset(gl, c, a, size, hrz)); 2941a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne } 2942a1f7b10f7299b40ee3a4e5e309882ea1a931cd5ePhilip Milne }; 29433f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 29443f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne }; 29453f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 29463f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne /** 29473f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne * Indicates that a view should expanded to fit the boundaries of its cell group. 294893cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * This constant may be used in both {@link LayoutParams#rowSpec rowSpecs} and 294993cd6a6c78683643de51f9e698b38847bd1f1155Philip Milne * {@link LayoutParams#columnSpec columnSpecs}. 29503f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne */ 29513f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne public static final Alignment FILL = new Alignment() { 295247d248eb438674ab0ca10154f3ff5e88f4ed70b7Fabrice Di Meglio @Override 29536216e87fe8ad3273855233965b34049d22763e94Philip Milne int getGravityOffset(View view, int cellDelta) { 29546216e87fe8ad3273855233965b34049d22763e94Philip Milne return 0; 29556216e87fe8ad3273855233965b34049d22763e94Philip Milne } 29566216e87fe8ad3273855233965b34049d22763e94Philip Milne 29576216e87fe8ad3273855233965b34049d22763e94Philip Milne @Override 29587a23b49a8ceb07d3fa12c45fd42cd16131fd746aPhilip Milne public int getAlignmentValue(View view, int viewSize, int mode) { 29593f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return UNDEFINED; 29603f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 29613f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne 2962c9885f6557dc1c96e2cc2c1a86fba359f00f131cPhilip Milne @Override 29636216e87fe8ad3273855233965b34049d22763e94Philip Milne public int getSizeInCell(View view, int viewSize, int cellSize) { 29643f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne return cellSize; 29653f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne } 29663f8956d82bb40b15acee26017db0d13ddf43c80aPhilip Milne }; 296748b55244d286b6d4e3699a5d9e938a9c87aaae75Philip Milne 2968f6679c88d12e9f7e10e6884d4a8487673e53c097Philip Milne static boolean canStretch(int flexibility) { 29695d1a9840aaf57ae90716f0ac34abdcd09f7f4ed6Philip Milne return (flexibility & CAN_STRETCH) != 0; 29705d1a9840aaf57ae90716f0ac34abdcd09f7f4ed6Philip Milne } 29715d1a9840aaf57ae90716f0ac34abdcd09f7f4ed6Philip Milne 29725125e21bc0bbe5b9718d0f03b26cdafc67a7c726Philip Milne private static final int INFLEXIBLE = 0; 29734c8cf4c93314722a77ce69396b9cb201ac007a58Philip Milne private static final int CAN_STRETCH = 2; 2974452eec33d667f9e705b57e60948b070536fbc1b4Jim Miller} 2975