ColorPickerPalettepublic class ColorPickerPalette extends android.widget.TableLayout A color picker custom view which creates an grid of color squares. The number of squares per
row (and the padding between the squares) is determined by the user. |
Fields Summary |
---|
public com.android.colorpicker.ColorPickerSwatch.OnColorSelectedListener | mOnColorSelectedListener | private String | mDescription | private String | mDescriptionSelected | private int | mSwatchLength | private int | mMarginSize | private int | mNumColumns |
Methods Summary |
---|
private static void | addSwatchToRow(android.widget.TableRow row, android.view.View swatch, int rowNumber)Appends a swatch to the end of the row for even-numbered rows (starting with row 0),
to the beginning of a row for odd-numbered rows.
if (rowNumber % 2 == 0) {
row.addView(swatch);
} else {
row.addView(swatch, 0);
}
| private android.widget.ImageView | createBlankSpace()Creates a blank space to fill the row.
ImageView view = new ImageView(getContext());
TableRow.LayoutParams params = new TableRow.LayoutParams(mSwatchLength, mSwatchLength);
params.setMargins(mMarginSize, mMarginSize, mMarginSize, mMarginSize);
view.setLayoutParams(params);
return view;
| private ColorPickerSwatch | createColorSwatch(int color, int selectedColor)Creates a color swatch.
ColorPickerSwatch view = new ColorPickerSwatch(getContext(), color,
color == selectedColor, mOnColorSelectedListener);
TableRow.LayoutParams params = new TableRow.LayoutParams(mSwatchLength, mSwatchLength);
params.setMargins(mMarginSize, mMarginSize, mMarginSize, mMarginSize);
view.setLayoutParams(params);
return view;
| private android.widget.TableRow | createTableRow()
TableRow row = new TableRow(getContext());
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
row.setLayoutParams(params);
return row;
| public void | drawPalette(int[] colors, int selectedColor)Adds swatches to table in a serpentine format.
if (colors == null) {
return;
}
this.removeAllViews();
int tableElements = 0;
int rowElements = 0;
int rowNumber = 0;
// Fills the table with swatches based on the array of colors.
TableRow row = createTableRow();
for (int color : colors) {
tableElements++;
View colorSwatch = createColorSwatch(color, selectedColor);
setSwatchDescription(rowNumber, tableElements, rowElements, color == selectedColor,
colorSwatch);
addSwatchToRow(row, colorSwatch, rowNumber);
rowElements++;
if (rowElements == mNumColumns) {
addView(row);
row = createTableRow();
rowElements = 0;
rowNumber++;
}
}
// Create blank views to fill the row if the last row has not been filled.
if (rowElements > 0) {
while (rowElements != mNumColumns) {
addSwatchToRow(row, createBlankSpace(), rowNumber);
rowElements++;
}
addView(row);
}
| public void | init(int size, int columns, com.android.colorpicker.ColorPickerSwatch.OnColorSelectedListener listener)Initialize the size, columns, and listener. Size should be a pre-defined size (SIZE_LARGE
or SIZE_SMALL) from ColorPickerDialogFragment.
mNumColumns = columns;
Resources res = getResources();
if (size == ColorPickerDialog.SIZE_LARGE) {
mSwatchLength = res.getDimensionPixelSize(R.dimen.color_swatch_large);
mMarginSize = res.getDimensionPixelSize(R.dimen.color_swatch_margins_large);
} else {
mSwatchLength = res.getDimensionPixelSize(R.dimen.color_swatch_small);
mMarginSize = res.getDimensionPixelSize(R.dimen.color_swatch_margins_small);
}
mOnColorSelectedListener = listener;
mDescription = res.getString(R.string.color_swatch_description);
mDescriptionSelected = res.getString(R.string.color_swatch_description_selected);
| private void | setSwatchDescription(int rowNumber, int index, int rowElements, boolean selected, android.view.View swatch)Add a content description to the specified swatch view. Because the colors get added in a
snaking form, every other row will need to compensate for the fact that the colors are added
in an opposite direction from their left->right/top->bottom order, which is how the system
will arrange them for accessibility purposes.
int accessibilityIndex;
if (rowNumber % 2 == 0) {
// We're in a regular-ordered row
accessibilityIndex = index;
} else {
// We're in a backwards-ordered row.
int rowMax = ((rowNumber + 1) * mNumColumns);
accessibilityIndex = rowMax - rowElements;
}
String description;
if (selected) {
description = String.format(mDescriptionSelected, accessibilityIndex);
} else {
description = String.format(mDescription, accessibilityIndex);
}
swatch.setContentDescription(description);
|
|