1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 package org.chromium.ui;
6 import android.content.Context;
7 import android.graphics.Canvas;
8 import android.graphics.Color;
9 import android.graphics.Paint;
10 import android.graphics.Rect;
11 import android.util.AttributeSet;
12 import android.view.MotionEvent;
13 import android.view.View;
17 * Draws a grid of (predefined) colors and allows the user to choose one of
20 public class ColorPickerSimple extends View {
21 private static final int ROW_COUNT = 2;
23 private static final int COLUMN_COUNT = 4;
25 private static final int GRID_CELL_COUNT = ROW_COUNT * COLUMN_COUNT;
27 private static final int[] COLORS = { Color.RED,
37 private Paint mBorderPaint;
39 private Rect[] mBounds;
41 private Paint[] mPaints;
43 private OnColorChangedListener mOnColorTouchedListener;
45 private int mLastTouchedXPosition;
47 private int mLastTouchedYPosition;
49 public ColorPickerSimple(Context context) {
53 public ColorPickerSimple(Context context, AttributeSet attrs) {
54 super(context, attrs);
57 public ColorPickerSimple(Context context, AttributeSet attrs, int defStyle) {
58 super(context, attrs, defStyle);
62 * Initializes the listener and precalculates the grid and color positions.
64 * @param onColorChangedListener The listener that gets notified when the user touches
67 public void init(OnColorChangedListener onColorChangedListener) {
68 mOnColorTouchedListener = onColorChangedListener;
70 // This will get calculated when the layout size is updated.
73 mPaints = new Paint[GRID_CELL_COUNT];
74 for (int i = 0; i < GRID_CELL_COUNT; ++i) {
75 Paint newPaint = new Paint();
76 newPaint.setColor(COLORS[i]);
77 mPaints[i] = newPaint;
80 mBorderPaint = new Paint();
81 int borderColor = getContext().getResources().getColor(R.color.color_picker_border_color);
82 mBorderPaint.setColor(borderColor);
84 // Responds to the user touching the grid and works out which color has been chosen as
85 // a result, depending on the X,Y coordinate. Note that we respond to the click event
86 // here, but the onClick() method doesn't provide us with the X,Y coordinates, so we
87 // track them in onTouchEvent() below. This way the grid reacts properly to touch events
88 // whereas if we put this onClick() code in onTouchEvent below then we get some strange
89 // interactions with the ScrollView in the parent ColorPickerDialog.
90 setOnClickListener(new OnClickListener() {
92 public void onClick(View v) {
93 if (mOnColorTouchedListener != null && getWidth() > 0 && getHeight() > 0) {
94 int column = mLastTouchedXPosition * COLUMN_COUNT / getWidth();
95 int row = mLastTouchedYPosition * ROW_COUNT / getHeight();
97 int colorIndex = (row * COLUMN_COUNT) + column;
98 if (colorIndex >= 0 && colorIndex < COLORS.length) {
99 mOnColorTouchedListener.onColorChanged(COLORS[colorIndex]);
107 * Draws the grid of colors, based on the rectangles calculated in onSizeChanged().
108 * Also draws borders in between the colored rectangles.
110 * @param canvas The canvas the colors are drawn onto.
113 public void onDraw(Canvas canvas) {
114 if (mBounds == null || mPaints == null) {
118 canvas.drawColor(Color.WHITE);
120 // Draw the actual colored rectangles.
121 for (int i = 0; i < GRID_CELL_COUNT; ++i) {
122 canvas.drawRect(mBounds[i], mPaints[i]);
125 // Draw 1px borders between the rows.
126 for (int i = 0; i < ROW_COUNT - 1; ++i) {
128 mBounds[i * COLUMN_COUNT].bottom + 1,
130 mBounds[i * COLUMN_COUNT].bottom + 1,
134 // Draw 1px borders between the columns.
135 for (int j = 0; j < COLUMN_COUNT - 1; ++j) {
136 canvas.drawLine(mBounds[j].right + 1,
138 mBounds[j].right + 1,
145 * Stores the X,Y coordinates of the touch so that we can use them in the onClick() listener
146 * above to work out where the click was on the grid.
148 * @param event The MotionEvent the X,Y coordinates are retrieved from.
151 public boolean onTouchEvent(MotionEvent event) {
152 if (event.getAction() == MotionEvent.ACTION_DOWN) {
153 mLastTouchedXPosition = (int) event.getX();
154 mLastTouchedYPosition = (int) event.getY();
156 return super.onTouchEvent(event);
160 * Recalculates the color grid with the new sizes.
163 protected void onSizeChanged(int width, int height, int oldw, int oldh) {
164 calculateGrid(width, height);
168 * Calculates the sizes and positions of the cells in the grid, splitting
169 * them up as evenly as possible. Leaves 3 pixels between each cell so that
170 * we can draw a border between them as well, and leaves a pixel around the
173 private void calculateGrid(final int width, final int height) {
174 mBounds = new Rect[GRID_CELL_COUNT];
176 for (int i = 0; i < ROW_COUNT; ++i) {
177 for (int j = 0; j < COLUMN_COUNT; ++j) {
178 int left = j * (width + 1) / COLUMN_COUNT + 1;
179 int right = (j + 1) * (width + 1) / COLUMN_COUNT - 2;
181 int top = i * (height + 1) / ROW_COUNT + 1;
182 int bottom = (i + 1) * (height + 1) / ROW_COUNT - 2;
184 Rect rect = new Rect(left, top, right, bottom);
185 mBounds[(i * COLUMN_COUNT) + j] = rect;