Created
June 23, 2014 19:26
-
-
Save pskink/4918ec060a244540dcca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
<declare-styleable name="GridSpanLayout"> | |
<attr name="rows" format="integer" /> | |
<attr name="columns" format="integer" /> | |
<attr name="column" format="integer" /> | |
<attr name="row" format="integer" /> | |
<attr name="columnSpan" format="integer" /> | |
<attr name="rowSpan" format="integer" /> | |
</declare-styleable> | |
* | |
*/ | |
public class GridSpanLayout extends ViewGroup { | |
private final static String TAG = "GridSpanLayout"; | |
private int mColumns; | |
private int mRows; | |
private int mIndex; | |
public GridSpanLayout(Context context) { | |
super(context); | |
} | |
public GridSpanLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.GridSpanLayout); | |
mColumns = a.getInt(R.styleable.GridSpanLayout_columns, 0); | |
mRows = a.getInt(R.styleable.GridSpanLayout_rows, 0); | |
a.recycle(); | |
} | |
@Override | |
public LayoutParams generateLayoutParams(AttributeSet attrs) { | |
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.GridSpanLayout); | |
try { | |
int c = a.getInt(R.styleable.GridSpanLayout_column, -1); | |
int r = a.getInt(R.styleable.GridSpanLayout_row, -1); | |
if (c != -1 && r != -1) { | |
mIndex = c + r * mColumns; | |
} else { | |
if (c != -1) { | |
throw new RuntimeException(attrs.getPositionDescription() + ": alone column definition"); | |
} | |
if (r != -1) { | |
throw new RuntimeException(attrs.getPositionDescription() + ": alone row definition"); | |
} | |
c = mIndex % mColumns; | |
r = mIndex / mColumns; | |
} | |
mIndex++; | |
int cs = a.getInt(R.styleable.GridSpanLayout_columnSpan, 1); | |
int rs = a.getInt(R.styleable.GridSpanLayout_rowSpan, 1); | |
return new LayoutParams(c, r, cs, rs); | |
} finally { | |
a.recycle(); | |
} | |
} | |
@Override | |
protected void onLayout(boolean changed, int l, int t, int r, int b) { | |
float xs = ((float) getWidth()) / mColumns; | |
float ys = ((float) getHeight()) / mRows; | |
int cnt = getChildCount(); | |
for (int i = 0; i < cnt; i++) { | |
View child = getChildAt(i); | |
LayoutParams params = (LayoutParams) child.getLayoutParams(); | |
// Log.d(TAG, "onLayout " + params); | |
child.layout(Math.round(params.rect.left * xs), Math.round(params.rect.top * ys), | |
Math.round(params.rect.right * xs), Math.round(params.rect.bottom * ys)); | |
} | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
float xs = ((float) getWidth()) / mColumns; | |
float ys = ((float) getHeight()) / mRows; | |
int cnt = getChildCount(); | |
for (int i = 0; i < cnt; i++) { | |
View child = getChildAt(i); | |
LayoutParams params = (LayoutParams) child.getLayoutParams(); | |
widthMeasureSpec = MeasureSpec.makeMeasureSpec(Math.round(params.rect.width() * xs), MeasureSpec.EXACTLY); | |
heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.round(params.rect.height() * ys), MeasureSpec.EXACTLY); | |
child.measure(widthMeasureSpec, heightMeasureSpec); | |
} | |
} | |
class LayoutParams extends ViewGroup.LayoutParams { | |
private Rect rect; | |
public LayoutParams(int c, int r, int cs, int rs) { | |
super(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); | |
rect = new Rect(c, r, c + cs, r + rs); | |
} | |
@Override | |
public String toString() { | |
if (rect.width() == 1 && rect.height() == 1) { | |
return rect.left + ":" + rect.top; | |
} | |
return rect.left + ":" + rect.top + ", " + rect.width() + "x" + rect.height(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment